-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFilePathSplitValidator.php
More file actions
42 lines (35 loc) · 1.52 KB
/
FilePathSplitValidator.php
File metadata and controls
42 lines (35 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class FilePathSplitValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof MultiplePath) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\MultiplePath');
}
// > $minCount
if( $constraint->minCount && ($constraint->minCount > $this->getCountOfValue($value)) ) {
$this->context->buildViolation($constraint->minCountMessage)
->setParameter('{{ minCount }}', $this->formatValue($constraint->minCount))
->addViolation();
}
// < $maxCount
if( $constraint->maxCount && ($constraint->maxCount < $this->getCountOfValue($value)) ) {
$this->context->buildViolation($constraint->maxCountMessage)
->setParameter('{{ maxCount }}', $this->formatValue($constraint->maxCount))
->addViolation();
}
// === $exactCount
if( $constraint->exactCount && ($constraint->exactCount !== $this->getCountOfValue($value)) ) {
$this->context->buildViolation($constraint->exactCountMessage)
->setParameter('{{ exactCount }}', $this->formatValue($constraint->exactCount))
->addViolation();
}
}
private function getCountOfValue($value)
{
return is_null($value) ? 0 : count($value);
}
}