|
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 AbstractComparator |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ComparatorInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $firstComparator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ComparatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $secondComparator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ComparatorInterface $firstComparator |
|
32
|
|
|
* @param ComparatorInterface $secondComparator |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(ComparatorInterface $firstComparator, ComparatorInterface $secondComparator) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->firstComparator = $firstComparator; |
|
37
|
|
|
$this->secondComparator = $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
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.