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\RuleHelper; |
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 ValueValidator extends ConstraintValidator |
14
|
|
|
{ |
15
|
|
|
protected $ruleHelper; |
16
|
|
|
|
17
|
|
|
public function __construct(RuleHelper $ruleHelper) |
18
|
|
|
{ |
19
|
|
|
$this->ruleHelper = $ruleHelper; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function validate($value, Constraint $constraint) |
23
|
|
|
{ |
24
|
|
|
if (!$constraint instanceof Value) { |
25
|
|
|
throw new UnexpectedTypeException($constraint, Value::class); |
26
|
|
|
} |
27
|
|
|
if (!$value instanceof Team && !$value instanceof TeamVersion) { |
28
|
|
|
throw new UnexpectedTypeException($value, Team::class); |
29
|
|
|
} |
30
|
|
|
if ($value instanceof Team) { |
31
|
|
|
$value = TeamHelper::getLastVersion($value); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$helper = $this->ruleHelper->getHelper($value->getTeam()); |
35
|
|
|
$teamCost = $helper->calculateTeamValue($value); |
36
|
|
|
$limit = $helper->getMaxTeamCost(); |
37
|
|
|
|
38
|
|
|
if ($teamCost > $limit) { |
39
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
40
|
|
|
->setParameter('{{ limit }}', $limit) |
41
|
|
|
->setParameter('{{ current }}', $teamCost) |
42
|
|
|
->addViolation(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|