Skip to content

Commit 9dabf55

Browse files
committed
Add the entity manager
1 parent 77c1a6c commit 9dabf55

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
3+
/**
4+
* LightQL - The lightweight PHP ORM
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package LightQL
26+
* @author Axel Nana <ax.lnana@outlook.com>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/LightQL/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://lightql.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\LightQL\Entities;
34+
35+
use ElementaryFramework\Annotations\Annotations;
36+
use ElementaryFramework\LightQL\LightQL;
37+
use ElementaryFramework\LightQL\Persistence\PersistenceUnit;
38+
39+
/**
40+
* Entity Manager
41+
*
42+
* Manage all entities, using one same persistence unit.
43+
*
44+
* @abstract
45+
* @category Library
46+
* @package LightQL
47+
* @author Nana Axel <ax.lnana@outlook.com>
48+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Entities/PersistenceUnit
49+
*/
50+
final class EntityManager
51+
{
52+
/**
53+
* The persistence unit of this entity
54+
* manager.
55+
*
56+
* @var PersistenceUnit
57+
*/
58+
private $_persistenceUnit;
59+
60+
/**
61+
* The LightQL instance used by this
62+
* entity manager.
63+
*
64+
* @var LightQL
65+
*/
66+
private $_lightql;
67+
68+
/**
69+
* EntityManager constructor.
70+
*
71+
* @param PersistenceUnit $persistenceUnit The persistence unit to use in this manager.
72+
*/
73+
public function __construct(PersistenceUnit $persistenceUnit)
74+
{
75+
// Save the persistence unit
76+
$this->_persistenceUnit = $persistenceUnit;
77+
78+
// Create a LightQL instance
79+
$this->_lightql = new LightQL(
80+
array(
81+
"dbms" => $this->_persistenceUnit->getDbms(),
82+
"database" => $this->_persistenceUnit->getDatabase(),
83+
"hostname" => $this->_persistenceUnit->getHostname(),
84+
"username" => $this->_persistenceUnit->getUsername(),
85+
"password" => $this->_persistenceUnit->getPassword()
86+
)
87+
);
88+
}
89+
90+
/**
91+
* Finds an entity from the database.
92+
*
93+
* @param string $entityClass The class name of the entity to find.
94+
* @param mixed $id The value of the primary key.
95+
*
96+
* @return Entity
97+
*
98+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
99+
* @throws \ElementaryFramework\LightQL\Exceptions\LightQLException
100+
*/
101+
public function find(string $entityClass, $id): Entity
102+
{
103+
$entityAnnotation = Annotations::ofClass($entityClass, "@entity");
104+
105+
$entity = new $entityClass;
106+
$columns = $entity->getColumns();
107+
108+
$where = array();
109+
110+
foreach ($columns as $property => $column) {
111+
if (count($where) === 0) {
112+
if ($column->isPrimaryKey) {
113+
$where[$column->getName()] = $this->_lightql->quote($id);
114+
}
115+
} else break;
116+
}
117+
118+
$raw = $this->_lightql
119+
->from($entityAnnotation[0]->table)
120+
->where($where)
121+
->selectFirst();
122+
123+
$entity->hydrate($raw);
124+
125+
return$entity;
126+
}
127+
128+
/**
129+
* Persists an entity into the database.
130+
*
131+
* @param Entity $entity The entity to create.
132+
*
133+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
134+
* @throws \ElementaryFramework\LightQL\Exceptions\LightQLException
135+
*/
136+
public function persist(Entity &$entity)
137+
{
138+
$entityAnnotation = Annotations::ofClass($entity, "@entity");
139+
140+
$columns = $entity->getColumns();
141+
$fieldAndValues = array();
142+
143+
$autoIncrementProperty = null;
144+
145+
foreach ($columns as $property => $column) {
146+
$fieldAndValues[$column->getName()] = $this->_lightql->quote($entity->$property);
147+
148+
if ($autoIncrementProperty === null && $column->isAutoIncrement) {
149+
$autoIncrementProperty = $property;
150+
}
151+
}
152+
153+
$this->_lightql
154+
->from($entityAnnotation[0]->table)
155+
->insert($fieldAndValues);
156+
157+
if ($autoIncrementProperty !== null) {
158+
$entity->$autoIncrementProperty = $this->_lightql->lastInsertID();
159+
}
160+
}
161+
162+
/**
163+
* Merges the entity in the database with the given one.
164+
*
165+
* @param Entity $entity The entity to edit.
166+
*
167+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
168+
* @throws \ElementaryFramework\LightQL\Exceptions\LightQLException
169+
*/
170+
public function merge(Entity &$entity)
171+
{
172+
$entityAnnotation = Annotations::ofClass($entity, "@entity");
173+
174+
$columns = $entity->getColumns();
175+
$fieldAndValues = array();
176+
177+
$where = array();
178+
179+
foreach ($columns as $property => $column) {
180+
$fieldAndValues[$column->getName()] = $this->_lightql->quote($entity->$property);
181+
182+
if ($column->isPrimaryKey) {
183+
$where[$column->getName()] = $this->_lightql->quote($entity->get($column->getName()));
184+
}
185+
}
186+
187+
$this->_lightql
188+
->from($entityAnnotation[0]->table)
189+
->where($where)
190+
->update($fieldAndValues);
191+
}
192+
193+
/**
194+
* Removes an entity from the database.
195+
*
196+
* @param Entity $entity The entity to delete.
197+
*
198+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
199+
* @throws \ElementaryFramework\LightQL\Exceptions\LightQLException
200+
*/
201+
public function delete(Entity &$entity)
202+
{
203+
$entityAnnotation = Annotations::ofClass($entity, "@entity");
204+
205+
$columns = $entity->getColumns();
206+
$fieldAndValues = array();
207+
208+
$where = array();
209+
$pk = array();
210+
211+
foreach ($columns as $property => $column) {
212+
$fieldAndValues[$column->getName()] = $this->_lightql->quote($entity->$property);
213+
214+
if ($column->isPrimaryKey) {
215+
$where[$column->getName()] = $this->_lightql->quote($entity->$property);
216+
$pk[] = $property;
217+
}
218+
}
219+
220+
$this->_lightql
221+
->from($entityAnnotation[0]->table)
222+
->where($where)
223+
->delete();
224+
225+
if (count($pk) > 0) {
226+
foreach ($pk as $item) {
227+
$entity->$item = null;
228+
}
229+
}
230+
}
231+
232+
/**
233+
* @return LightQL
234+
*/
235+
public function getLightQL(): LightQL
236+
{
237+
return $this->_lightql;
238+
}
239+
}

0 commit comments

Comments
 (0)