当前位置: 首页>>代码示例>>PHP>>正文


PHP ClassMetadata::addConstraint方法代码示例

本文整理汇总了PHP中Symfony\Component\Validator\Mapping\ClassMetadata::addConstraint方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::addConstraint方法的具体用法?PHP ClassMetadata::addConstraint怎么用?PHP ClassMetadata::addConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Symfony\Component\Validator\Mapping\ClassMetadata的用法示例。


在下文中一共展示了ClassMetadata::addConstraint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: loadValidatorMetadata

 static function loadValidatorMetadata(ClassMetadata $metadatas)
 {
     $metadatas->addConstraint(new Unique(array('fields' => 'name')));
     $metadatas->addConstraint(new Unique(array('fields' => 'role')));
     $metadatas->addPropertyConstraint("name", new Length(array('min' => 4, "max" => 100)));
     $metadatas->addPropertyConstraint("role", new Length(array('min' => 6, "max" => 100)));
     $metadatas->addPropertyConstraint("role", new Regex(array('pattern' => "/^ROLE\\_/", "message" => "The value must begin by 'ROLE_'")));
 }
开发者ID:mparaiso,项目名称:doctrineodmserviceprovider,代码行数:8,代码来源:Role.php

示例2: loadValidatorMetadata

 /** validate user entity */
 static function loadValidatorMetadata(ClassMetadata $metadata)
 {
     $metadata->addConstraint(new UniqueEntity(array("username")));
     $metadata->addConstraint(new UniqueEntity(array("email")));
     $metadata->addPropertyConstraint("username", new NotNull());
     $metadata->addPropertyConstraint("username", new Length(array("min" => 5, "max" => 100)));
     $metadata->addPropertyConstraint("email", new NotNull());
     $metadata->addPropertyConstraint("email", new Length(array("min" => 5, "max" => 100)));
     $metadata->addPropertyConstraint("password", new NotNull());
     $metadata->addPropertyConstraint("password", new Length(array("min" => 5, "max" => 100)));
 }
开发者ID:mparaiso,项目名称:simpleuserserviceprovider,代码行数:12,代码来源:User.php

示例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;
 }
开发者ID:notbrain,项目名称:symfony,代码行数:52,代码来源:AnnotationLoader.php

示例4: 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;
 }
开发者ID:janmarek,项目名称:Neuron,代码行数:34,代码来源:YamlFileLoader.php

示例5: testInitializeObjectsOnFirstValidation

 public function testInitializeObjectsOnFirstValidation()
 {
     $test = $this;
     $entity = new Entity();
     $entity->initialized = false;
     // prepare initializers that set "initialized" to true
     $initializer1 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
     $initializer2 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
     $initializer1->expects($this->once())->method('initialize')->with($entity)->will($this->returnCallback(function ($object) {
         $object->initialized = true;
     }));
     $initializer2->expects($this->once())->method('initialize')->with($entity);
     $this->visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator(), null, array($initializer1, $initializer2));
     // prepare constraint which
     // * checks that "initialized" is set to true
     // * validates the object again
     $callback = function ($object, ExecutionContextInterface $context) use($test) {
         $test->assertTrue($object->initialized);
         // validate again in same group
         $context->validate($object);
         // validate again in other group
         $context->validate($object, '', 'SomeGroup');
     };
     $this->metadata->addConstraint(new Callback(array($callback)));
     $this->visitor->validate($entity, 'Default', '');
     $this->assertTrue($entity->initialized);
 }
开发者ID:rolfmadsen,项目名称:dummy_alma,代码行数:27,代码来源:ValidationVisitorTest.php

示例6: testValidateUniqueness

 /**
  * This is a functinoal test as there is a large integration necessary to get the validator working.
  */
 public function testValidateUniqueness()
 {
     $entityManagerName = "foo";
     $em = $this->createTestEntityManager();
     $schemaTool = new SchemaTool($em);
     $schemaTool->createSchema(array($em->getClassMetadata('Symfony\\Tests\\Bridge\\Doctrine\\Form\\Fixtures\\SingleIdentEntity')));
     $entity1 = new SingleIdentEntity(1, 'Foo');
     $registry = $this->createRegistryMock($entityManagerName, $em);
     $uniqueValidator = new UniqueEntityValidator($registry);
     $metadata = new ClassMetadata('Symfony\\Tests\\Bridge\\Doctrine\\Form\\Fixtures\\SingleIdentEntity');
     $metadata->addConstraint(new UniqueEntity(array('fields' => array('name'), 'em' => $entityManagerName)));
     $metadataFactory = $this->createMetadataFactoryMock($metadata);
     $validatorFactory = $this->createValidatorFactory($uniqueValidator);
     $validator = new Validator($metadataFactory, $validatorFactory);
     $violationsList = $validator->validate($entity1);
     $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database.");
     $em->persist($entity1);
     $em->flush();
     $violationsList = $validator->validate($entity1);
     $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database.");
     $entity2 = new SingleIdentEntity(2, 'Foo');
     $violationsList = $validator->validate($entity2);
     $this->assertEquals(1, $violationsList->count(), "No violations found on entity after it was saved to the database.");
     $violation = $violationsList[0];
     $this->assertEquals('This value is already used.', $violation->getMessage());
     $this->assertEquals('name', $violation->getPropertyPath());
     $this->assertEquals('Foo', $violation->getInvalidValue());
 }
开发者ID:RogerWebb,项目名称:symfony,代码行数:31,代码来源:UniqueValidatorTest.php

示例7: 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;
 }
开发者ID:faridos,项目名称:ServerGroveLiveChat,代码行数:34,代码来源:XmlFileLoader.php

示例8: 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;
 }
开发者ID:skoop,项目名称:symfony-sandbox,代码行数:34,代码来源:AnnotationLoader.php

示例9: loadValidatorMetadata

 public static function loadValidatorMetadata(ClassMetadata $metadata)
 {
     $metadata->addConstraint(new Callback('validate'));
     $metadata->addPropertyConstraint('password', new NotBlank());
     $metadata->addPropertyConstraint('password', new Length(array('min' => 8, 'max' => 40)));
     $metadata->addPropertyConstraint('repeatPassword', new NotBlank());
     $metadata->addPropertyConstraint('repeatPassword', new Length(array('min' => 8, 'max' => 40)));
 }
开发者ID:zyxist,项目名称:cantiga,代码行数:8,代码来源:PasswordRecoveryCompleteIntent.php

示例10: loadValidatorMetadata

 public static function loadValidatorMetadata(ClassMetadata $metadata)
 {
     $metadata->addConstraint(new Callback('checkPassword'));
     $metadata->addPropertyConstraint('password', new NotBlank());
     $metadata->addPropertyConstraint('password', new Length(array('min' => 8, 'max' => 40)));
     $metadata->addPropertyConstraint('email', new Length(array('min' => 2, 'max' => 100)));
     $metadata->addPropertyConstraint('email', new Email());
 }
开发者ID:zyxist,项目名称:cantiga,代码行数:8,代码来源:EmailChangeIntent.php

示例11: 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;
 }
开发者ID:tahermarkos,项目名称:Transport,代码行数:48,代码来源:AnnotationLoader.php

示例12: testLoadClassMetadataAndMerge

 /**
  * Test MetaData merge with parent annotation.
  */
 public function testLoadClassMetadataAndMerge()
 {
     $loader = new AnnotationLoader();
     // Load Parent MetaData
     $parent_metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent');
     $loader->loadClassMetadata($parent_metadata);
     $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
     // Merge parent metaData.
     $metadata->mergeConstraints($parent_metadata);
     $loader->loadClassMetadata($metadata);
     $expected_parent = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent');
     $expected_parent->addPropertyConstraint('other', new NotNull());
     $expected_parent->getReflectionClass();
     $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
     $expected->mergeConstraints($expected_parent);
     $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);
 }
开发者ID:rsky,项目名称:symfony,代码行数:31,代码来源:AnnotationLoaderTest.php

示例13: testMergeConstraintsMergesClassConstraints

 public function testMergeConstraintsMergesClassConstraints()
 {
     $parent = new ClassMetadata(self::PARENTCLASS);
     $parent->addConstraint(new ConstraintA());
     $this->metadata->mergeConstraints($parent);
     $this->metadata->addConstraint(new ConstraintA());
     $constraints = array(new ConstraintA(array('groups' => array('Default', 'EntityParent', 'Entity'))), new ConstraintA(array('groups' => array('Default', 'Entity'))));
     $this->assertEquals($constraints, $this->metadata->getConstraints());
 }
开发者ID:notbrain,项目名称:symfony,代码行数:9,代码来源:ClassMetadataTest.php

示例14: testLoadClassMetadata

 public function testLoadClassMetadata()
 {
     $loader = new XmlFileLoader(__DIR__ . '/constraint-mapping.xml');
     $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
     $loader->loadClassMetadata($metadata);
     $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
     $expected->addConstraint(new ConstraintA());
     $expected->addConstraint(new ConstraintB());
     $expected->addPropertyConstraint('firstName', new NotNull());
     $expected->addPropertyConstraint('firstName', new Min(3));
     $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
     $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
     $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
     $expected->addPropertyConstraint('firstName', 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);
 }
开发者ID:nicodmf,项目名称:symfony,代码行数:18,代码来源:XmlFileLoaderTest.php

示例15: loadValidatorMetadata

 public static function loadValidatorMetadata(ClassMetadata $metadata)
 {
     $callbackConstraintOptions = array('groups' => 'flow_revalidatePreviousSteps_step1');
     if (Kernel::VERSION_ID < 20400) {
         $callbackConstraintOptions['methods'] = array('isDataValid');
     } else {
         $callbackConstraintOptions['callback'] = 'isDataValid';
     }
     $metadata->addConstraint(new Assert\Callback($callbackConstraintOptions));
 }
开发者ID:raizeta,项目名称:CraueFormFlowBundle,代码行数:10,代码来源:RevalidatePreviousStepsData.php


注:本文中的Symfony\Component\Validator\Mapping\ClassMetadata::addConstraint方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。