|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Entity\EntityInterface; |
|
6
|
|
|
use \Doctrine\ODM\MongoDB as ODM; |
|
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractRepository extends ODM\DocumentRepository implements RepositoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
protected $entityPrototype; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param ODM\DocumentManager $dm |
|
16
|
|
|
* @param ODM\UnitOfWork $uow |
|
17
|
|
|
* @param ODM\Mapping\ClassMetadata $class |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(ODM\DocumentManager $dm, ODM\UnitOfWork $uow, ODM\Mapping\ClassMetadata $class) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($dm, $uow, $class); |
|
22
|
|
|
$eventArgs = new DoctrineMongoODM\Event\EventArgs( |
|
23
|
|
|
array( |
|
24
|
|
|
'repository' => $this |
|
25
|
|
|
) |
|
26
|
|
|
); |
|
27
|
|
|
$dm->getEventManager()->dispatchEvent(DoctrineMongoODM\Event\RepositoryEventsSubscriber::postConstruct, $eventArgs); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
32
|
|
|
*/ |
|
33
|
|
|
public function init(ServiceLocatorInterface $serviceLocator) |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param $name |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getService($name) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->dm->getConfiguration()->getServiceLocator()->get($name); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param EntityInterface $entity |
|
50
|
|
|
* |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setEntityPrototype(EntityInterface $entity) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->entityPrototype = $entity; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $data |
|
|
|
|
|
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function create(array $data = null, $persist = false) |
|
65
|
|
|
{ |
|
66
|
|
|
if (null === $this->entityPrototype) { |
|
67
|
|
|
throw new \RuntimeException('Could not create an entity. No prototype is set!'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$entity = clone $this->entityPrototype; |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $data) { |
|
73
|
|
|
foreach ($data as $property => $value) { |
|
74
|
|
|
$setter = "set$property"; |
|
75
|
|
|
|
|
76
|
|
|
if (method_exists($entity, $setter)) { |
|
77
|
|
|
$entity->$setter($value); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($persist) { |
|
83
|
|
|
$this->dm->persist($entity); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $entity; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param $entity |
|
91
|
|
|
* @throws \InvalidArgumentException |
|
92
|
|
|
* @return self |
|
93
|
|
|
*/ |
|
94
|
|
|
public function store($entity) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->checkEntityType($entity); |
|
97
|
|
|
$this->dm->persist($entity); |
|
98
|
|
|
$this->dm->flush($entity); |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function remove($entity) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->checkEntityType($entity); |
|
106
|
|
|
$this->dm->remove($entity); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function checkEntityType($entity) |
|
112
|
|
|
{ |
|
113
|
|
|
if ( !($entity instanceOf $this->entityPrototype) ) { |
|
114
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
115
|
|
|
'Entity must be of type %s but recieved %s instead', |
|
116
|
|
|
get_class($this->entityPrototype), |
|
117
|
|
|
get_class($entity) |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: