1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Managlea\Component\EntityManager; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\Decorator\EntityManagerDecorator; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface as DoctrineEntityManagerInterface; |
10
|
|
|
use Managlea\Component\EntityManagerInterface; |
11
|
|
|
|
12
|
|
|
class DoctrineEntityManager extends EntityManagerDecorator implements EntityManagerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* DoctrineEntityManager constructor. |
16
|
|
|
* @param DoctrineEntityManagerInterface $wrapped |
17
|
|
|
*/ |
18
|
16 |
|
public function __construct(DoctrineEntityManagerInterface $wrapped) |
19
|
|
|
{ |
20
|
16 |
|
parent::__construct($wrapped); |
21
|
16 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
6 |
|
public function get(string $objectName, int $id, array $criteria = array()) |
27
|
|
|
{ |
28
|
6 |
|
$repository = $this->getRepository($objectName); |
29
|
|
|
|
30
|
5 |
|
$criteria['id'] = $id; |
31
|
5 |
|
$entityObject = $repository->findOneBy($criteria); |
32
|
|
|
|
33
|
5 |
|
if (!$entityObject) { |
34
|
3 |
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
return $entityObject; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
6 |
|
public function getCollection(string $objectName, array $filters = array(), int $limit = 20, int $offset = 0, array $order = null) : array |
44
|
|
|
{ |
45
|
6 |
|
$repository = $this->getRepository($objectName); |
46
|
5 |
|
$collection = $repository->findBy($filters, $order, $limit, $offset); |
47
|
|
|
|
48
|
5 |
|
return $collection; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
2 |
|
public function create(string $objectName, array $data) |
55
|
|
|
{ |
56
|
2 |
|
$detachedEntity = self::createDetachedEntity($objectName, $data); |
57
|
|
|
|
58
|
2 |
|
$entity = $this->merge($detachedEntity); |
59
|
2 |
|
$this->persist($entity); |
60
|
2 |
|
$this->flush(); |
61
|
|
|
|
62
|
2 |
|
return $entity; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function update(string $objectName, int $id, array $data) |
69
|
|
|
{ |
70
|
1 |
|
return $this->executeActionOnEntity('update', $objectName, $id, $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function delete(string $objectName, int $id) : bool |
77
|
|
|
{ |
78
|
1 |
|
return (bool)$this->executeActionOnEntity('remove', $objectName, $id); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $method |
83
|
|
|
* @param string $objectName |
84
|
|
|
* @param int $id |
85
|
|
|
* @param array $data |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
2 |
|
private function executeActionOnEntity(string $method, string $objectName, int $id, array $data = null) |
89
|
|
|
{ |
90
|
2 |
|
$entityObject = $this->get($objectName, $id); |
91
|
|
|
|
92
|
2 |
|
if (!$entityObject) { |
93
|
2 |
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
if ($method == 'update' && is_array($data)) { |
97
|
1 |
|
self::updateEntityFromArray($entityObject, $data); |
98
|
1 |
|
} elseif ($method == 'remove') { |
99
|
1 |
|
$this->remove($entityObject); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$this->flush(); |
103
|
|
|
|
104
|
2 |
|
if ($method == 'update') { |
105
|
1 |
|
return $entityObject; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates new entity based on object name sets object |
113
|
|
|
* parameter from data |
114
|
|
|
* @param string $objectName |
115
|
|
|
* @param array $data |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
3 |
|
public static function createDetachedEntity(string $objectName, array $data) |
119
|
|
|
{ |
120
|
3 |
|
$detachedEntity = new $objectName; |
121
|
|
|
|
122
|
3 |
|
self::updateEntityFromArray($detachedEntity, $data); |
123
|
|
|
|
124
|
3 |
|
return $detachedEntity; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Updates Entity from data calling setters based |
129
|
|
|
* on data keys |
130
|
|
|
* @param object $entity |
131
|
|
|
* @param array $data |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
4 |
|
public static function updateEntityFromArray($entity, array $data) |
135
|
|
|
{ |
136
|
4 |
|
foreach ($data as $field => $value) { |
137
|
4 |
|
$method = 'set' . self::formatStringToMethodName($field); |
138
|
4 |
|
if (method_exists($entity, $method)) { |
139
|
4 |
|
$entity->$method($value); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
4 |
|
return $entity; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Generates camel-case method names from string |
148
|
|
|
* @param string $input |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
4 |
|
public static function formatStringToMethodName(string $input) : string |
152
|
|
|
{ |
153
|
4 |
|
$methodName = implode('', array_map('ucfirst', explode('_', strtolower($input)))); |
154
|
|
|
|
155
|
4 |
|
return $methodName; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|