1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Validator\Constraints\Team; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Entity\Team; |
6
|
|
|
use Obblm\Core\Entity\TeamVersion; |
7
|
|
|
use Obblm\Core\Helper\CoreTranslation; |
8
|
|
|
use Obblm\Core\Helper\RuleHelper; |
9
|
|
|
use Obblm\Core\Helper\TeamHelper; |
10
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
11
|
|
|
use Symfony\Component\Validator\Constraint; |
12
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
13
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
14
|
|
|
|
15
|
|
|
class CompositionValidator extends ConstraintValidator |
16
|
|
|
{ |
17
|
|
|
private $ruleHelper; |
18
|
|
|
private $translator; |
19
|
|
|
|
20
|
|
|
public function __construct(RuleHelper $ruleHelper, TranslatorInterface $translator) |
21
|
|
|
{ |
22
|
|
|
$this->ruleHelper = $ruleHelper; |
23
|
|
|
$this->translator = $translator; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function validate($value, Constraint $constraint) |
27
|
|
|
{ |
28
|
|
|
if (!$constraint instanceof Composition) { |
29
|
|
|
throw new UnexpectedTypeException($constraint, Composition::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($value instanceof TeamVersion) { |
33
|
|
|
$value->getNotDeadPlayerVersions(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!$value instanceof TeamVersion) { |
37
|
|
|
throw new UnexpectedTypeException($value, TeamVersion::class); |
38
|
|
|
} |
39
|
|
|
if ($value instanceof Team) { |
|
|
|
|
40
|
|
|
$value = TeamHelper::getLastVersion($value); |
41
|
|
|
} |
42
|
|
|
$count = []; |
43
|
|
|
|
44
|
|
|
/** @var TeamVersion $value */ |
45
|
|
|
$team = $value->getTeam(); |
46
|
|
|
$helper = $this->ruleHelper->getHelper($team); |
47
|
|
|
foreach ($value->getNotDeadPlayerVersions() as $version) { |
48
|
|
|
if ($version->getPlayer()->getPosition()) { |
49
|
|
|
$position = $version->getPlayer()->getPosition(); |
50
|
|
|
$basePosition = $helper->getRoster($team)->getPosition($position); |
|
|
|
|
51
|
|
|
isset($count[$position]) ? $count[$position]++ : $count[$position] = 1; |
52
|
|
|
if ($count[$position] > $basePosition->getMax()) { |
53
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
54
|
|
|
->setParameter('{{ limit }}', $basePosition->getMax()) |
55
|
|
|
->setParameter('{{ player_type }}', $this->translator->trans($basePosition->getName(), [], $basePosition->getTranslationDomain())) |
56
|
|
|
->addViolation(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|