|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Db\TableGateway; |
|
12
|
|
|
|
|
13
|
|
|
use Drone\Db\Entity; |
|
14
|
|
|
use Drone\Db\SQLFunction; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* EntityAdapter class |
|
18
|
|
|
* |
|
19
|
|
|
* This class allows to persist objects to database (Data Mapper pattern) |
|
20
|
|
|
*/ |
|
21
|
|
|
class EntityAdapter |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The gateway linked |
|
25
|
|
|
* |
|
26
|
|
|
* @var TableGateway $tableGateway |
|
27
|
|
|
*/ |
|
28
|
|
|
private $tableGateway; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param TableGateway $tableGateway |
|
34
|
|
|
*/ |
|
35
|
6 |
|
public function __construct(TableGateway $tableGateway) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$this->tableGateway = $tableGateway; |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the tableGateway |
|
42
|
|
|
* |
|
43
|
|
|
* @return TableGateway |
|
44
|
|
|
*/ |
|
45
|
6 |
|
public function getTableGateway() |
|
46
|
|
|
{ |
|
47
|
6 |
|
return $this->tableGateway; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns a rowset with entity instances |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $where |
|
54
|
|
|
* |
|
55
|
|
|
* @return Entity[] |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function select(array $where) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$result = $this->tableGateway->select($where); |
|
60
|
|
|
|
|
61
|
1 |
|
if (!count($result)) { |
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
$array_result = []; |
|
66
|
|
|
|
|
67
|
1 |
|
foreach ($result as $row) { |
|
68
|
1 |
|
$filtered_array = []; |
|
69
|
|
|
|
|
70
|
1 |
|
foreach ($row as $key => $value) { |
|
71
|
1 |
|
if (is_string($key)) { |
|
72
|
1 |
|
$filtered_array[$key] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$user_entity = get_class($this->tableGateway->getEntity()); |
|
77
|
|
|
|
|
78
|
1 |
|
$entity = new $user_entity($filtered_array); |
|
79
|
|
|
|
|
80
|
1 |
|
$array_result[] = $entity; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $array_result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Creates a row from an entity or array |
|
88
|
|
|
* |
|
89
|
|
|
* @param Entity|array $entity |
|
90
|
|
|
* |
|
91
|
|
|
* @throws InvalidArgumentException |
|
92
|
|
|
* |
|
93
|
|
|
* @return resource|object |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function insert($entity) |
|
96
|
|
|
{ |
|
97
|
1 |
|
if ($entity instanceof Entity) { |
|
98
|
1 |
|
$entity = get_object_vars($entity); |
|
99
|
|
|
} elseif (!is_array($entity)) { |
|
|
|
|
|
|
100
|
|
|
throw new \InvalidArgumentException("Invalid type given. Drone\Db\Entity or Array expected"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$this->parseEntity($entity); |
|
104
|
|
|
|
|
105
|
1 |
|
return $this->tableGateway->insert($entity); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Updates an entity |
|
110
|
|
|
* |
|
111
|
|
|
* @param Entity|array $entity |
|
112
|
|
|
* @param array $where |
|
113
|
|
|
* |
|
114
|
|
|
* @throws RuntimeException from internal execute() |
|
115
|
|
|
* @throws InvalidArgumentException |
|
116
|
|
|
* |
|
117
|
|
|
* @return object|resource |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function update($entity, $where) |
|
120
|
|
|
{ |
|
121
|
1 |
|
if ($entity instanceof Entity) { |
|
122
|
1 |
|
$changedFields = $entity->getChangedFields(); |
|
123
|
1 |
|
$entity = get_object_vars($entity); |
|
124
|
|
|
|
|
125
|
1 |
|
$fieldsToModify = []; |
|
126
|
|
|
|
|
127
|
1 |
|
foreach ($entity as $key => $value) { |
|
128
|
1 |
|
if (in_array($key, $changedFields)) { |
|
129
|
1 |
|
$fieldsToModify[$key] = $value; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
$entity = $fieldsToModify; |
|
134
|
|
|
} elseif (!is_array($entity)) { |
|
|
|
|
|
|
135
|
|
|
throw new \InvalidArgumentException("Invalid type given. Drone\Db\Entity or Array expected"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
$this->parseEntity($entity); |
|
139
|
|
|
|
|
140
|
1 |
|
return $this->tableGateway->update($entity, $where); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Deletes an entity |
|
145
|
|
|
* |
|
146
|
|
|
* @param Entity|array $entity |
|
147
|
|
|
* |
|
148
|
|
|
* @throws RuntimeException from internal execute() |
|
149
|
|
|
* @throws InvalidArgumentException |
|
150
|
|
|
* |
|
151
|
|
|
* @return object|resource |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function delete($entity) |
|
154
|
|
|
{ |
|
155
|
1 |
|
if ($entity instanceof Entity) { |
|
156
|
|
|
$entity = get_object_vars($entity); |
|
157
|
1 |
|
} elseif (!is_array($entity)) { |
|
|
|
|
|
|
158
|
|
|
throw new \InvalidArgumentException("Invalid type given. Drone\Db\Entity or Array expected"); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return $this->tableGateway->delete($entity); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Converts several objects to SQLFunction objects |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $entity |
|
168
|
|
|
* |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
2 |
|
private function parseEntity(array &$entity) |
|
172
|
|
|
{ |
|
173
|
2 |
|
$drv = $this->getTableGateway()->getDb()->getDriverName(); |
|
174
|
|
|
|
|
175
|
2 |
|
foreach ($entity as $field => $value) { |
|
176
|
2 |
|
if ($value instanceof \DateTime) { |
|
177
|
|
|
switch ($drv) { |
|
178
|
|
|
case 'Oci8': |
|
179
|
|
|
$entity[$field] = new SQLFunction('TO_DATE', [$value->format('Y-m-d'), 'YYYY-MM-DD']); |
|
180
|
|
|
break; |
|
181
|
|
|
case 'Mysqli': |
|
182
|
|
|
$entity[$field] = new SQLFunction('STR_TO_DATE', [$value->format('Y-m-d'), '%Y-%m-%d']); |
|
183
|
|
|
break; |
|
184
|
|
|
case 'Sqlsrv': |
|
185
|
|
|
$entity[$field] = new SQLFunction('CONVERT', ['DATETIME', $value->format('Y-m-d')]); |
|
186
|
2 |
|
break; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
return $entity; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|