<commit_before><?php
/**
 * Active Record Set ORM
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2012, <NAME>
 * @link https://github.com/nickwhitt/CRUD Source
 * @license http://www.apache.org/licenses/ Apache License Version 2.0
 */

namespace CRUD;
class ActiveSet extends Base {
	protected $conditions = array();
	protected $parameters = array();
	protected $orders = array();
	
	/**
	 * Statically generate class constructor
	 *
	 * @param void
	 * @return static extended class
	 */
	public static function create($table) {
		return new static($table);
	}
	
	/**
	 * Query termination method
	 *
	 * Executes the constructed query and returns the $styled results
	 *
	 * @param mixed $class
	 * @param int $style
	 * @return mixed
	 */
	public function fetchAll($class=NULL, $style=\PDO::FETCH_OBJ) {
		$records = array();
		foreach (Query::query($this->buildStatement(), $this->parameters, $style) as $row) {
			$records[] = is_null($class)
				? new ActiveModel($this->table, $row->{$this->primary_key})
				: new $class($row->{$this->primary_key});
		}
		
		return $records;
	}
	
	/**
	 * Query termination method
	 *
	 * Executes the contructed query and returns the $styled result
	 *
	 * @param mixed $class
	 * @param int $style
	 * @return mixed
	 */
	public function fetchOne($class=NULL, $style=\PDO::FETCH_OBJ) {
		$row = Query::prepare($this->buildStatement(), $this->parameters)->fetch($style);
		return is_null($class)
			? new ActiveModel($this->table, $row->{$this->primary_key})
			: new $class($row->{$this->primary_key});
	}
	
	/**
	 * Filter Helper
	 *
	 * Determines the appropriate filter method based on $attribute type
	 *
	 * @param str $attribute
	 * @param mixed $condition
	 * @param bool $negate Negates the filter
	 * @return self
	 */
	public function filterBy($attribute, $condition, $negate=FALSE) {
		if (is_array($condition)) {
			return $this->filterByList($attribute, $condition, $negate);
		}
		
		if (is_null($condition) OR strtolower($condition) == 'null') {
			return $this->filterByNull($attribute, $negate);
		}
		
		return $this->filterByString($attribute, $condition, $negate);
	}
	
	/**
	 * Filters results by the conditional string
	 *
	 * Creates clauses like:
	 *   where X [!]= :Y
	 *   where X [not] like :Y
	 *
	 * @param str $attribute
	 * @param str $condition
	 * @param bool $negate
	 * @return self
	 */
	public function filterByString($attribute, $condition, $negate=FALSE) {
		$this->parameters[] = $condition;
		$this->conditions[] = strpos($condition, '%') !== FALSE
			? Query::buildLikeCondition($attribute, $negate)
			: Query::buildEqualCondition($attribute, $negate);
		
		return $this;
	}
	
	/**
	 * Filters NULL results
	 *
	 * Creates clause like: where X is [not] null
	 *
	 * @param str $attribute
	 * @param bool $negate
	 * @return self
	 */
	public function filterByNull($attribute, $negate=FALSE) {
		$this->conditions[] = Query::buildNullCondition($attribute, $negate);
		return $this;
	}
	
	/**
	 * Filters results by the conditional list
	 *
	 * Creates clause like: where X [not] in (:Y1[, :Yn])
	 *
	 * @param str $attribute
	 * @param array $condition
	 * @param bool $negate
	 * @return self
	 */
	public function filterByList($attribute, array $condition, $negate=FALSE) {
		$this->conditions[] = Query::buildInCondition($attribute, count($condition), $negate);
		foreach ($conditions as $paramter) {
			$this->parameters[] = $paramter;
		}
		
		return $this;
	}
	
	/**
	 * Filter by boolean value
	 *
	 * @param str attribute
	 * @param mixed $condition string, bool, or int
	 * @return self
	 */
	public function filterByBool($attribute, $condition) {
		// for "yes", "on", "true", true, 1: where X = 0
		// for "no", "off", "false", false, 0: where X = 1<commit_msg>Typo fix for filterByList() conditions.<commit_after><?php
/**
 * Active Record Set ORM
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2012, <NAME>
 * @link https://github.com/nickwhitt/CRUD Source
 * @license http://www.apache.org/licenses/ Apache License Version 2.0
 */

namespace CRUD;
class ActiveSet extends Base {
	protected $conditions = array();
	protected $parameters = array();
	protected $orders = array();
	
	/**
	 * Statically generate class constructor
	 *
	 * @param void
	 * @return static extended class
	 */
	public static function create($table) {
		return new static($table);
	}
	
	/**
	 * Query termination method
	 *
	 * Executes the constructed query and returns the $styled results
	 *
	 * @param mixed $class
	 * @param int $style
	 * @return mixed
	 */
	public function fetchAll($class=NULL, $style=\PDO::FETCH_OBJ) {
		$records = array();
		foreach (Query::query($this->buildStatement(), $this->parameters, $style) as $row) {
			$records[] = is_null($class)
				? new ActiveModel($this->table, $row->{$this->primary_key})
				: new $class($row->{$this->primary_key});
		}
		
		return $records;
	}
	
	/**
	 * Query termination method
	 *
	 * Executes the contructed query and returns the $styled result
	 *
	 * @param mixed $class
	 * @param int $style
	 * @return mixed
	 */
	public function fetchOne($class=NULL, $style=\PDO::FETCH_OBJ) {
		$row = Query::prepare($this->buildStatement(), $this->parameters)->fetch($style);
		return is_null($class)
			? new ActiveModel($this->table, $row->{$this->primary_key})
			: new $class($row->{$this->primary_key});
	}
	
	/**
	 * Filter Helper
	 *
	 * Determines the appropriate filter method based on $attribute type
	 *
	 * @param str $attribute
	 * @param mixed $condition
	 * @param bool $negate Negates the filter
	 * @return self
	 */
	public function filterBy($attribute, $condition, $negate=FALSE) {
		if (is_array($condition)) {
			return $this->filterByList($attribute, $condition, $negate);
		}
		
		if (is_null($condition) OR strtolower($condition) == 'null') {
			return $this->filterByNull($attribute, $negate);
		}
		
		return $this->filterByString($attribute, $condition, $negate);
	}
	
	/**
	 * Filters results by the conditional string
	 *
	 * Creates clauses like:
	 *   where X [!]= :Y
	 *   where X [not] like :Y
	 *
	 * @param str $attribute
	 * @param str $condition
	 * @param bool $negate
	 * @return self
	 */
	public function filterByString($attribute, $condition, $negate=FALSE) {
		$this->parameters[] = $condition;
		$this->conditions[] = strpos($condition, '%') !== FALSE
			? Query::buildLikeCondition($attribute, $negate)
			: Query::buildEqualCondition($attribute, $negate);
		
		return $this;
	}
	
	/**
	 * Filters NULL results
	 *
	 * Creates clause like: where X is [not] null
	 *
	 * @param str $attribute
	 * @param bool $negate
	 * @return self
	 */
	public function filterByNull($attribute, $negate=FALSE) {
		$this->conditions[] = Query::buildNullCondition($attribute, $negate);
		return $this;
	}
	
	/**
	 * Filters results by the conditional list
	 *
	 * Creates clause like: where X [not] in (:Y1[, :Yn])
	 *
	 * @param str $attribute
	 * @param array $condition
	 * @param bool $negate
	 * @return self
	 */
	public function filterByList($attribute, array $conditions, $negate=FALSE) {
		$this->conditions[] = Query::buildInCondition($attribute, count($conditions), $negate);
		foreach ($conditions as $paramter) {
			$this->parameters[] = $paramter;
		}
		
		return $this;
	}
	
	/**
	 * Filter by boolean value
	 *
	 * @param str attribute
	 * @param mixed $condition string, bool, or int
	 * @return self
	 */
	public function filterByBool($attribute, $condition) {
		// for "yes", "on", "true", true, 1: where X = 0
		// for "no", "off", "false", false, 0: where X = 1
{'fraction_non_alphanumeric': 0.1565803382663848, 'fraction_numerical': 0.0029069767441860465, 'mean_word_length': 3.754396984924623, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1399799', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2288, 'n_words': 665}