1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami\Stdlib; |
5
|
|
|
|
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class EqualsBuilder |
10
|
|
|
* |
11
|
|
|
* This class provides methods to build a good equals method for any class. Intended to be use to compare Value Objects. |
12
|
|
|
* @package Selami\Stdlib |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
final class EqualsBuilder |
17
|
|
|
{ |
18
|
|
|
private $isEquals = true; |
19
|
|
|
|
20
|
|
|
public static function create() : EqualsBuilder |
21
|
|
|
{ |
22
|
|
|
return new static(); |
23
|
|
|
} |
24
|
|
|
public function append($leftHandedValue, $rightHandedValue) |
25
|
|
|
{ |
26
|
|
|
if ($this->isEquals === false) { |
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
if ($this->checkIfValuesAreClass($leftHandedValue, $rightHandedValue)) { |
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
if ($this->checkForSameType($leftHandedValue, $rightHandedValue)) { |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function checkForSameType($leftHandedValue, $rightHandedValue) :bool |
39
|
|
|
{ |
40
|
|
|
if ($leftHandedValue !== $rightHandedValue) { |
41
|
|
|
$this->isEquals = false; |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function checkIfValuesAreClass($leftHandedValue, $rightHandedValue) : bool |
48
|
|
|
{ |
49
|
|
|
if (! is_object($leftHandedValue) || ! is_object($rightHandedValue)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
if (get_class($leftHandedValue) !== get_class($rightHandedValue)) { |
53
|
|
|
$this->isEquals = false; |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
if (! $this->compareObjectProperties($leftHandedValue, $rightHandedValue)) { |
57
|
|
|
$this->isEquals = false; |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function compareObjectProperties($leftHandedObject, $rightHandedObject) : bool |
64
|
|
|
{ |
65
|
|
|
$reflectionOfLeftHandedObject = new ReflectionClass($leftHandedObject); |
66
|
|
|
$propertiesOfLeftHandedObject = $this->getPropertiesAsAnArray($leftHandedObject, $reflectionOfLeftHandedObject); |
67
|
|
|
$reflectionOfRightHandedObject = new ReflectionClass($rightHandedObject); |
68
|
|
|
$propertiesOfRightHandedObject = $this->getPropertiesAsAnArray( |
69
|
|
|
$rightHandedObject, |
70
|
|
|
$reflectionOfRightHandedObject |
71
|
|
|
); |
72
|
|
|
return $this->checkValuesRecursively($propertiesOfRightHandedObject, $propertiesOfLeftHandedObject); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function checkValuesRecursively( |
76
|
|
|
array $propertiesOfLeftHandedObject, |
77
|
|
|
array $propertiesOfRightHandedObject |
78
|
|
|
) : bool { |
79
|
|
|
$innerEqualsBuilder = self::create(); |
80
|
|
|
foreach ($propertiesOfLeftHandedObject as $propertyName => $propertyValue) { |
81
|
|
|
$innerEqualsBuilder = $innerEqualsBuilder->append( |
82
|
|
|
$propertyValue, |
83
|
|
|
$propertiesOfRightHandedObject[$propertyName] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
return $innerEqualsBuilder->isEquals(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getPropertiesAsAnArray($sourceObject, $object) :array |
90
|
|
|
{ |
91
|
|
|
$propertyList = []; |
92
|
|
|
foreach ($object->getProperties() as $property) { |
93
|
|
|
if ($property->isProtected() || $property->isPrivate()) { |
94
|
|
|
$property->setAccessible(true); |
95
|
|
|
} |
96
|
|
|
$propertyList[$property->name] = $property->getValue($sourceObject); |
97
|
|
|
} |
98
|
|
|
return $propertyList; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isEquals() : bool |
102
|
|
|
{ |
103
|
|
|
return $this->isEquals; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|