本文整理汇总了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_'")));
}
示例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)));
}
示例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 = 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;
}
示例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);
}
示例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());
}
示例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;
}
示例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;
}
示例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)));
}
示例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());
}
示例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;
}
示例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);
}
示例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());
}
示例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);
}
示例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));
}