. */ namespace App\Validator\Constraints\ProjectSystem; use App\Entity\Parts\PartLot; use App\Helpers\Projects\ProjectBuildRequest; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; class ValidProjectBuildRequestValidator extends ConstraintValidator { private function buildViolationForLot(PartLot $partLot, string $message): ConstraintViolationBuilderInterface { return $this->context->buildViolation($message) ->atPath('lot_' . $partLot->getID()) ->setParameter('{{ lot }}', $partLot->getName()); } public function validate($value, Constraint $constraint) { if (!$constraint instanceof ValidProjectBuildRequest) { throw new UnexpectedTypeException($constraint, ValidProjectBuildRequest::class); } if (null === $value || '' === $value) { return; } if (!$value instanceof ProjectBuildRequest) { throw new UnexpectedTypeException($value, ProjectBuildRequest::class); } foreach ($value->getPartBomEntries() as $bom_entry) { $withdraw_sum = $value->getWithdrawAmountSum($bom_entry); $needed_amount = $value->getNeededAmountForBOMEntry($bom_entry); foreach ($value->getPartLotsForBOMEntry($bom_entry) as $lot) { $withdraw_amount = $value->getLotWithdrawAmount($lot); if ($withdraw_amount < 0) { $this->buildViolationForLot($lot, 'validator.project_build.lot_must_not_smaller_0') ->addViolation(); } if ($withdraw_amount > $lot->getAmount()) { $this->buildViolationForLot($lot, 'validator.project_build.lot_must_not_bigger_than_stock') ->addViolation(); } if ($withdraw_sum > $needed_amount) { $this->buildViolationForLot($lot, 'validator.project_build.lot_bigger_than_needed') ->addViolation(); } if ($withdraw_sum < $needed_amount) { $this->buildViolationForLot($lot, 'validator.project_build.lot_smaller_than_needed') ->addViolation(); } } } } }