|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Obblm\Core\Validator\Constraints\Team; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
7
|
|
|
use Obblm\Core\Helper\Rule\Inducement\Inducement; |
|
8
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
9
|
|
|
use Symfony\Component\Validator\Constraint; |
|
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
11
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class InducementsQuantityValidator extends ConstraintValidator |
|
14
|
|
|
{ |
|
15
|
|
|
private $translator; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(TranslatorInterface $translator) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->translator = $translator; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function validate($value, Constraint $constraint) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$constraint instanceof InducementsQuantity) { |
|
25
|
|
|
throw new UnexpectedTypeException($constraint, InducementsQuantity::class); |
|
26
|
|
|
} |
|
27
|
|
|
if (!is_array($value)) { |
|
28
|
|
|
throw new UnexpectedTypeException($value, 'array'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$count = []; |
|
32
|
|
|
|
|
33
|
|
|
// Validate each inducement |
|
34
|
|
|
foreach ($value as $i => $inducement) { |
|
35
|
|
|
/** @var Inducement $inducement */ |
|
36
|
|
|
if (!isset($count[$inducement->getKey()])) { |
|
37
|
|
|
$count[$inducement->getKey()] = 0; |
|
38
|
|
|
} |
|
39
|
|
|
$count[$inducement->getKey()]++; |
|
40
|
|
|
if ($count[$inducement->getKey()] > $inducement->getMax()) { |
|
41
|
|
|
$type = $this->translator->trans((string) $inducement, [], $inducement->getTranslationDomain()); |
|
42
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
|
43
|
|
|
->setParameter('{{ type }}', $type) |
|
44
|
|
|
->setParameter('{{ limit }}', $inducement->getMax()) |
|
45
|
|
|
->addViolation(); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Validate quantity of star players |
|
50
|
|
|
$value = new ArrayCollection($value); |
|
51
|
|
|
$criteria = (Criteria::create()); |
|
52
|
|
|
$criteria->where(Criteria::expr()->eq('type', 'star_players')); |
|
53
|
|
|
$criteria->orderBy(['value' => 'ASC']); |
|
54
|
|
|
|
|
55
|
|
|
$starPlayers = $value->matching($criteria); |
|
56
|
|
|
|
|
57
|
|
|
if ($starPlayers->count() > $constraint->helper->getMaxStarPlayers()) { |
|
58
|
|
|
$type = $this->translator->trans($starPlayers->getTranslationType(), [], $starPlayers->getTranslationDomain()); |
|
|
|
|
|
|
59
|
|
|
$this->context->buildViolation($constraint->limitMessage) |
|
60
|
|
|
->setParameter('{{ type }}', $type) |
|
61
|
|
|
->setParameter('{{ limit }}', $constraint->helper->getMaxStarPlayers()) |
|
62
|
|
|
->addViolation(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.