public function testLoadClassMetadata() { $loader = new XmlFileLoader(__DIR__ . '/constraint-mapping.xml'); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new ConstraintB()); $expected->addConstraint(new Callback('validateMe')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addConstraint(new Callback(array('Symfony\\Component\\Validator\\Tests\\Fixtures\\CallbackClass', 'callback'))); $expected->addConstraint(new Traverse(false)); $expected->addPropertyConstraint('firstName', new NotNull()); $expected->addPropertyConstraint('firstName', new Range(array('min' => 3))); $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B'))); $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3))))); $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3)))))); $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array('foo' => array(new NotNull(), new Range(array('min' => 3))), 'bar' => array(new Range(array('min' => 5))))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); $this->assertEquals($expected, $metadata); }
/** * Valorizes model validation metadata * * @return void */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addGetterConstraint('title', new Assert\NotBlank()); $metadata->addGetterConstraint('content', new Assert\NotBlank()); $metadata->addGetterConstraint('image', new Assert\NotBlank()); $metadata->addGetterConstraint('image', new Assert\Image(['maxWidth' => 400])); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->getName(); $loaded = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addConstraint($constraint); } } elseif ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $loaded = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } $loaded = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addGetterConstraint($name, $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addGetterConstraint($name, $constraint); } $loaded = true; } } } return $loaded; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = array(); $xml = $this->parseFile($this->file); foreach ($xml->namespace as $namespace) { $this->namespaces[(string) $namespace['prefix']] = trim((string) $namespace); } foreach ($xml->class as $class) { $this->classes[(string) $class['name']] = $class; } } if (isset($this->classes[$metadata->getClassName()])) { $xml = $this->classes[$metadata->getClassName()]; foreach ($this->parseConstraints($xml->constraint) as $constraint) { $metadata->addConstraint($constraint); } foreach ($xml->property as $property) { foreach ($this->parseConstraints($property->constraint) as $constraint) { $metadata->addPropertyConstraint((string) $property['name'], $constraint); } } foreach ($xml->getter as $getter) { foreach ($this->parseConstraints($getter->constraint) as $constraint) { $metadata->addGetterConstraint((string) $getter['property'], $constraint); } } return true; } return false; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = Yaml::load($this->file); } // TODO validation if (isset($this->classes[$metadata->getClassName()])) { $yaml = $this->classes[$metadata->getClassName()]; if (isset($yaml['constraints'])) { foreach ($this->parseNodes($yaml['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($yaml['properties'])) { foreach ($yaml['properties'] as $property => $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } if (isset($yaml['getters'])) { foreach ($yaml['getters'] as $getter => $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } return true; } return false; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $annotClass = 'Symfony\\Component\\Validator\\Constraints\\Validation'; $reflClass = $metadata->getReflectionClass(); $loaded = false; if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) { foreach ($annot->constraints as $constraint) { $metadata->addConstraint($constraint); } $loaded = true; } foreach ($reflClass->getProperties() as $property) { if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) { foreach ($annot->constraints as $constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } $loaded = true; } } foreach ($reflClass->getMethods() as $method) { if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) { foreach ($annot->constraints as $constraint) { // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); $metadata->addGetterConstraint($name, $constraint); } $loaded = true; } } return $loaded; }
public function loadClassMetadata(ClassMetadata $metadata) { /** @var \ReflectionClass $refClass */ $refClass = $metadata->getReflectionClass(); $baseClass = $this->classNameProvider->getBaseClass($refClass->name); if (!$baseClass) { return false; } if (!array_key_exists($baseClass, $this->config->models)) { return false; } foreach ($this->config->models[$baseClass]->properties as $propName => $property) { if ($property->validation) { if (!is_array($property->validation)) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation must be array"); } foreach ($property->validation as $index => $rule) { if (!is_array($rule) or count($rule) != 1) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation: rule #{$index} must be array of 1 element which contains array"); } $constraintClass = array_keys($rule)[0]; $constraintOptions = $rule[$constraintClass]; $fullClass = $this->getFullConstraintClass($constraintClass); $metadata->addGetterConstraint($propName, new $fullClass($constraintOptions)); } } } return true; }
public function testGetterConstraint() { $test = $this; $entity = new Entity(); $entity->setLastName('Schussek'); $callback = function ($value, ExecutionContextInterface $context) use($test, $entity) { $propertyMetadatas = $test->metadata->getPropertyMetadata('lastName'); $test->assertSame($test::ENTITY_CLASS, $context->getClassName()); $test->assertSame('lastName', $context->getPropertyName()); $test->assertSame('lastName', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Schussek', $context->getValue()); $test->assertSame('Schussek', $value); $context->addViolation('Message %param%', array('%param%' => 'value')); }; $this->metadata->addGetterConstraint('lastName', new Callback(array('callback' => $callback, 'groups' => 'Group'))); $violations = $this->validate($entity, null, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); $this->assertSame('Message value', $violations[0]->getMessage()); $this->assertSame('Message %param%', $violations[0]->getMessageTemplate()); $this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters()); $this->assertSame('lastName', $violations[0]->getPropertyPath()); $this->assertSame($entity, $violations[0]->getRoot()); $this->assertSame('Schussek', $violations[0]->getInvalidValue()); $this->assertNull($violations[0]->getMessagePluralization()); $this->assertNull($violations[0]->getCode()); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = Yaml::parse($this->file); // empty file if (null === $this->classes) { return false; } // not an array if (!is_array($this->classes)) { throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file)); } if (isset($this->classes['namespaces'])) { foreach ($this->classes['namespaces'] as $alias => $namespace) { $this->addNamespaceAlias($alias, $namespace); } unset($this->classes['namespaces']); } } // TODO validation if (isset($this->classes[$metadata->getClassName()])) { $yaml = $this->classes[$metadata->getClassName()]; if (isset($yaml['group_sequence_provider'])) { $metadata->setGroupSequenceProvider((bool) $yaml['group_sequence_provider']); } if (isset($yaml['group_sequence'])) { $metadata->setGroupSequence($yaml['group_sequence']); } if (isset($yaml['constraints']) && is_array($yaml['constraints'])) { foreach ($this->parseNodes($yaml['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($yaml['properties']) && is_array($yaml['properties'])) { foreach ($yaml['properties'] as $property => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } } if (isset($yaml['getters']) && is_array($yaml['getters'])) { foreach ($yaml['getters'] as $getter => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } } return true; } return false; }
public function testValidateInOtherGroupTraversesNoGroupSequence() { $entity = new Entity(); $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array('groups' => 'First'))); $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array('groups' => $this->metadata->getDefaultGroup()))); $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup())); $this->visitor->validate($entity, $this->metadata->getDefaultGroup(), ''); // Only group "Second" was validated $violations = new ConstraintViolationList(array(new ConstraintViolation('Failed', 'Failed', array(), 'Root', 'lastName', ''))); $this->assertEquals($violations, $this->visitor->getViolations()); }
public function testLoadClassMetadata() { $loader = new YamlFileLoader(__DIR__ . '/constraint-mapping.yml'); $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $expected->addConstraint(new NotNull()); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new Min(3)); $expected->addConstraint(new Choice(array('A', 'B'))); $expected->addConstraint(new All(array(new NotNull(), new Min(3)))); $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3))))); $expected->addConstraint(new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => array(new Min(5)))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $this->assertEquals($expected, $metadata); }
/** * {@inheritdoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->name; $success = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof GroupSequenceProvider) { $metadata->setGroupSequenceProvider(true); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $success = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->name == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->name, $constraint); } $success = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->name == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { if ($constraint instanceof Callback) { $constraint->callback = $method->getName(); $constraint->methods = null; $metadata->addConstraint($constraint); } elseif ($constraint instanceof Constraint) { if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { $metadata->addGetterConstraint(lcfirst($matches[2]), $constraint); } else { throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name)); } } $success = true; } } } return $success; }
public function testLoadClassMetadata() { $loader = new AnnotationLoader(); $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $expected->addConstraint(new NotNull()); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new Min(3)); $expected->addConstraint(new Choice(array('A', 'B'))); $expected->addConstraint(new All(array(new NotNull(), new Min(3)))); $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3))))); $expected->addConstraint(new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => new Min(5))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); // load reflection class so that the comparison passes $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = array(); $xml = $this->parseFile($this->file); foreach ($xml->namespace as $namespace) { $this->addNamespaceAlias((string) $namespace['prefix'], trim((string) $namespace)); } foreach ($xml->class as $class) { $this->classes[(string) $class['name']] = $class; } } if (isset($this->classes[$metadata->getClassName()])) { $xml = $this->classes[$metadata->getClassName()]; foreach ($xml->{'group-sequence-provider'} as $provider) { $metadata->setGroupSequenceProvider(true); } foreach ($xml->{'group-sequence'} as $groupSequence) { if (count($groupSequence->value) > 0) { $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value)); } } foreach ($this->parseConstraints($xml->constraint) as $constraint) { $metadata->addConstraint($constraint); } foreach ($xml->property as $property) { foreach ($this->parseConstraints($property->constraint) as $constraint) { $metadata->addPropertyConstraint((string) $property['name'], $constraint); } } foreach ($xml->getter as $getter) { foreach ($this->parseConstraints($getter->constraint) as $constraint) { $metadata->addGetterConstraint((string) $getter['property'], $constraint); } } return true; } return false; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = Yaml::load($this->file); } // empty file if (null === $this->classes) { return false; } // not an array if (!is_array($this->classes)) { throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file)); } // TODO validation if (isset($this->classes[$metadata->getClassName()])) { $yaml = $this->classes[$metadata->getClassName()]; if (isset($yaml['constraints'])) { foreach ($this->parseNodes($yaml['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($yaml['properties'])) { foreach ($yaml['properties'] as $property => $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } if (isset($yaml['getters'])) { foreach ($yaml['getters'] as $getter => $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } return true; } return false; }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('title', new Constraints\NotBlank()); $metadata->addPropertyConstraint('title', new Constraints\MinLength(5)); $metadata->addGetterConstraint('description', new Constraints\NotNull()); }
/** * Loads the validation metadata from the given XML class description. * * @param ClassMetadata $metadata The metadata to load * @param array $classDescription The XML class description */ private function loadClassMetadataFromXml(ClassMetadata $metadata, $classDescription) { if (count($classDescription->{'group-sequence-provider'}) > 0) { $metadata->setGroupSequenceProvider(true); } foreach ($classDescription->{'group-sequence'} as $groupSequence) { if (count($groupSequence->value) > 0) { $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value)); } } foreach ($this->parseConstraints($classDescription->constraint) as $constraint) { $metadata->addConstraint($constraint); } foreach ($classDescription->property as $property) { foreach ($this->parseConstraints($property->constraint) as $constraint) { $metadata->addPropertyConstraint((string) $property['name'], $constraint); } } foreach ($classDescription->getter as $getter) { foreach ($this->parseConstraints($getter->constraint) as $constraint) { $metadata->addGetterConstraint((string) $getter['property'], $constraint); } } }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('user', new NotBlank(array('message' => 'You must enter your name'))); $metadata->addGetterConstraint('comment', new NotBlank(array('message' => 'You must enter a comment'))); }
/** * Test MetaData merge with parent annotation. */ public function testLoadClassMetadataAndMerge() { $loader = new AnnotationLoader(new AnnotationReader()); // Load Parent MetaData $parent_metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\EntityParent'); $loader->loadClassMetadata($parent_metadata); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); // Merge parent metaData. $metadata->mergeConstraints($parent_metadata); $loader->loadClassMetadata($metadata); $expected_parent = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\EntityParent'); $expected_parent->addPropertyConstraint('other', new NotNull()); $expected_parent->getReflectionClass(); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $expected->mergeConstraints($expected_parent); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new Callback(array('Symfony\\Component\\Validator\\Tests\\Fixtures\\CallbackClass', 'callback'))); $expected->addConstraint(new Callback('validateMe')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addPropertyConstraint('firstName', new NotNull()); $expected->addPropertyConstraint('firstName', new Range(array('min' => 3))); $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3))))); $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3)))))); $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array('foo' => array(new NotNull(), new Range(array('min' => 3))), 'bar' => new Range(array('min' => 5)))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); // load reflection class so that the comparison passes $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }
/** * Add constraint on product values integrity. * * It registers constraint assertion that "hasValidValues" must return true on * mass edit form submission. * * @param ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addGetterConstraint('validValues', new IsTrue(['message' => 'mass_edit.edit_common_attributes.invalid_values'])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addGetterConstraint('ajax', new NotNull()); }
/** * Loads the validation metadata from the given YAML class description. * * @param ClassMetadata $metadata The metadata to load * @param array $classDescription The YAML class description */ private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription) { if (isset($classDescription['group_sequence_provider'])) { $metadata->setGroupSequenceProvider((bool) $classDescription['group_sequence_provider']); } if (isset($classDescription['group_sequence'])) { $metadata->setGroupSequence($classDescription['group_sequence']); } if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) { foreach ($this->parseNodes($classDescription['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($classDescription['properties']) && is_array($classDescription['properties'])) { foreach ($classDescription['properties'] as $property => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } } if (isset($classDescription['getters']) && is_array($classDescription['getters'])) { foreach ($classDescription['getters'] as $getter => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } } }