<commit_before><?php

	/*
	 *
	 *	LiftKit MVC PHP Framework
	 *
	 */

	namespace LiftKit\Database\Result;

	use LiftKit\Database\Entity\Entity as DatabaseRow;

	use LiftKit\DependencyInjection\Container\Container;

	use PDO;
	use PDOStatement;

	use Countable;
	use Iterator;


	/**
	 * Class Result
	 *
	 * @package LiftKit\Database\Result
	 */
	class Result implements Countable, Iterator
	{
		/**
		 *
		 * @var array
		 */
		protected $current = false;

		/**
		 * @var PDOStatement
		 */
		protected $pdoStatement;

		/**
		 * @var callable
		 */
		protected $castCallback;

		/**
		 * @var int
		 */
		protected $cursor = -1;


		public function __construct (PDOStatement $pdoStatement, callable $castCallback = null)
		{
			$this->pdoStatement = $pdoStatement;
			$this->castCallback = $castCallback;
		}


		public function fetchAll ()
		{
			$data = array();

			while ($row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC)) {
				$data[] = $this->cast($row);
			}

			return $data;
		}


		public function fetchRow ()
		{
			$row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);

			if ($row) {
				return $this->cast($row);
			} else {
				return null;
			}
		}


		public function fetchColumn ($column = null)
		{
			$data = $this->fetchAll();
			$list = array();

			foreach ($data as $row) {
				if ($column == null) {
					$values = array_values($row->toArray());
					$list[] = array_shift($values);
				} else {
					$list[] = $row[$column];
				}
			}

			return $list;
		}


		public function fetchField ($field = null)
		{
			$row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);

			if (is_null($field)) {
				return array_shift($row);
			} else {
				return $row[$field];
			}
		}


		public function transform ($callback)
		{
			$array  = array();
			foreach ($this as $row)
			{
				$array[] = $callback($row);
			}

			return $array;
		}


		public function flatten ()
		{
			return $this->transform(
				function ($row)
				{
					return $row->toArray();
				}
			);
		}


		public function count()
		{
			return $this->pdoStatement->rowCount();
		}


		public function current()
		{
			return $this->current ? $this->cast($this->current) : false;
		}


		public function key()
		{
			return $this->cursor;
		}


		public function next()
		{
			if ($this->hasNext()) {
				$this->cursor++;
				$this->current = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
				if (empty($this->current))
					$this->current = false;
				else
					return true;
			}else
				$this->current = false;
			return false;
		}


		public function rewind()
		{
			if ($this->cursor != -1) {
				$this->cursor = -1;
				$this->pdoStatement->execute();
			} else {
				$this->next();
			}
		}


		public function valid()
		{
			return ($this->current != false) || (($this->cursor == -1) && ($this->count() > 0));
		}


		public function getQueryString ()
		{
			return $this->pdoStatement->queryString;
		}


		public function setCastCallback (callable $callback)
		{
			$this->castCallback = $callback;

			return $this;
		}


		protected function cast($data)
		{
			if (is_null($this->castCallback)) {
				return new DatabaseRow($data);

			} else {
				return call_user_func_array(
					$this->castCallback,<commit_msg>Allow setCastCallback to be null<commit_after><?php

	/*
	 *
	 *	LiftKit MVC PHP Framework
	 *
	 */

	namespace LiftKit\Database\Result;

	use LiftKit\Database\Entity\Entity as DatabaseRow;

	use LiftKit\DependencyInjection\Container\Container;

	use PDO;
	use PDOStatement;

	use Countable;
	use Iterator;


	/**
	 * Class Result
	 *
	 * @package LiftKit\Database\Result
	 */
	class Result implements Countable, Iterator
	{
		/**
		 *
		 * @var array
		 */
		protected $current = false;

		/**
		 * @var PDOStatement
		 */
		protected $pdoStatement;

		/**
		 * @var callable
		 */
		protected $castCallback;

		/**
		 * @var int
		 */
		protected $cursor = -1;


		public function __construct (PDOStatement $pdoStatement, callable $castCallback = null)
		{
			$this->pdoStatement = $pdoStatement;
			$this->castCallback = $castCallback;
		}


		public function fetchAll ()
		{
			$data = array();

			while ($row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC)) {
				$data[] = $this->cast($row);
			}

			return $data;
		}


		public function fetchRow ()
		{
			$row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);

			if ($row) {
				return $this->cast($row);
			} else {
				return null;
			}
		}


		public function fetchColumn ($column = null)
		{
			$data = $this->fetchAll();
			$list = array();

			foreach ($data as $row) {
				if ($column == null) {
					$values = array_values($row->toArray());
					$list[] = array_shift($values);
				} else {
					$list[] = $row[$column];
				}
			}

			return $list;
		}


		public function fetchField ($field = null)
		{
			$row = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);

			if (is_null($field)) {
				return array_shift($row);
			} else {
				return $row[$field];
			}
		}


		public function transform ($callback)
		{
			$array  = array();
			foreach ($this as $row)
			{
				$array[] = $callback($row);
			}

			return $array;
		}


		public function flatten ()
		{
			return $this->transform(
				function ($row)
				{
					return $row->toArray();
				}
			);
		}


		public function count()
		{
			return $this->pdoStatement->rowCount();
		}


		public function current()
		{
			return $this->current ? $this->cast($this->current) : false;
		}


		public function key()
		{
			return $this->cursor;
		}


		public function next()
		{
			if ($this->hasNext()) {
				$this->cursor++;
				$this->current = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
				if (empty($this->current))
					$this->current = false;
				else
					return true;
			}else
				$this->current = false;
			return false;
		}


		public function rewind()
		{
			if ($this->cursor != -1) {
				$this->cursor = -1;
				$this->pdoStatement->execute();
			} else {
				$this->next();
			}
		}


		public function valid()
		{
			return ($this->current != false) || (($this->cursor == -1) && ($this->count() > 0));
		}


		public function getQueryString ()
		{
			return $this->pdoStatement->queryString;
		}


		public function setCastCallback (callable $callback = null)
		{
			$this->castCallback = $callback;

			return $this;
		}


		protected function cast($data)
		{
			if (is_null($this->castCallback)) {
				return new DatabaseRow($data);

			} else {
				return call_user_func_array(
					$this->castCallback,
{'fraction_non_alphanumeric': 0.16150510994115824, 'fraction_numerical': 0.001548467017652524, 'mean_word_length': 2.6553480475382005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2447459', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2194, 'n_words': 464}