本文整理汇总了PHP中Symfony\Component\Validator\Mapping\ClassMetadata::addGetterConstraint方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::addGetterConstraint方法的具体用法?PHP ClassMetadata::addGetterConstraint怎么用?PHP ClassMetadata::addGetterConstraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::addGetterConstraint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testLoadClassMetadata
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);
}
示例2: loadValidatorMetadata
/**
* 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]));
}
示例3: loadClassMetadata
/**
* {@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;
}
示例4: loadClassMetadata
/**
* {@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;
}
示例5: loadClassMetadata
/**
* {@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;
}
示例6: loadClassMetadata
/**
* {@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;
}
示例7: loadClassMetadata
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;
}
示例8: testGetterConstraint
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());
}
示例9: loadClassMetadata
/**
* {@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;
}
示例10: testValidateInOtherGroupTraversesNoGroupSequence
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());
}
示例11: testLoadClassMetadata
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);
}
示例12: loadClassMetadata
/**
* {@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;
}
示例13: testLoadClassMetadata
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);
}
示例14: loadClassMetadata
/**
* {@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;
}
示例15: loadClassMetadata
/**
* {@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;
}