-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractEquatable.php
More file actions
37 lines (32 loc) · 1.13 KB
/
AbstractEquatable.php
File metadata and controls
37 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace Jkhaled\Equatable;
abstract class AbstractEquatable implements EquatableInterface
{
abstract public function getProperties(): array;
/**
* @param $object
* @return bool
* @throws PropertyNotExistException|BadClassException
*/
public function equalTo(EquatableInterface $object): bool
{
if (!$object instanceof static) {
throw new BadClassException(sprintf('object must be instance of same class'));
}
$props = $this->getProperties();
foreach ($props as $prop) {
if (!property_exists($this, $prop)) {
throw new PropertyNotExistException(sprintf('property %s not exist', $prop));
}
$getPropertyValue = function ($o, $property) {
return $o->$property;
};
$propOfObject = \Closure::bind($getPropertyValue, null, $object);
$propOfCurrent = \Closure::bind($getPropertyValue, null, $this);
if ($propOfCurrent($this, $prop) !== $propOfObject($object, $prop)) {
return false;
}
}
return true;
}
}