-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.php
More file actions
55 lines (50 loc) · 1.26 KB
/
Entity.php
File metadata and controls
55 lines (50 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace TuxBoy;
use Cake\ORM;
use TuxBoy\Exception\NotEntitySetterException;
/**
* Class Entity.
*/
class Entity extends ORM\Entity
{
/**
* Entity constructor.
*
* @param array $properties
* @param array $options
*/
public function __construct(array $properties = [], array $options = [])
{
parent::__construct($properties, $options);
}
/**
* @param array $properties
*/
public function setData(array $properties)
{
$this->loadData($properties);
}
/**
* @param array $data
*
* @throws NotEntitySetterException
*/
private function loadData(array $data = [])
{
if (!empty($data)) {
foreach ($data as $field => $value) {
if (mb_substr($field, -3) === '_id') {
$foreignKeyToObject = str_replace(mb_substr($field, -3), '', $field);
$field = $foreignKeyToObject;
}
if ($field !== 'id') {
$setter = 'set' . ucfirst($field);
}
if (!method_exists($this, $setter)) {
throw new NotEntitySetterException();
}
$this->$setter($value);
}
}
}
}