|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oc\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Oc\GeoCache\Enum\WaypointType; |
|
6
|
|
|
use Oc\GeoCache\Exception\UnknownWaypointTypeException; |
|
7
|
|
|
use Symfony\Component\Validator\Constraint; |
|
8
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class WaypointValidator |
|
12
|
|
|
* |
|
13
|
|
|
* @package Oc\Validator\Constraints |
|
|
|
|
|
|
14
|
|
|
*/ |
|
15
|
|
|
class WaypointValidator extends ConstraintValidator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Checks if the passed value is valid. |
|
19
|
|
|
* |
|
20
|
|
|
* @param mixed $waypoint The value that should be validated |
|
|
|
|
|
|
21
|
|
|
* @param Constraint $constraint The constraint for the validation |
|
|
|
|
|
|
22
|
|
|
* |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
|
|
public function validate($waypoint, Constraint $constraint) |
|
26
|
|
|
{ |
|
27
|
|
|
$valid = true; |
|
28
|
|
|
|
|
29
|
|
|
try { |
|
30
|
|
|
WaypointType::guess($waypoint); |
|
31
|
|
|
} catch (UnknownWaypointTypeException $e) { |
|
32
|
|
|
$this->context->buildViolation($constraint->messageInvalid) |
|
33
|
|
|
->setParameter('%waypoint%', $waypoint) |
|
34
|
|
|
->setInvalidValue($waypoint) |
|
35
|
|
|
->addViolation(); |
|
36
|
|
|
$valid = false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $valid; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check if the given waypoint is an oc waypoint. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $waypoint |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool True if it is an oc waypoint |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function isOCWaypoint($waypoint) |
|
50
|
|
|
{ |
|
51
|
|
|
return stripos($waypoint, 'oc') === 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check if the given waypoint is an gc waypoint. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $waypoint |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool True if it is an gc waypoint |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function isGCWaypoint($waypoint) |
|
62
|
|
|
{ |
|
63
|
|
|
return stripos($waypoint, 'gc') === 0; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|