addGetterMethodConstraint example

$violations = $this->validator->validate($entity, null, ['one', 'two']);

        $this->assertCount(2, $violations);
        $this->assertInstanceOf(Length::class$violations->get(0)->getConstraint());
        $this->assertInstanceOf(NotBlank::class$violations->get(1)->getConstraint());
    }

    public function testGroupedMethodConstraintValidateInSequence()
    {
        $metadata = new ClassMetadata(EntityWithGroupedConstraintOnMethods::class);
        $metadata->addPropertyConstraint('bar', new NotNull(['groups' => 'Foo']));
        $metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(['groups' => 'Foo']));
        $metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(['groups' => 'Bar']));

        $this->metadataFactory->addMetadata($metadata);

        $entity = new EntityWithGroupedConstraintOnMethods();
        $groups = new GroupSequence(['EntityWithGroupedConstraintOnMethods', 'Foo', 'Bar']);

        $violations = $this->validator->validate($entity, null, $groups);

        $this->assertCount(2, $violations);
        $this->assertInstanceOf(NotNull::class$violations->get(0)->getConstraint());
        
return $this;
    }

    /** * @param Constraint[] $constraints * * @return $this */
    public function addGetterMethodConstraints(string $property, string $method, array $constraints)static
    {
        foreach ($constraints as $constraint) {
            $this->addGetterMethodConstraint($property$method$constraint);
        }

        return $this;
    }

    /** * Merges the constraints of the given metadata into this object. * * @return void */
    public function mergeConstraints(self $source)
    {
return $this;
    }

    /** * @param Constraint[] $constraints * * @return $this */
    public function addGetterMethodConstraints(string $property, string $method, array $constraints)static
    {
        foreach ($constraints as $constraint) {
            $this->addGetterMethodConstraint($property$method$constraint);
        }

        return $this;
    }

    /** * Merges the constraints of the given metadata into this object. * * @return void */
    public function mergeConstraints(self $source)
    {


        foreach ($reflClass->getMethods() as $method) {
            if ($method->getDeclaringClass()->name === $className) {
                foreach ($this->getAnnotations($method) as $constraint) {
                    if ($constraint instanceof Callback) {
                        $constraint->callback = $method->getName();

                        $metadata->addConstraint($constraint);
                    } elseif ($constraint instanceof Constraint) {
                        if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
                            $metadata->addGetterMethodConstraint(lcfirst($matches[2])$matches[0]$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;
    }


        foreach ($reflClass->getMethods() as $method) {
            if ($method->getDeclaringClass()->name === $className) {
                foreach ($this->getAnnotations($method) as $constraint) {
                    if ($constraint instanceof Callback) {
                        $constraint->callback = $method->getName();

                        $metadata->addConstraint($constraint);
                    } elseif ($constraint instanceof Constraint) {
                        if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
                            $metadata->addGetterMethodConstraint(lcfirst($matches[2])$matches[0]$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;
    }
$expected->addPropertyConstraint('firstName', new AtLeastOneOf([
            new NotNull(),
            new Range(['min' => 3]),
        ], null, null, 'foo', null, false));
        $expected->addPropertyConstraint('firstName', new Sequentially([
            new NotBlank(),
            new Range(['min' => 5]),
        ]));
        $expected->addPropertyConstraint('childA', new Valid());
        $expected->addPropertyConstraint('childB', new Valid());
        $expected->addGetterConstraint('lastName', new NotNull());
        $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
        $expected->addGetterConstraint('permissions', new IsTrue());
        $expected->addPropertyConstraint('other', new Type('integer'));

        // load reflection class so that the comparison passes         $expected->getReflectionClass();

        $this->assertEquals($expected$metadata);
    }

    /** * Test MetaData merge with parent annotation. */
Home | Imprint | This part of the site doesn't use cookies.