本文整理汇总了PHP中Symfony\Component\Validator\Mapping\ClassMetadata类的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata类的具体用法?PHP ClassMetadata怎么用?PHP ClassMetadata使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClassMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('title', new NotBlank());
$metadata->addPropertyConstraint('coment', new NotBlank());
$metadata->addPropertyConstraint('author', new NotBlank());
$metadata->addPropertyConstraint('photo', new NotBlank());
}
示例2: 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;
}
示例3: 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());
}
示例4: loadValidatorMetadata
/**
* Load validator metadata
*
* @param Symfony\Component\Validator\Mapping\ClassMetadata $metadata
*/
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('created', new Assert\NotBlank());
$metadata->addPropertyConstraint('title', new Assert\NotBlank());
$metadata->addPropertyConstraint('title', new Assert\Length(array('min' => 5)));
$metadata->addPropertyConstraint('body', new Assert\NotBlank());
}
示例5: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('teamA', new Assert\Valid());
$metadata->addPropertyConstraint('teamB', new Assert\Valid());
$metadata->addPropertyConstraint('setScores', new Assert\Valid());
$metadata->addPropertyConstraint('gameTimeLengthInSeconds', new Assert\Range(['min' => 0, 'max' => 65535]));
}
示例6: loadValidatorMetadata
/**
* @param ClassMetadata $metadata
*/
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('title', new NotBlank(array('message' => 'You must enter a title')));
$metadata->addPropertyConstraint('blog', new NotBlank(array('message' => 'You must enter little bit blog?')));
$metadata->addPropertyConstraint('author', new NotBlank(array('message' => 'You must enter your name!')));
$metadata->addPropertyConstraint('tags', new NotBlank(array('message' => 'You must enter minimal one tag')));
}
示例7: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('email', new Email());
$metadata->addPropertyConstraint('short_name', new NotBlank());
$metadata->addPropertyConstraint('address', new NotBlank());
}
示例8: 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->setGroupSequence(array('Foo', '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);
}
示例9: 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]));
}
示例10: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('name', new Length(['min' => 5, 'max' => 10, 'minMessage' => 'Your name must be at least {{ limit }} characters long', 'maxMessage' => 'Your name cannot be longer than {{ limit }} characters']));
$metadata->addPropertyConstraint('password', new NotBlank());
$metadata->addPropertyConstraint('password', new Length(['min' => 5, 'max' => 10, 'minMessage' => 'Your password must be at least {{ limit }} characters long', 'maxMessage' => 'Your password cannot be longer than {{ limit }} characters']));
}
示例11: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('email', new NotBlank());
$metadata->addPropertyConstraint('email', new Email(array('message' => 'Please provide a valid email address.')));
$metadata->addPropertyConstraint('subject', new NotBlank());
}
示例12: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new Constraints\NotBlank());
$metadata->addPropertyConstraint('name', new Constraints\MinLength(2));
$metadata->addPropertyConstraint('username', new Constraints\NotBlank());
$metadata->addPropertyConstraint('username', new Constraints\MinLength(2));
}
示例13: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('login', new NotBlank());
$metadata->addPropertyConstraint('login', new Length(array('min' => 6, 'max' => 40)));
$metadata->addPropertyConstraint('email', new Length(array('min' => 2, 'max' => 100)));
$metadata->addPropertyConstraint('email', new Email());
}
示例14: loadValidatorMetadata
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('title', new NotBlank(array('message' => 'You must enter a title')));
$metadata->addPropertyConstraint('author', new NotBlank(array('message' => 'You must enter a author')));
$metadata->addPropertyConstraint('blog', new NotBlank(array('message' => 'You must enter the text')));
$metadata->addPropertyConstraint('blog', new Length(array('min' => 50)));
}
示例15: 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;
}