Skip to content

Commit 11b3e7b

Browse files
committed
Add base class for entities, and his associated exception handler
1 parent 2504e2c commit 11b3e7b

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

src/LightQL/Entities/Entity.php

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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\Annotations\IAnnotation;
37+
use ElementaryFramework\LightQL\Exceptions\EntityException;
38+
39+
/**
40+
* LightQL - Database Manager Class
41+
*
42+
* @abstract
43+
* @category Library
44+
* @package LightQL
45+
* @author Nana Axel <ax.lnana@outlook.com>
46+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Entities/Entity
47+
*/
48+
abstract class Entity implements IEntity
49+
{
50+
/**
51+
* Fetch data in eager mode.
52+
*/
53+
const FETCH_EAGER = 1;
54+
55+
/**
56+
* Fetch data in lazy mode.
57+
*/
58+
const FETCH_LAZY = 2;
59+
60+
/**
61+
* The raw data provided from the database.
62+
*
63+
* @var array
64+
*/
65+
protected $raw = array();
66+
67+
/**
68+
* The reflection class of this entity.
69+
*
70+
* @var \ReflectionClass
71+
*/
72+
private $_reflection = null;
73+
74+
/**
75+
* The array of database column of this entity.
76+
*
77+
* @var Column[]
78+
*/
79+
private $_columns = array();
80+
81+
82+
/**
83+
* Entity constructor.
84+
*
85+
* @param array $data The raw database data.
86+
*
87+
* @throws EntityException
88+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
89+
*/
90+
public function __construct(array $data = array())
91+
{
92+
if (!Annotations::classHasAnnotation($this, "@entity")) {
93+
throw new EntityException("Cannot create an entity without the @entity annotation.");
94+
}
95+
96+
$this->_reflection = new \ReflectionClass($this);
97+
$properties = $this->_reflection->getProperties();
98+
99+
foreach ($properties as $property) {
100+
if ($this->_hasAnnotation($property->name, "@column")) {
101+
$name = $this->_getMetadata($property->name, "@column", 'name');
102+
$type = $this->_getMetadata($property->name, "@column", 'type');
103+
$size = array(
104+
$this->_getMetadata($property->name, '@size', 'min'),
105+
$this->_getMetadata($property->name, '@size', 'max')
106+
);
107+
108+
$column = new Column($name, $type, $size);
109+
110+
$column->isPrimaryKey = $this->_hasAnnotation($property->name, '@id');
111+
$column->isUniqueKey = $column->isPrimaryKey || $this->_hasAnnotation($property->name, '@unique');
112+
$column->isAutoIncrement = $this->_hasAnnotation($property->name, '@autoIncrement');
113+
114+
$this->_columns[$property->name] = $column;
115+
}
116+
}
117+
118+
$this->hydrate($data);
119+
}
120+
121+
/**
122+
* Populates data in the entity.
123+
*
124+
* @param array $data The raw database data.
125+
*/
126+
public function hydrate(array $data)
127+
{
128+
$this->raw = $data;
129+
130+
// Populate @column properties
131+
foreach ($this->_columns as $property => $column) {
132+
if (array_key_exists($column->getName(), $this->raw)) {
133+
$this->{$property} = $this->raw[$column->getName()];
134+
} else {
135+
$this->{$property} = $column->getDefault();
136+
}
137+
}
138+
}
139+
140+
/**
141+
* Gets the raw value of a table column.
142+
*
143+
* @param string $column The table column name.
144+
*
145+
* @return mixed
146+
*/
147+
public function get(string $column)
148+
{
149+
if ($this->_exists($column)) {
150+
return $this->raw[$column];
151+
}
152+
153+
return null;
154+
}
155+
156+
/**
157+
* Sets the raw value of a table column.
158+
*
159+
* @param string $column The table column name.
160+
* @param mixed $value The table column value.
161+
*/
162+
public function set(string $column, $value)
163+
{
164+
if ($this->_exists($column)) {
165+
$this->raw[$column] = $value;
166+
}
167+
}
168+
169+
/**
170+
* Gets the list of table columns
171+
* of this entity.
172+
*
173+
* @return Column[]
174+
*/
175+
public function getColumns(): array
176+
{
177+
return $this->_columns;
178+
}
179+
180+
/**
181+
* @param $property
182+
* @param $annotation
183+
* @return bool
184+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
185+
*/
186+
private function _hasAnnotation($property, $annotation): bool
187+
{
188+
return Annotations::propertyHasAnnotation($this, $property, $annotation);
189+
}
190+
191+
/**
192+
* @param $property
193+
* @param $type
194+
* @param $name
195+
* @param null $default
196+
* @return IAnnotation
197+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
198+
*/
199+
private function _getMetadata($property, $type, $name = null, $default = null)
200+
{
201+
$a = Annotations::ofProperty($this, $property, $type);
202+
203+
if (!count($a)) {
204+
return $default;
205+
}
206+
207+
if ($name === null) {
208+
return $a[0];
209+
}
210+
211+
return $a[0]->$name;
212+
}
213+
214+
/**
215+
* @param string $column
216+
* @return bool
217+
*/
218+
private function _exists(string $column): bool
219+
{
220+
return array_key_exists($column, $this->raw);
221+
}
222+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Exceptions;
34+
35+
/**
36+
* Entity Exception
37+
*
38+
* @package LightQL
39+
* @author Nana Axel <ax.lnana@outlook.com>
40+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Exceptions/EntityException
41+
*/
42+
class EntityException extends \Exception
43+
{
44+
}

0 commit comments

Comments
 (0)