Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

WaypointValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 2
A isOCWaypoint() 0 4 1
A isGCWaypoint() 0 4 1
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
class WaypointValidator extends ConstraintValidator
11
{
12
    /**
13
     * Checks if the passed value is valid.
14
     *
15
     * @param mixed $waypoint The value that should be validated
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
16
     * @param Constraint $constraint The constraint for the validation
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
introduced by
Constraint => \Symfony\Component\Validator\Constraint
Loading history...
17
     *
18
     * @return bool
19
     */
20
    public function validate($waypoint, Constraint $constraint)
21
    {
22
        $valid = true;
23
24
        try {
25
            WaypointType::guess($waypoint);
26
        } catch (UnknownWaypointTypeException $e) {
27
            $this->context->buildViolation($constraint->messageInvalid)
28
                ->setParameter('%waypoint%', $waypoint)
29
                ->setInvalidValue($waypoint)
30
                ->addViolation();
31
            $valid = false;
32
        }
33
34
        return $valid;
35
    }
36
37
    /**
38
     * Check if the given waypoint is an oc waypoint.
39
     *
40
     * @param string $waypoint
41
     *
42
     * @return bool True if it is an oc waypoint
43
     */
44
    protected function isOCWaypoint($waypoint)
45
    {
46
        return stripos($waypoint, 'oc') === 0;
47
    }
48
49
    /**
50
     * Check if the given waypoint is an gc waypoint.
51
     *
52
     * @param string $waypoint
53
     *
54
     * @return bool True if it is an gc waypoint
55
     */
56
    protected function isGCWaypoint($waypoint)
57
    {
58
        return stripos($waypoint, 'gc') === 0;
59
    }
60
}
61