-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntityModel.php
150 lines (109 loc) · 3.84 KB
/
EntityModel.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Greg\Orm;
class EntityModel extends ModelAbstract
{
const STATE_NEW = 1;
const STATE_MANAGED = 2;
protected $entityClass;
private $entityStates = [];
public function entityClass()
{
if (!$this->entityClass) {
throw new \Exception('Entity class is not defined in the model.');
}
return $this->entityClass;
}
public function new(array $record = [])
{
return $this->prepareRowInstance($record);
}
public function create(array $record = [])
{
$entity = $this->prepareRowInstance($record);
$this->save($entity);
return $entity;
}
public function save($entity)
{
$entityClass = $this->entityClass();
if (!$entity instanceof $entityClass) {
throw new \Exception('Entity should be an instance of `' . $entityClass . '`.');
}
$record = [];
(function () use (&$record) {
$record = get_object_vars($this);
})->call($entity);
switch ($this->getEntityState($entity)) {
case self::STATE_NEW:
$this->insert($record);
if ($autoIncrement = $this->autoIncrement()) {
$id = (int) $this->connection()->lastInsertId();
(function () use ($autoIncrement, $id) {
$this->{$autoIncrement} = $id;
})->call($entity);
}
$this->setEntityState($entity, self::STATE_MANAGED);
break;
case self::STATE_MANAGED:
$keys = [];
foreach ($this->firstUniqueKey() as $key) {
$keys[$key] = $record[$key];
}
$query = $this->newUpdateQuery()
->setMultiple(array_diff_key($record, $keys))
->whereMultiple($keys);
$this->connection()->sqlExecute(...$query->toSql());
break;
}
return $this;
}
private function setEntityState($entity, $state)
{
$this->entityStates[spl_object_hash($entity)] = $state;
return $this;
}
private function getEntityState($entity)
{
return $this->entityStates[spl_object_hash($entity)] ?? self::STATE_NEW;
}
protected function prepareRowInstance(array $record)
{
$reflection = $this->getReflectionClass($this->entityClass());
$entity = $reflection->newInstanceWithoutConstructor();
(function () use ($record, $reflection) {
foreach ($record as $key => $value) {
if ($reflection->hasProperty($key)) {
$this->{$key} = $value;
}
}
})->call($entity);
if (method_exists($entity, '__construct')) {
$entity->__construct();
}
return $entity;
}
protected function prepareRowsInstance(array $records)
{
$entities = [];
foreach ($records as $record) {
$entities[] = $this->prepareRowInstance($record);
}
return $entities;
}
private function getReflectionClass($className): \ReflectionClass
{
if (interface_exists($className)) {
throw new \InvalidArgumentException(
sprintf('The provided type "%s" is an interface, and can not be instantiated', $className)
);
}
if (!class_exists($className)) {
throw new \InvalidArgumentException(sprintf('The provided class "%s" does not exist', $className));
}
$reflection = new \ReflectionClass($className);
if ($reflection->isAbstract()) {
throw new \InvalidArgumentException(sprintf('The provided class "%s" is abstract, and can not be instantiated', $reflection->getName()));
}
return $reflection;
}
}