|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\presentation; |
|
25
|
|
|
|
|
26
|
|
|
use ArrayIterator; |
|
27
|
|
|
use Countable; |
|
28
|
|
|
use IteratorAggregate; |
|
29
|
|
|
use precore\lang\Object; |
|
30
|
|
|
use precore\lang\ObjectInterface; |
|
31
|
|
|
use precore\util\Objects; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Several Order instance can be stored in a Sort object. |
|
35
|
|
|
* It provides some methods to be able to handle Orders easily. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class Sort extends Object implements IteratorAggregate, Countable |
|
40
|
|
|
{ |
|
41
|
|
|
private $orders = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $orders Array of Order objects |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $orders) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->orders = $orders; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Factory method to order by several properties with the same direction. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $properties |
|
55
|
|
|
* @param Direction $direction |
|
56
|
|
|
* @return Sort |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function create(array $properties, Direction $direction = null) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if ($direction === null) { |
|
61
|
|
|
$direction = Direction::$ASC; |
|
62
|
|
|
} |
|
63
|
|
|
$orders = []; |
|
64
|
|
|
foreach ($properties as $property) { |
|
65
|
|
|
$orders[] = new Order($direction, $property); |
|
66
|
|
|
} |
|
67
|
|
|
return new self($orders); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Does not modifies the object itself, |
|
72
|
|
|
* will return a new instance instead. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Sort $sort |
|
75
|
|
|
* @return Sort |
|
76
|
|
|
*/ |
|
77
|
|
|
public function andSort(Sort $sort = null) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($sort === null) { |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
$orders = $this->orders; |
|
83
|
|
|
foreach ($sort as $order) { |
|
84
|
|
|
$orders[] = $order; |
|
85
|
|
|
} |
|
86
|
|
|
return new Sort($orders); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns the direction defined to the given property. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $property |
|
93
|
|
|
* @return Order |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getOrderFor($property) |
|
96
|
|
|
{ |
|
97
|
|
|
/* @var $order Order */ |
|
98
|
|
|
foreach ($this as $order) { |
|
99
|
|
|
if ($order->getProperty() == $property) { |
|
100
|
|
|
return $order; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getIterator() |
|
107
|
|
|
{ |
|
108
|
|
|
return new ArrayIterator($this->orders); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function count() |
|
112
|
|
|
{ |
|
113
|
|
|
return count($this->orders); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function equals(ObjectInterface $object = null) |
|
117
|
|
|
{ |
|
118
|
|
|
if ($object === $this) { |
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
/* @var $object Sort */ |
|
122
|
|
|
return $object !== null |
|
123
|
|
|
&& $this->getClassName() === $object->getClassName() |
|
124
|
|
|
&& $this->orders === $object->orders; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function toString() |
|
128
|
|
|
{ |
|
129
|
|
|
return Objects::toStringHelper($this) |
|
130
|
|
|
->add('orders', $this->orders) |
|
131
|
|
|
->toString(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This check marks variable names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.