mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
entity person: create a validator to check a person entity is linked
with a center This validator take a parameter in configuration
This commit is contained in:
parent
03e8624528
commit
eec798cfd3
@ -60,6 +60,9 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
|
||||
$container->setParameter('chill_person.allow_multiple_simultaneous_accompanying_periods',
|
||||
$config['allow_multiple_simultaneous_accompanying_periods']);
|
||||
|
||||
// register all configuration in a unique parameter
|
||||
$container->setParameter('chill_person', $config);
|
||||
|
||||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
|
||||
$loader->load('services.yaml');
|
||||
$loader->load('services/widgets.yaml');
|
||||
|
@ -44,22 +44,25 @@ class Configuration implements ConfigurationInterface
|
||||
->canBeDisabled()
|
||||
->children()
|
||||
->scalarNode('birthdate_not_after')
|
||||
->info($this->validationBirthdateNotAfterInfos)
|
||||
->defaultValue('P1D')
|
||||
->validate()
|
||||
->ifTrue(function($period) {
|
||||
try {
|
||||
$interval = new \DateInterval($period);
|
||||
} catch (\Exception $ex) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
->thenInvalid('Invalid period for birthdate validation : "%s" '
|
||||
. 'The parameter should match duration as defined by ISO8601 : '
|
||||
. 'https://en.wikipedia.org/wiki/ISO_8601#Durations')
|
||||
->info($this->validationBirthdateNotAfterInfos)
|
||||
->defaultValue('P1D')
|
||||
->validate()
|
||||
->ifTrue(function($period) {
|
||||
try {
|
||||
$interval = new \DateInterval($period);
|
||||
} catch (\Exception $ex) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
->thenInvalid('Invalid period for birthdate validation : "%s" '
|
||||
. 'The parameter should match duration as defined by ISO8601 : '
|
||||
. 'https://en.wikipedia.org/wiki/ISO_8601#Durations')
|
||||
->end() // birthdate_not_after, parent = children of validation
|
||||
|
||||
->booleanNode('center_required')
|
||||
->info('Enable a center for each person entity. If disabled, you must provide your own center provider')
|
||||
->defaultValue(true)
|
||||
->end()
|
||||
->end() // children for 'validation', parent = validation
|
||||
->end() //validation, parent = children of root
|
||||
->end() // children of root, parent = root
|
||||
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Validator\Person;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Validator\Constraints\Person\PersonHasCenter;
|
||||
use Chill\PersonBundle\Validator\Constraints\Person\PersonHasCenterValidator;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
|
||||
class PersonHasCenterValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
{
|
||||
public function testValidateRequired()
|
||||
{
|
||||
$constraint = $this->getConstraint();
|
||||
$personHasCenter = (new Person())->setCenter(new Center());
|
||||
$personNoCenter = new Person();
|
||||
|
||||
$this->validator->validate($personHasCenter, $constraint);
|
||||
$this->assertNoViolation();
|
||||
|
||||
$this->validator->validate($personNoCenter, $constraint);
|
||||
$this->buildViolation('msg')
|
||||
->atPath('property.path.center')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
protected function getConstraint()
|
||||
{
|
||||
return new PersonHasCenter([
|
||||
'message' => 'msg'
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createValidator()
|
||||
{
|
||||
$parameterBag = $this->createMock(ParameterBagInterface::class);
|
||||
$parameterBag
|
||||
->method('get')
|
||||
->with($this->equalTo('chill_person'))
|
||||
->willReturn([
|
||||
'validation' => [
|
||||
'center_required' => true
|
||||
]
|
||||
])
|
||||
;
|
||||
|
||||
return new PersonHasCenterValidator($parameterBag);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Person;
|
||||
|
||||
class PersonHasCenter extends \Symfony\Component\Validator\Constraint
|
||||
{
|
||||
public string $message = "A center is required";
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return [
|
||||
self::CLASS_CONSTRAINT
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Person;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class PersonHasCenterValidator extends \Symfony\Component\Validator\ConstraintValidator
|
||||
{
|
||||
private bool $centerRequired;
|
||||
|
||||
public function __construct(ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$this->centerRequired = $parameterBag->get('chill_person')['validation']['center_required'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validate($person, Constraint $constraint)
|
||||
{
|
||||
if (!$person instanceof Person) {
|
||||
throw new UnexpectedTypeException($constraint, Person::class);
|
||||
}
|
||||
|
||||
if (!$this->centerRequired) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (NULL === $person->getCenter()) {
|
||||
$this
|
||||
->context
|
||||
->buildViolation($constraint->message)
|
||||
->atPath('center')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user