1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Validator\Constraints\Team; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Entity\TeamVersion; |
6
|
|
|
use Obblm\Core\Helper\RuleHelper; |
7
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
8
|
|
|
use Symfony\Component\Validator\Constraint; |
9
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
10
|
|
|
|
11
|
|
|
class SkillsQuantityValidator extends ConstraintValidator |
12
|
|
|
{ |
13
|
|
|
private $ruleHelper; |
14
|
|
|
private $helper = null; |
15
|
|
|
private $total = 0; |
16
|
|
|
private $max = ['total' => 0]; |
17
|
|
|
private $count = ['total' => 0]; |
18
|
|
|
|
19
|
|
|
public function __construct(RuleHelper $ruleHelper) |
20
|
|
|
{ |
21
|
|
|
$this->ruleHelper = $ruleHelper; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function validate($value, Constraint $constraint) |
25
|
|
|
{ |
26
|
|
|
if (!$constraint instanceof SkillsQuantity) { |
27
|
|
|
throw new UnexpectedTypeException($constraint, SkillsQuantity::class); |
28
|
|
|
} |
29
|
|
|
if (!$value instanceof TeamVersion) { |
30
|
|
|
throw new UnexpectedTypeException($value, TeamVersion::class); |
31
|
|
|
} |
32
|
|
|
$this->max['total'] = $value->getTeam()->getCreationOption("skills_allowed.total") ?: 0; |
33
|
|
|
$this->initializeMaxValues($value, $constraint->context); |
34
|
|
|
|
35
|
|
|
$this->helper = $this->ruleHelper->getHelper($value->getTeam()); |
36
|
|
|
foreach ($value->getNotDeadPlayerVersions() as $playerVersion) { |
37
|
|
|
if (count($playerVersion->getAdditionalSkills()) > $value->getTeam()->getCreationOption("skills_allowed.max_skills_per_player")) { |
38
|
|
|
$this->context->buildViolation($constraint->limitByPlayerMessage) |
39
|
|
|
->setParameter('{{ limit }}', $value->getTeam()->getCreationOption("skills_allowed.max_skills_per_player")) |
|
|
|
|
40
|
|
|
->addViolation(); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
foreach ($playerVersion->getAdditionalSkills() as $skillKey) { |
44
|
|
|
$skill = $this->helper->getSkill($skillKey); |
45
|
|
|
$context = $this->helper->getSkillContextForPlayerVersion($playerVersion, $skill); |
46
|
|
|
$this->count['total']++; |
47
|
|
|
if ($this->count['total'] > $this->max['total']) { |
48
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
49
|
|
|
->setParameter('{{ limit }}', $this->max['total']) |
50
|
|
|
->addViolation(); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
if (isset($this->max[$context])) { |
54
|
|
|
$this->count[$context]++; |
55
|
|
|
$limit = $this->max[$context] ?: 0; |
56
|
|
|
if ($this->count[$context] > $limit) { |
57
|
|
|
$this->context->buildViolation($constraint->limitByTypeMessage) |
58
|
|
|
->setParameter('{{ type }}', $context) |
59
|
|
|
->setParameter('{{ limit }}', $limit) |
60
|
|
|
->addViolation(); |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function initializeMaxValues(TeamVersion $value, array $context) |
69
|
|
|
{ |
70
|
|
|
$this->total = 0; |
71
|
|
|
foreach ($context as $key) { |
72
|
|
|
if (is_string($key)) { |
73
|
|
|
$this->max[$key] = $value->getTeam()->getCreationOption("skills_allowed.$key") ?: 0; |
74
|
|
|
$this->count[$key] = 0; |
75
|
|
|
$this->total += $value->getTeam()->getCreationOption("skills_allowed.$key") ?: 0; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|