|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
5
|
|
|
* Date: 6/15/15 |
|
6
|
|
|
* Time: 11:08 PM. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace NilPortugues\Foundation\Domain\Model\Repository; |
|
12
|
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Order as OrderInterface; |
|
15
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort as SortInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Sort implements SortInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $properties; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Order |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $order; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates a new Sort instance using the given Order. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $properties |
|
33
|
|
|
* @param OrderInterface $order |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(array $properties = [], OrderInterface $order = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setOrder($order); |
|
38
|
|
|
$this->setProperties($properties); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param OrderInterface|null $order |
|
43
|
|
|
* |
|
44
|
|
|
* @return OrderInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function setOrder(OrderInterface $order = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if (null === $order) { |
|
49
|
|
|
$order = new Order(Order::ASCENDING); |
|
50
|
|
|
} |
|
51
|
|
|
$this->order = $order; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a new Sort consisting of the orders of the current Sort combined with the given ones. |
|
56
|
|
|
* |
|
57
|
|
|
* @param SortInterface $sort |
|
58
|
|
|
* |
|
59
|
|
|
* @return SortInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
public function andSort(SortInterface $sort) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->properties = array_merge($this->orders(), $sort->orders()); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function orders() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->properties; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param array $properties |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function setProperties(array $properties) |
|
80
|
|
|
{ |
|
81
|
|
|
if (false === empty($properties)) { |
|
82
|
|
|
$properties = array_fill_keys(array_values($properties), clone $this->order); |
|
83
|
|
|
} |
|
84
|
|
|
$this->properties = $properties; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param SortInterface $sort |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function equals(SortInterface $sort) |
|
93
|
|
|
{ |
|
94
|
|
|
return $sort->orders() == $this->orders(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the order registered for the given property. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $propertyName |
|
101
|
|
|
* |
|
102
|
|
|
* @return Order |
|
103
|
|
|
*/ |
|
104
|
|
|
public function orderFor($propertyName) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->hasProperty($propertyName); |
|
107
|
|
|
|
|
108
|
|
|
return $this->properties[$propertyName]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $propertyName |
|
113
|
|
|
* |
|
114
|
|
|
* @throws \InvalidArgumentException |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function hasProperty($propertyName) |
|
117
|
|
|
{ |
|
118
|
|
|
if (true === empty($this->properties[$propertyName])) { |
|
119
|
|
|
throw new InvalidArgumentException('Provided property could not be found.'); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $propertyName |
|
125
|
|
|
* @param OrderInterface $order |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setOrderFor($propertyName, OrderInterface $order) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->properties[$propertyName] = $order; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $propertyName |
|
134
|
|
|
* |
|
135
|
|
|
* @return OrderInterface |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \InvalidArgumentException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function property($propertyName) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->hasProperty($propertyName); |
|
142
|
|
|
|
|
143
|
|
|
return $this->properties[$propertyName]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|