1 | <?php |
||
16 | final class EqualsBuilder |
||
17 | { |
||
18 | private $isEquals = true; |
||
19 | |||
20 | public static function create() : EqualsBuilder |
||
37 | |||
38 | private function checkForSameType($leftHandedValue, $rightHandedValue) :bool |
||
46 | |||
47 | private function checkIfValuesAreAnObjectAndEqual($leftHandedValue, $rightHandedValue) : bool |
||
48 | { |
||
49 | if (! $this->checkIfValuesAreAnObject($leftHandedValue, $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 | private function checkIfValuesAreAnObject($leftHandedValue, $rightHandedValue) : bool |
||
63 | { |
||
64 | return (is_object($leftHandedValue) && is_object($rightHandedValue)); |
||
65 | } |
||
66 | |||
67 | private function compareObjectProperties($leftHandedObject, $rightHandedObject) : bool |
||
68 | { |
||
69 | $reflectionOfLeftHandedObject = new ReflectionClass($leftHandedObject); |
||
70 | $propertiesOfLeftHandedObject = $this->getPropertiesAsAnArray($leftHandedObject, $reflectionOfLeftHandedObject); |
||
71 | $reflectionOfRightHandedObject = new ReflectionClass($rightHandedObject); |
||
72 | $propertiesOfRightHandedObject = $this->getPropertiesAsAnArray( |
||
73 | $rightHandedObject, |
||
74 | $reflectionOfRightHandedObject |
||
75 | ); |
||
76 | return $this->checkValuesRecursively($propertiesOfRightHandedObject, $propertiesOfLeftHandedObject); |
||
77 | } |
||
78 | |||
79 | private function checkValuesRecursively( |
||
80 | array $propertiesOfLeftHandedObject, |
||
81 | array $propertiesOfRightHandedObject |
||
82 | ) : bool { |
||
83 | $innerEqualsBuilder = self::create(); |
||
84 | foreach ($propertiesOfLeftHandedObject as $propertyName => $propertyValue) { |
||
85 | $innerEqualsBuilder = $innerEqualsBuilder->append( |
||
86 | $propertyValue, |
||
87 | $propertiesOfRightHandedObject[$propertyName] |
||
88 | ); |
||
89 | } |
||
90 | return $innerEqualsBuilder->isEquals(); |
||
91 | } |
||
92 | |||
93 | private function getPropertiesAsAnArray($sourceObject, $object) :array |
||
104 | |||
105 | public function isEquals() : bool |
||
109 | } |
||
110 |