|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2016 Sebastian Böttger <[email protected]> |
|
4
|
|
|
* You may use, distribute and modify this code under the |
|
5
|
|
|
* terms of the MIT license. |
|
6
|
|
|
* |
|
7
|
|
|
* You should have received a copy of the MIT license with |
|
8
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Seboettg\Collection\Comparable; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Abstract class Comparator. When extending this class the compare function must be implemented. compare() is a |
|
15
|
|
|
* comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a |
|
16
|
|
|
* sort method to allow precise control over the sort order. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Seboettg\Collection |
|
19
|
|
|
* |
|
20
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class Comparator |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* static constant for sorting order ascending |
|
27
|
|
|
*/ |
|
28
|
|
|
const ORDER_ASC = "ASC"; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* static constant for sorting order descending |
|
32
|
|
|
*/ |
|
33
|
|
|
const ORDER_DESC = "DESC"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* static constant for a custom sorting order |
|
37
|
|
|
*/ |
|
38
|
|
|
const ORDER_CUSTOM = "CUSTOM"; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* defines the order (ascending|descending) for a comparison |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $sortingOrder; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* object/array which can be used to define a custom order |
|
49
|
|
|
* @var mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $customOrder; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Comparator constructor. |
|
55
|
|
|
* @param string $sortingOrder defines the order (ascending|descending) for a comparison |
|
56
|
|
|
* @param mixed $customOrder |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function __construct($sortingOrder = self::ORDER_ASC, $customOrder = null) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$this->sortingOrder = $sortingOrder; |
|
61
|
2 |
|
$this->customOrder = $customOrder; |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first |
|
66
|
|
|
* argument is less than, equal to, or greater than the second. |
|
67
|
|
|
* |
|
68
|
|
|
* @param Comparable $a |
|
69
|
|
|
* @param Comparable $b |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public abstract function compare(Comparable $a, Comparable $b); |
|
73
|
|
|
} |
|
74
|
|
|
|