<commit_before><?php

/**
 * This file is part of the Lean Mapper library (http://www.leanmapper.com)
 *
 * Copyright (c) 2013 <NAME> (aka Tharos)
 *
 * For the full copyright and license information, please view the file
 * license-mit.txt that was distributed with this source code.
 */

namespace LeanMapper;

use DibiRow;
use LeanMapper\Exception\InvalidArgumentException;
use LeanMapper\Exception\InvalidStateException;
use LeanMapper\Reflection\AnnotationsParser;
use ReflectionClass;

/**
 * Base class for concrete repositories
 *
 * @author <NAME>
 */
abstract class Repository
{

	/** @var Connection */
	protected $connection;

	/** @var IMapper */
	protected $mapper;

	/** @var IEntityFactory */
	protected $entityFactory;

	/** @var string */
	protected $table;

	/** @var string */
	protected $entityClass;

	/** @var Events */
	protected $events;

	/** @var string */
	private $docComment;

	/** @var bool */
	private $tableAnnotationChecked = false;


	/**
	 * @param Connection $connection
	 * @param IMapper $mapper
	 * @param IEntityFactory $entityFactory
	 */
	public function __construct(Connection $connection, IMapper $mapper, IEntityFactory $entityFactory)
	{
		$this->connection = $connection;
		$this->mapper = $mapper;
		$this->entityFactory = $entityFactory;
		$this->events = new Events;
		$this->initEvents();
	}

	/**
	 * @param string $name
	 * @return array|null
	 */
	public function &__get($name)
	{
		if (preg_match('#^on[A-Z]#', $name)) {
			return $this->events->getCallbacksReference(lcfirst(substr($name, 2)));
		}
	}

	/**
	 * @return Fluent
	 */
	protected function createFluent()
	{
		$table = $this->getTable();
		$statement = $this->connection->select('%n.*', $table)->from($table);
		$filters = $this->mapper->getImplicitFilters($this->mapper->getEntityClass($table), $this);
		if (!empty($filters)) {
			$targetedArgs = array();
			if ($filters instanceof ImplicitFilters) {
				$targetedArgs = $filters->getTargetedArgs();
				$filters = $filters->getFilters();
			}
			foreach ($filters as $filter) {
				$args = array($filter);
				if (array_key_exists($filter, $targetedArgs)) {
					$args = array_merge($args, $targetedArgs[$filter]);
				}
				call_user_func_array(array($statement, 'applyFilter'), $args);<commit_msg>Allowed to pass dynamic arguments for filters in Repository::createFluent<commit_after><?php

/**
 * This file is part of the Lean Mapper library (http://www.leanmapper.com)
 *
 * Copyright (c) 2013 <NAME> (aka Tharos)
 *
 * For the full copyright and license information, please view the file
 * license-mit.txt that was distributed with this source code.
 */

namespace LeanMapper;

use DibiRow;
use LeanMapper\Exception\InvalidArgumentException;
use LeanMapper\Exception\InvalidStateException;
use LeanMapper\Reflection\AnnotationsParser;
use ReflectionClass;

/**
 * Base class for concrete repositories
 *
 * @author <NAME>
 */
abstract class Repository
{

	/** @var Connection */
	protected $connection;

	/** @var IMapper */
	protected $mapper;

	/** @var IEntityFactory */
	protected $entityFactory;

	/** @var string */
	protected $table;

	/** @var string */
	protected $entityClass;

	/** @var Events */
	protected $events;

	/** @var string */
	private $docComment;

	/** @var bool */
	private $tableAnnotationChecked = false;


	/**
	 * @param Connection $connection
	 * @param IMapper $mapper
	 * @param IEntityFactory $entityFactory
	 */
	public function __construct(Connection $connection, IMapper $mapper, IEntityFactory $entityFactory)
	{
		$this->connection = $connection;
		$this->mapper = $mapper;
		$this->entityFactory = $entityFactory;
		$this->events = new Events;
		$this->initEvents();
	}

	/**
	 * @param string $name
	 * @return array|null
	 */
	public function &__get($name)
	{
		if (preg_match('#^on[A-Z]#', $name)) {
			return $this->events->getCallbacksReference(lcfirst(substr($name, 2)));
		}
	}

	/**
	 * @return Fluent
	 */
	protected function createFluent(/*$filterArg1, $filterArg2, ...*/)
	{
		$table = $this->getTable();
		$statement = $this->connection->select('%n.*', $table)->from($table);
		$filters = $this->mapper->getImplicitFilters($this->mapper->getEntityClass($table), $this);
		if (!empty($filters)) {
			$targetedArgs = array();
			$funcArgs = func_get_args();
			if ($filters instanceof ImplicitFilters) {
				$targetedArgs = $filters->getTargetedArgs();
				$filters = $filters->getFilters();
			}
			foreach ($filters as $filter) {
				$args = array($filter);
				if (array_key_exists($filter, $targetedArgs)) {
					$args = array_merge($args, $targetedArgs[$filter]);
				}
				if (!empty($funcArgs)) {
					$args = array_merge($args, $funcArgs);
				}
				call_user_func_array(array($statement, 'applyFilter'), $args);
{'fraction_non_alphanumeric': 0.15711878685762426, 'fraction_numerical': 0.002527379949452401, 'mean_word_length': 4.2015334063526835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '3710602', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1476, 'n_words': 359}