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 GroupOfPositionsValidator 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 GroupOfPositions) { |
29
|
|
|
throw new UnexpectedTypeException($constraint, GroupOfPositions::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
|
|
|
|
46
|
|
|
$maxPositions = $this->getMaxPlayersByTypes($value->getTeam()); |
|
|
|
|
47
|
|
|
foreach ($value->getNotDeadPlayerVersions() as $version) { |
48
|
|
|
if ($version->getPlayer()->getPosition()) { |
49
|
|
|
$playerType = $version->getPlayer()->getPosition(); |
50
|
|
|
list($ruleKey, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $playerType); |
51
|
|
|
$limit = $maxPositions[$type]; |
52
|
|
|
isset($count[$type]) ? $count[$type]++ : $count[$type] = 1; |
53
|
|
|
if ($count[$type] > $limit) { |
54
|
|
|
$translationName = CoreTranslation::getPlayerKeyType($ruleKey, $roster, $type); |
55
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
56
|
|
|
->setParameter('{{ limit }}', $limit) |
57
|
|
|
->setParameter('{{ player_type }}', $this->translator->trans($translationName, [], 'lrb6')) |
58
|
|
|
->addViolation(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getMaxPlayersByTypes(Team $team):array |
65
|
|
|
{ |
66
|
|
|
$helper = $this->ruleHelper->getHelper($team); |
67
|
|
|
$maxPositions = []; |
68
|
|
|
|
69
|
|
|
if ($helper->getAvailablePlayerTypes($team->getRoster())) { |
70
|
|
|
$types = $helper->getAvailablePlayerTypes($team->getRoster()); |
71
|
|
|
foreach ($types as $key => $type) { |
72
|
|
|
$maxPositions[$key] = $type['max']; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $maxPositions; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|