<commit_before><?php
/**
 * Widget Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Widget\Validator;

/**
 * Check if the input is existing Doctrine ORM entity
 * 
 * @author      <NAME> <<EMAIL>>
 * @property \Widget\EntityManager $entityManager The doctrine orm entity manager widget
 * @method \Doctrine\ORM\EntityManager entityManager() Returns the doctrine orm entity object
 * @todo        Adds property like dql, queryBuilder, etc
 */
class EntityExists extends AbstractValidator
{
    protected $notFoundMessage = '%name% not exists';
    
    protected $notMessage = '%name% already exists';

    protected $entityClass;
    
    protected $entity;
    
    public function __invoke($input, $entityClass = null)
    {
        $entityClass && $this->storeOption('entityClass', $entityClass);
        
        return $this->isValid($input);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function validate($input)
    {
        $this->entity = $this->entityManager()->find($this->entityClass, $input);
        
        if (!$this->entity) {
            $this->addError('notFound');
            return false;
        }
        
        return true;
    }
    
    /**
     * Returns the entity object
     * 
     * @return object
     */
    public function getEntity()
    {
        return $this->entity;
    }
}<commit_msg>added field & criteria option for entityExists validtor<commit_after><?php
/**
 * Widget Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Widget\Validator;

/**
 * Check if the input is existing Doctrine ORM entity
 * 
 * @author      <NAME> <<EMAIL>>
 * @property \Widget\EntityManager $entityManager The doctrine orm entity manager widget
 * @method \Doctrine\ORM\EntityManager entityManager() Returns the doctrine orm entity object
 */
class EntityExists extends AbstractValidator
{
    protected $notFoundMessage = '%name% not exists';
   
    protected $notMessage = '%name% already exists';

    protected $entityClass;
   
    protected $field;
   
    protected $entity;
   
    protected $criteria;
   
    public function __invoke($input, $entityClass = null, $filed = null)
    {
        $entityClass && $this->storeOption('entityClass', $entityClass);
        $filed && $this->storeOption('field', $filed);
        
        return $this->isValid($input);
    }
    
    protected function validate($input)
    {
        /* @var $em \Doctrine\ORM\EntityManager */
        $em = $this->entityManager();

        /* @var $repo \Doctrine\ORM\EntityRepository */
        $repo = $em->getRepository($this->entityClass);

        if ($this->field) {
            $this->entity = $repo->findOneBy(array(
                $this->field => $input
            ));
        } elseif ($this->criteria) {
            $this->entity = $repo->findOneBy($this->criteria);
        } else {
            $this->entity = $repo->find((string)$input);
        }
       
        if (!$this->entity) {
            $this->addError('notFound');
            return false;
        }
       
        return true;
    }
   
    /**
     * Returns the entity object
     *
     * @return object
     */
    public function getEntity()
    {
        return $this->entity;
    }
}
{'fraction_non_alphanumeric': 0.13009082918253737, 'fraction_numerical': 0.005859947260474656, 'mean_word_length': 2.2953667953667956, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 40, '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': '5920856', 'n_tokens_mistral': 968, 'n_tokens_neox': 950, 'n_words': 235}