|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Stratadox\IdentityMap; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Ignore a certain class, for instance a value object. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Stratadox |
|
10
|
|
|
*/ |
|
11
|
|
|
final class Ignore implements MapsObjectsByIdentity |
|
12
|
|
|
{ |
|
13
|
|
|
private $ignoredClass; |
|
14
|
|
|
private $identityMap; |
|
15
|
|
|
|
|
16
|
|
|
private function __construct(string $ignoredClass, MapsObjectsByIdentity $identityMap) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->ignoredClass = $ignoredClass; |
|
19
|
|
|
$this->identityMap = $identityMap->removeAllObjectsOfThe($ignoredClass); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Wraps the identity map with a decorator that ignores a certain class. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $ignoredClass The class to be ignored. |
|
26
|
|
|
* @param MapsObjectsByIdentity $identityMap The identity map to wrap. |
|
27
|
|
|
* @return Ignore The wrapped identity map. |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function the(string $ignoredClass, MapsObjectsByIdentity $identityMap): MapsObjectsByIdentity |
|
30
|
|
|
{ |
|
31
|
|
|
return new self($ignoredClass, $identityMap); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** @inheritdoc */ |
|
35
|
|
|
public function has(string $class, string $id): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->identityMap->has($class, $id); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @inheritdoc */ |
|
41
|
|
|
public function hasThe(object $object): bool |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->identityMap->hasThe($object); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** @inheritdoc */ |
|
47
|
|
|
public function get(string $class, string $id): object |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->identityMap->get($class, $id); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @inheritdoc */ |
|
53
|
|
|
public function add(string $id, object $object): MapsObjectsByIdentity |
|
54
|
|
|
{ |
|
55
|
|
|
if ($object instanceof $this->ignoredClass) { |
|
56
|
|
|
return $this; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
return new self($this->ignoredClass, $this->identityMap->add($id, $object)); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @inheritdoc */ |
|
62
|
|
|
public function remove(string $class, string $id): MapsObjectsByIdentity |
|
63
|
|
|
{ |
|
64
|
|
|
if ($class === $this->ignoredClass) { |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
return $this->identityMap->remove($class, $id); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
|
71
|
|
|
public function removeThe(object $object): MapsObjectsByIdentity |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->identityMap->removeThe($object); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @inheritdoc */ |
|
77
|
|
|
public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity |
|
78
|
|
|
{ |
|
79
|
|
|
if ($class === $this->ignoredClass) { |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
return $this->identityMap->removeAllObjectsOfThe($class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @inheritdoc */ |
|
86
|
|
|
public function idOf(object $object): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->identityMap->idOf($object); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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.