1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Adapters\Sensio; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\ApiEntityManager; |
6
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
7
|
|
|
|
8
|
|
|
final class MapperRegistry implements ManagerRegistry |
9
|
|
|
{ |
10
|
|
|
/** @var ApiEntityManager */ |
11
|
|
|
private $manager; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* MapperRegistry constructor. |
15
|
|
|
* |
16
|
|
|
* @param ApiEntityManager $manager |
17
|
|
|
*/ |
18
|
1 |
|
public function __construct(ApiEntityManager $manager) |
19
|
|
|
{ |
20
|
1 |
|
$this->manager = $manager; |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
/** {@inheritdoc} */ |
24
|
|
|
public function getDefaultConnectionName() |
25
|
|
|
{ |
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** {@inheritdoc} */ |
30
|
|
|
public function getConnection($name = null) |
31
|
|
|
{ |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Gets an array of all registered connections. |
37
|
|
|
* |
38
|
|
|
* @return array An array of Connection instances. |
39
|
|
|
*/ |
40
|
|
|
public function getConnections() |
41
|
|
|
{ |
42
|
|
|
return []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** {@inheritdoc} */ |
46
|
|
|
public function getConnectionNames() |
47
|
|
|
{ |
48
|
|
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** {@inheritdoc} */ |
52
|
1 |
|
public function getDefaultManagerName() |
53
|
|
|
{ |
54
|
1 |
|
return 'default'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritdoc} */ |
58
|
|
|
public function getManager($name = null) |
59
|
|
|
{ |
60
|
|
|
if (null === $name || $this->getDefaultManagerName() === $name) { |
61
|
|
|
return $this->manager; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new \OutOfBoundsException('Invalid API entity manager: ' . $name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
68
|
1 |
|
public function getManagers() |
69
|
|
|
{ |
70
|
1 |
|
return [$this->getDefaultManagerName() => $this->manager]; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** {@inheritdoc} */ |
74
|
|
|
public function resetManager($name = null) |
75
|
|
|
{ |
76
|
|
|
return $this->getManager($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** {@inheritdoc} */ |
80
|
|
|
public function getAliasNamespace($alias) |
81
|
|
|
{ |
82
|
|
|
throw new \OutOfBoundsException('Unable to resolve alias ' . $alias); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** {@inheritdoc} */ |
86
|
|
|
public function getManagerNames() |
87
|
|
|
{ |
88
|
|
|
return [$this->getDefaultManagerName()]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** {@inheritdoc} */ |
92
|
|
|
public function getRepository($persistentObject, $persistentManagerName = null) |
93
|
|
|
{ |
94
|
|
|
return $this->getManager($persistentManagerName)->getRepository($persistentObject); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** {@inheritdoc} */ |
98
|
1 |
|
public function getManagerForClass($class) |
99
|
|
|
{ |
100
|
1 |
|
if (!$this->manager->getMetadataFactory()->isTransient($class)) { |
101
|
1 |
|
return $this->manager; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.