1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Entity\Team; |
6
|
|
|
use Obblm\Core\Entity\TeamVersion; |
7
|
|
|
use Obblm\Core\Helper\PlayerHelper; |
8
|
|
|
use Obblm\Core\Helper\TeamHelper; |
9
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
10
|
|
|
use Symfony\Component\Validator\Constraint; |
11
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
12
|
|
|
|
13
|
|
|
class TeamCompositionValidator extends ConstraintValidator |
14
|
|
|
{ |
15
|
|
|
private $teamHelper; |
16
|
|
|
|
17
|
|
|
public function __construct(TeamHelper $teamHelper) |
18
|
|
|
{ |
19
|
|
|
$this->teamHelper = $teamHelper; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function validate($value, Constraint $constraint) |
23
|
|
|
{ |
24
|
|
|
if (!$constraint instanceof TeamComposition) { |
25
|
|
|
throw new UnexpectedTypeException($constraint, TeamComposition::class); |
26
|
|
|
} |
27
|
|
|
if (!$value instanceof TeamVersion && !$value instanceof Team) { |
28
|
|
|
throw new UnexpectedTypeException($value, Team::class); |
29
|
|
|
} |
30
|
|
|
if ($value instanceof Team) { |
31
|
|
|
$value = TeamHelper::getLastVersion($value); |
32
|
|
|
} |
33
|
|
|
$count = []; |
34
|
|
|
|
35
|
|
|
/** @var TeamVersion $value */ |
36
|
|
|
|
37
|
|
|
$maxPositions = $this->getMaxPlayersByTypes($value->getTeam()); |
|
|
|
|
38
|
|
|
foreach ($value->getNotDeadPlayerVersions() as $version) { |
39
|
|
|
if ($version->getPlayer()->getType()) { |
40
|
|
|
$limit = $maxPositions[$version->getPlayer()->getType()]; |
41
|
|
|
$type = $version->getPlayer()->getType(); |
42
|
|
|
isset($count[$type]) ? $count[$type]++ : $count[$type] = 1; |
43
|
|
|
if ($count[$type] > $limit) { |
44
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
45
|
|
|
->setParameter('{{ limit }}', $limit) |
46
|
|
|
->setParameter('{{ player_type }}', $type) |
47
|
|
|
->addViolation(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getMaxPlayersByTypes(Team $team):array |
54
|
|
|
{ |
55
|
|
|
$helper = $this->teamHelper->getRuleHelper($team); |
56
|
|
|
$maxPositions = []; |
57
|
|
|
|
58
|
|
|
if ($helper->getAvailablePlayerTypes($team->getRoster())) { |
59
|
|
|
$types = $helper->getAvailablePlayerTypes($team->getRoster()); |
60
|
|
|
foreach ($types as $key => $type) { |
61
|
|
|
$key = PlayerHelper::composePlayerKey($helper->getAttachedRule()->getRuleKey(), $team->getRoster(), $key); |
62
|
|
|
$maxPositions[$key] = $type['max']; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $maxPositions; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|