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