| Conditions | 9 |
| Paths | 12 |
| Total Lines | 37 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | public function validate($value, Constraint $constraint) |
||
| 20 | { |
||
| 21 | if (!$constraint instanceof Age) { |
||
| 22 | throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Age'); |
||
| 23 | } |
||
| 24 | if (!$value instanceof \DateTime || !$constraint instanceof Age) { |
||
| 25 | throw new UnexpectedTypeException($value, '\DateTime'); |
||
| 26 | } |
||
| 27 | |||
| 28 | $now = new \DateTime(); |
||
| 29 | $minAgeDate = new \DateTime("-{$constraint->min} years"); |
||
| 30 | $maxAgeDate = new \DateTime("-{$constraint->max} years"); |
||
| 31 | |||
| 32 | $error = false; |
||
| 33 | $limit = 0; |
||
| 34 | |||
| 35 | if ($value > $now) { |
||
| 36 | $error = $constraint->futureError; |
||
| 37 | } else { |
||
| 38 | if ($constraint->min > 0 && $value > $minAgeDate) { |
||
| 39 | $error = $constraint->minError; |
||
| 40 | $limit = $constraint->min; |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($value < $maxAgeDate) { |
||
| 44 | $error = $constraint->maxError; |
||
| 45 | $limit = $constraint->max; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($error) { |
||
|
|
|||
| 50 | $this->context |
||
| 51 | ->buildViolation($error) |
||
| 52 | ->setParameter('{{ limit }}', $limit) |
||
| 53 | ->addViolation(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: