/** * {@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 ($xml->{'group-sequence-provider'} as $provider) { $metadata->setGroupSequenceProvider(true); } 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; }
public function testLoadGroupSequenceProvider() { $loader = new XmlFileLoader(__DIR__ . '/constraint-mapping.xml'); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $expected->setGroupSequenceProvider(true); $this->assertEquals($expected, $metadata); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->setGroupSequenceProvider(true); $metadata->addPropertyConstraint('teamAScore', new Assert\NotNull()); $metadata->addPropertyConstraint('teamAScore', new Assert\Range(['min' => 0, 'max' => 64])); $metadata->addPropertyConstraint('teamBScore', new Assert\NotNull()); $metadata->addPropertyConstraint('teamBScore', new Assert\Range(['min' => 0, 'max' => 64])); $metadata->addPropertyConstraint('teamBForfeit', new Assert\NotNull(['groups' => ['SetScore', 'Forfeit']])); }
/** * {@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; }
/** * {@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 testValidate_groupSequenceProvider() { $entity = new GroupSequenceProviderEntity(); $metadata = new ClassMetadata(get_class($entity)); $metadata->addPropertyConstraint('firstName', new FailingConstraint(array('groups' => 'First'))); $metadata->addPropertyConstraint('lastName', new FailingConstraint(array('groups' => 'Second'))); $metadata->setGroupSequenceProvider(true); $this->factory->addClassMetadata($metadata); $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation('Failed', array(), $entity, 'firstName', '')); $entity->setGroups(array('First')); $result = $this->validator->validate($entity); $this->assertEquals($violations, $result); $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation('Failed', array(), $entity, 'lastName', '')); $entity->setGroups(array('Second')); $result = $this->validator->validate($entity); $this->assertEquals($violations, $result); $entity->setGroups(array()); $result = $this->validator->validate($entity); $this->assertEquals(new ConstraintViolationList(), $result); }
public function testReplaceDefaultGroupWithArrayFromGroupSequenceProvider() { $sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity'); $entity = new GroupSequenceProviderEntity($sequence); $callback1 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 2'); }; $callback2 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 3'); }; $metadata = new ClassMetadata(get_class($entity)); $metadata->addConstraint(new Callback(array('callback' => function () { }, 'groups' => 'Group 1'))); $metadata->addConstraint(new Callback(array('callback' => $callback1, 'groups' => 'Group 2'))); $metadata->addConstraint(new Callback(array('callback' => $callback2, 'groups' => 'Group 3'))); $metadata->setGroupSequenceProvider(true); $this->metadataFactory->addMetadata($metadata); $violations = $this->validate($entity, null, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); $this->assertSame('Violation in Group 2', $violations[0]->getMessage()); }
public function testGroupSequenceProvider() { $metadata = new ClassMetadata(self::PROVIDERCLASS); $metadata->setGroupSequenceProvider(true); $this->assertTrue($metadata->isGroupSequenceProvider()); }
/** * 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 function testLoadGroupSequenceProviderAnnotation() { $loader = new AnnotationLoader(new AnnotationReader()); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $expected->setGroupSequenceProvider(true); $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }
/** * 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); } } } } }