1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
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 Cubiche\Core\Comparable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Multi Comparator class. |
15
|
|
|
* |
16
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class MultiComparator extends Comparator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ComparatorInterface |
22
|
|
|
*/ |
23
|
|
|
protected $firstComparator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ComparatorInterface |
27
|
|
|
*/ |
28
|
|
|
protected $secondComparator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param callable $firstComparator |
32
|
|
|
* @param callable $secondComparator |
33
|
|
|
*/ |
34
|
|
|
public function __construct(callable $firstComparator, callable $secondComparator) |
35
|
|
|
{ |
36
|
|
|
$this->firstComparator = self::from($firstComparator); |
37
|
|
|
$this->secondComparator = self::from($secondComparator); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Cubiche\Core\Comparable\ComparatorInterface |
42
|
|
|
*/ |
43
|
|
|
public function firstComparator() |
44
|
|
|
{ |
45
|
|
|
return $this->firstComparator; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Cubiche\Core\Comparable\ComparatorInterface |
50
|
|
|
*/ |
51
|
|
|
public function secondComparator() |
52
|
|
|
{ |
53
|
|
|
return $this->secondComparator; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function compare($a, $b) |
60
|
|
|
{ |
61
|
|
|
$result = $this->firstComparator()->compare($a, $b); |
62
|
|
|
|
63
|
|
|
return $result !== 0 ? $result : $this->secondComparator()->compare($a, $b); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function reverse() |
70
|
|
|
{ |
71
|
|
|
return new self($this->firstComparator()->reverse(), $this->secondComparator()->reverse()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|