mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
person: add custom class validator for accompanying period
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user