Sort::setProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Nord\Lumen\Search;
2
3
use Nord\Lumen\Search\Exceptions\InvalidArgument;
4
5
class Sort
6
{
7
    const DIRECTION_ASCENDING  = 'asc';
8
    const DIRECTION_DESCENDING = 'desc';
9
10
    /**
11
     * @var string
12
     */
13
    private $property;
14
15
    /**
16
     * @var string
17
     */
18
    private $direction;
19
20
    /**
21
     * @var array
22
     */
23
    private $validDirections = [self::DIRECTION_ASCENDING, self::DIRECTION_DESCENDING];
24
25
26
    /**
27
     * Sort constructor.
28
     *
29
     * @param string $property
30
     * @param string $direction
31
     */
32
    public function __construct($property, $direction)
33
    {
34
        $this->setProperty($property);
35
        $this->setDirection($direction);
36
    }
37
38
39
    /**
40
     * @return string
41
     */
42
    public function getProperty()
43
    {
44
        return $this->property;
45
    }
46
47
48
    /**
49
     * @return string
50
     */
51
    public function getDirection()
52
    {
53
        return $this->direction;
54
    }
55
56
57
    /**
58
     * @param string $property
59
     */
60
    private function setProperty($property)
61
    {
62
        $this->property = $property;
63
    }
64
65
66
    /**
67
     * @param string $direction
68
     *
69
     * @throws InvalidArgument
70
     */
71
    private function setDirection($direction)
72
    {
73
        if (!in_array($direction, $this->validDirections)) {
74
            throw new InvalidArgument("Sort direction '$direction' is not supported.");
75
        }
76
77
        $this->direction = $direction;
78
    }
79
}
80