public function getInstance(Constraint $constraint)
 {
     $className = $constraint->validatedBy();
     if (!isset($this->validators[$className])) {
         $this->validators[$className] = new $className();
     }
     return $this->validators[$className];
 }
Example #2
0
 /**
  * Adds a constraint to the getter of the given property
  *
  * The name of the getter is assumed to be the name of the property with an
  * uppercased first letter and either the prefix "get" or "is".
  *
  * @param  string     $property    The name of the property
  * @param  Constraint $constraint  The constraint
  * @return ClassMetadata           This object
  */
 public function addGetterConstraint($property, Constraint $constraint)
 {
     if (!isset($this->getters[$property])) {
         $this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
         $this->addMemberMetadata($this->getters[$property]);
     }
     $constraint->addImplicitGroupName($this->getShortClassName());
     $this->getters[$property]->addConstraint($constraint);
     return $this;
 }
 /**
  * Returns a contraint validator from the container service, setting it if it
  * doesn't exist yet
  *
  * Throws an exception if validator service is not instance of
  * ConstraintValidatorInterface.
  *
  * @param  Constraint $constraint
  * @return ConstraintValidatorInterface
  * @throws \LogicException
  */
 public function getInstance(Constraint $constraint)
 {
     $className = $constraint->validatedBy();
     $id = $this->getServiceIdFromClass($className);
     if (!$this->container->hasService($id)) {
         $this->container->setService($id, new $className());
     }
     $validator = $this->container->getService($id);
     if (!$validator instanceof ConstraintValidatorInterface) {
         throw new \LogicException('Service "' . $id . '" is not instance of ConstraintValidatorInterface');
     }
     return $validator;
 }