person: add custom class validator for accompanying period

This commit is contained in:
nobohan
2021-12-03 18:10:52 +01:00
parent 611f0a9ca6
commit 2e4356c3c9
4 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class AccompanyingPeriodValidity extends Constraint
{
public $messageSocialIssueCannotBeDeleted = 'This social issue cannot be deleted because it is associated with an activity or an action';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class AccompanyingPeriodValidityValidator extends ConstraintValidator
{
private ActivityACLAwareRepository $activityRepository;
public function __construct(ActivityACLAwareRepository $activityRepository)
{
$this->activityRepository = $activityRepository;
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof AccompanyingPeriodValidity) {
throw new UnexpectedTypeException($constraint, AccompanyingPeriodValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($period, AccompanyingPeriod::class);
}
dump($period);
$socialIssues = [];
$activities = $this->activityRepository->findByAccompanyingPeriod($period, 'SEE');
dump($activities);
foreach ($activities as $activity) {
$socialIssues[] = $activity->getSocialIssues();
}
foreach ($period->getWorks() as $work) {
$socialIssues[] = $work->getSocialIssues();
}
dump($socialIssues);
foreach ($period->getSocialIssues() as $si) {
dump($si);
if (!in_array($si, $socialIssues)) {
$this->context
->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted
)
->addViolation();
}
}
}
}