lib/Yucca/Validator/Constraints/UniqueEntityValidator.php
public function validate($entity, Constraint $constraint)
    {
        if (false === ($entity instanceof ModelAbstract )) {
            throw new UnexpectedTypeException($entity, 'ModelAbstract');
        }
        /**
         * @var ModelAbstract $entity
         */

        if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
            throw new UnexpectedTypeException($constraint->fields, 'array');
        }

        if (null !== $constraint->errorPath && !is_string($constraint->errorPath)) {
            throw new UnexpectedTypeException($constraint->errorPath, 'string or null');
        }

        $fields = (array) $constraint->fields;

        if (0 === count($fields)) {
            throw new ConstraintDefinitionException('At least one field has to be specified.');
        }

        if (!$constraint->selector) {
            $selectorName = str_replace('Model', 'Selector', get_class($entity));
        } else {
            $selectorName = $constraint->selector;
        }

        $selector = $this->selectorManager->getSelector($selectorName);
        $selectorMethods = get_class_methods($selectorName);

        $criteria = array();
        foreach ($fields as $fieldName) {
            $camelFieldName = str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName)));
            $selectorMethod = 'add'.$camelFieldName.'Criteria';
            if (false === in_array($selectorMethod, $selectorMethods)) {
                throw new ConstraintDefinitionException(sprintf("The field '%s' is not mapped", $fieldName));
            }

            $getterMethod = 'get'.$camelFieldName;
            $value = $entity->$getterMethod();

            if ($constraint->ignoreNull && null === $value) {
                return;
            }

            $selector->$selectorMethod($value);
        }

        $iterator = new \Yucca\Component\Iterator\Iterator(
            $selector,
            $this->entityManager,
            get_class($entity)
        );

        /* If no entity matched the query criteria or a single entity matched,
         * which is the same as the entity being validated, the criteria is
         * unique.
         */
        if (0 == $iterator->count() || (1 == $iterator->count() && $entity->getId() == ($iterator->current()->getId()))) {
            return;
        }

        $errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];

        //$this->context->addViolationAt($errorPath, $constraint->message, array(), $value);
        $this->context->buildViolation($constraint->message)
            ->atPath($errorPath)
            ->addViolation();
    }
public function validate($entity, Constraint $constraint)
    {
        if (false === ($entity instanceof ModelAbstract )) {
            throw new UnexpectedTypeException($entity, 'ModelAbstract');
        }
        /**
         * @var ModelAbstract $entity
         */

        if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
            throw new UnexpectedTypeException($constraint->fields, 'array');
        }

        if (null !== $constraint->errorPath && !is_string($constraint->errorPath)) {
            throw new UnexpectedTypeException($constraint->errorPath, 'string or null');
        }

        $fields = (array) $constraint->fields;

        if (0 === count($fields)) {
            throw new ConstraintDefinitionException('At least one field has to be specified.');
        }

        if (!$constraint->selector) {
            $selectorName = str_replace('Model', 'Selector', get_class($entity));
        } else {
            $selectorName = $constraint->selector;
        }

        $selector = $this->selectorManager->getSelector($selectorName);
        $selectorMethods = get_class_methods($selectorName);

        $criteria = array();
        foreach ($fields as $fieldName) {
            $camelFieldName = str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName)));
            $selectorMethod = 'add'.$camelFieldName.'Criteria';
            if (false === in_array($selectorMethod, $selectorMethods)) {
                throw new ConstraintDefinitionException(sprintf("The field '%s' is not mapped", $fieldName));
            }

            $getterMethod = 'get'.$camelFieldName;
            $value = $entity->$getterMethod();

            if ($constraint->ignoreNull && null === $value) {
                return;
            }

            $selector->$selectorMethod($value);
        }

        $iterator = new \Yucca\Component\Iterator\Iterator(
            $selector,
            $this->entityManager,
            get_class($entity)
        );

        /* If no entity matched the query criteria or a single entity matched,
         * which is the same as the entity being validated, the criteria is
         * unique.
         */
        if (0 == $iterator->count() || (1 == $iterator->count() && $entity->getId() == ($iterator->current()->getId()))) {
            return;
        }

        $errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];

        //$this->context->addViolationAt($errorPath, $constraint->message, array(), $value);
        $this->context->buildViolation($constraint->message)
            ->atPath($errorPath)
            ->addViolation();
    }
public function validate ( $ entity , Constraint $ constraint ) { if ( false === ( $ entity instanceof ModelAbstract ) ) { throw new UnexpectedTypeException ( $ entity , 'ModelAbstract' ) ; } /**
         * @var ModelAbstract $entity
         */ if ( ! is_array ( $ constraint -> fields ) && ! is_string ( $ constraint -> fields ) ) { throw new UnexpectedTypeException ( $ constraint -> fields , 'array' ) ; } if ( null !== $ constraint -> errorPath && ! is_string ( $ constraint -> errorPath ) ) { throw new UnexpectedTypeException ( $ constraint -> errorPath , 'string or null' ) ; } $ fields = ( array ) $ constraint -> fields ; if ( 0 === count ( $ fields ) ) { throw new ConstraintDefinitionException ( 'At least one field has to be specified.' ) ; } if ( ! $ constraint -> selector ) { $ selectorName = str_replace ( 'Model' , 'Selector' , get_class ( $ entity ) ) ; } else { $ selectorName = $ constraint -> selector ; } $ selector = $ this -> selectorManager -> getSelector ( $ selectorName ) ; $ selectorMethods = get_class_methods ( $ selectorName ) ; $ criteria = array ( ) ; foreach ( $ fields as $ fieldName ) { $ camelFieldName = str_replace ( ' ' , '' , ucwords ( str_replace ( '_' , ' ' , $ fieldName ) ) ) ; $ selectorMethod = 'add' . $ camelFieldName . 'Criteria' ; if ( false === in_array ( $ selectorMethod , $ selectorMethods ) ) { throw new ConstraintDefinitionException ( sprintf ( "The field '%s' is not mapped" , $ fieldName ) ) ; } $ getterMethod = 'get' . $ camelFieldName ; $ value = $ entity -> $ getterMethod ( ) ; if ( $ constraint -> ignoreNull && null === $ value ) { return ; } $ selector -> $ selectorMethod ( $ value ) ; } $ iterator = new \ Yucca \ Component \ Iterator \ Iterator ( $ selector , $ this -> entityManager , get_class ( $ entity ) ) ; /* If no entity matched the query criteria or a single entity matched,
         * which is the same as the entity being validated, the criteria is
         * unique.
         */ if ( 0 == $ iterator -> count ( ) || ( 1 == $ iterator -> count ( ) && $ entity -> getId ( ) == ( $ iterator -> current ( ) -> getId ( ) ) ) ) { return ; } $ errorPath = null !== $ constraint -> errorPath ? $ constraint -> errorPath : $ fields [ 0 ] ; //$this->context->addViolationAt($errorPath, $constraint->message, array(), $value); $ this -> context -> buildViolation ( $ constraint -> message ) -> atPath ( $ errorPath ) -> addViolation ( ) ; }
@param ModelAbstract $entity
@param Constraint    $constraint

@throws UnexpectedTypeException
@throws ConstraintDefinitionException
@param ModelAbstract $entity @param Constraint $constraint