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