@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | * @param string $name |
43 | 43 | * @param string[] $columns |
44 | 44 | */ |
45 | - public function __construct (DB $db, string $name, array $columns) { |
|
45 | + public function __construct(DB $db, string $name, array $columns) { |
|
46 | 46 | parent::__construct($db); |
47 | 47 | $this->name = $name; |
48 | 48 | foreach ($columns as $column) { |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * |
56 | 56 | * @return string |
57 | 57 | */ |
58 | - final public function __toString () { |
|
58 | + final public function __toString() { |
|
59 | 59 | return $this->name; |
60 | 60 | } |
61 | 61 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | * @param array $values |
66 | 66 | * @return int Rows affected. |
67 | 67 | */ |
68 | - public function apply (array $values): int { |
|
68 | + public function apply(array $values): int { |
|
69 | 69 | $columns = implode(',', array_keys($values)); |
70 | 70 | $values = $this->db->quoteList($values); |
71 | 71 | if ($this->db->isSQLite()) { |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @param Closure $prepare `():Statement` |
86 | 86 | * @return Statement |
87 | 87 | */ |
88 | - protected function cache (string $key, Closure $prepare) { |
|
88 | + protected function cache(string $key, Closure $prepare) { |
|
89 | 89 | return $this->_cache[$key] ??= $prepare->__invoke(); |
90 | 90 | } |
91 | 91 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | * @param array $match `[a => b]` |
94 | 94 | * @return int |
95 | 95 | */ |
96 | - public function count (array $match = []) { |
|
96 | + public function count(array $match = []) { |
|
97 | 97 | $select = $this->select(['COUNT(*)']); |
98 | 98 | foreach ($match as $a => $b) { |
99 | 99 | $select->where($this->db->match($this[$a] ?? $a, $b)); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * @param array $match |
110 | 110 | * @return int Rows affected. |
111 | 111 | */ |
112 | - public function delete (array $match): int { |
|
112 | + public function delete(array $match): int { |
|
113 | 113 | foreach ($match as $a => $b) { |
114 | 114 | $match[$a] = $this->db->match($this[$a] ?? $a, $b); |
115 | 115 | } |
@@ -120,14 +120,14 @@ discard block |
||
120 | 120 | /** |
121 | 121 | * @return Column[] |
122 | 122 | */ |
123 | - final public function getColumns (): array { |
|
123 | + final public function getColumns(): array { |
|
124 | 124 | return $this->columns; |
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | 128 | * @return string |
129 | 129 | */ |
130 | - final public function getName (): string { |
|
130 | + final public function getName(): string { |
|
131 | 131 | return $this->name; |
132 | 132 | } |
133 | 133 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | * @param array $values |
138 | 138 | * @return Statement |
139 | 139 | */ |
140 | - public function insert (array $values) { |
|
140 | + public function insert(array $values) { |
|
141 | 141 | $columns = implode(',', array_keys($values)); |
142 | 142 | $values = $this->db->quoteList($values); |
143 | 143 | return $this->db->query("INSERT INTO {$this} ($columns) VALUES ($values)"); |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | * @param int|string $column |
148 | 148 | * @return Column |
149 | 149 | */ |
150 | - public function offsetGet ($column) { |
|
150 | + public function offsetGet($column) { |
|
151 | 151 | if (is_int($column)) { |
152 | 152 | return current(array_slice($this->columns, $column, 1)) ?: null; |
153 | 153 | } |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * @param string[] $columns Defaults to all columns. |
161 | 161 | * @return Select|array[] |
162 | 162 | */ |
163 | - public function select (array $columns = []) { |
|
163 | + public function select(array $columns = []) { |
|
164 | 164 | if (empty($columns)) { |
165 | 165 | $columns = $this->columns; |
166 | 166 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @param string $name |
174 | 174 | * @return Table |
175 | 175 | */ |
176 | - public function setName (string $name) { |
|
176 | + public function setName(string $name) { |
|
177 | 177 | $clone = clone $this; |
178 | 178 | $clone->name = $name; |
179 | 179 | foreach ($this->columns as $name => $column) { |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * @param array $match |
192 | 192 | * @return int Rows affected. |
193 | 193 | */ |
194 | - public function update (array $values, array $match): int { |
|
194 | + public function update(array $values, array $match): int { |
|
195 | 195 | foreach ($this->db->quoteArray($values) as $key => $value) { |
196 | 196 | $values[$key] = "{$key} = {$value}"; |
197 | 197 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * @param string|EntityInterface $class |
67 | 67 | * @return Record |
68 | 68 | */ |
69 | - public static function fromClass (DB $db, $class) { |
|
69 | + public static function fromClass(DB $db, $class) { |
|
70 | 70 | return (function() use ($db, $class) { |
71 | 71 | $rClass = new ReflectionClass($class); |
72 | 72 | $columns = []; |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @param string[] $columns Property names. |
96 | 96 | * @param EAV[] $eav Keyed by property name. |
97 | 97 | */ |
98 | - public function __construct (DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) { |
|
98 | + public function __construct(DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) { |
|
99 | 99 | parent::__construct($db, $table, $columns); |
100 | 100 | $this->proto = $proto; |
101 | 101 | (function() use ($proto, $columns, $eav) { |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return Table |
130 | 130 | */ |
131 | - public function asTable () { |
|
131 | + public function asTable() { |
|
132 | 132 | return $this->db->getTable($this->name); |
133 | 133 | } |
134 | 134 | |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | * @param array[] $eavMatch `[eav property => attribute => mixed]` |
142 | 142 | * @return Select |
143 | 143 | */ |
144 | - public function find (array $match, array $eavMatch = []) { |
|
144 | + public function find(array $match, array $eavMatch = []) { |
|
145 | 145 | $select = $this->select(); |
146 | 146 | foreach ($match as $a => $b) { |
147 | 147 | $select->where($this->db->match($this[$a] ?? $a, $b)); |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | * @param Statement $statement |
160 | 160 | * @return EntityInterface[] Keyed by ID |
161 | 161 | */ |
162 | - public function getAll (Statement $statement): array { |
|
162 | + public function getAll(Statement $statement): array { |
|
163 | 163 | return iterator_to_array($this->getEach($statement)); |
164 | 164 | } |
165 | 165 | |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | * @param Statement $statement |
171 | 171 | * @return Generator|EntityInterface[] Keyed by ID |
172 | 172 | */ |
173 | - public function getEach (Statement $statement) { |
|
173 | + public function getEach(Statement $statement) { |
|
174 | 174 | do { |
175 | 175 | $entities = []; |
176 | 176 | for ($i = 0; $i < 256 and false !== $row = $statement->fetch(); $i++) { |
@@ -187,14 +187,14 @@ discard block |
||
187 | 187 | * @param string $property |
188 | 188 | * @return EAV |
189 | 189 | */ |
190 | - final public function getEav (string $property) { |
|
190 | + final public function getEav(string $property) { |
|
191 | 191 | return $this->eav[$property]; |
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
195 | 195 | * @return EntityInterface |
196 | 196 | */ |
197 | - public function getProto () { |
|
197 | + public function getProto() { |
|
198 | 198 | return $this->proto; |
199 | 199 | } |
200 | 200 | |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | * @param EntityInterface $entity |
203 | 203 | * @return array |
204 | 204 | */ |
205 | - protected function getValues (EntityInterface $entity): array { |
|
205 | + protected function getValues(EntityInterface $entity): array { |
|
206 | 206 | $values = []; |
207 | 207 | foreach (array_keys($this->columns) as $name) { |
208 | 208 | $values[$name] = $this->properties[$name]->getValue($entity); |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | * @param int $id |
217 | 217 | * @return null|EntityInterface |
218 | 218 | */ |
219 | - public function load (int $id) { |
|
219 | + public function load(int $id) { |
|
220 | 220 | $statement = $this->cache(__FUNCTION__, function() { |
221 | 221 | return $this->select(array_keys($this->columns))->where('id = ?')->prepare(); |
222 | 222 | }); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | * |
237 | 237 | * @param EntityInterface[] $entities |
238 | 238 | */ |
239 | - protected function loadEav (array $entities): void { |
|
239 | + protected function loadEav(array $entities): void { |
|
240 | 240 | $ids = array_keys($entities); |
241 | 241 | foreach ($this->eav as $name => $eav) { |
242 | 242 | foreach ($eav->loadAll($ids) as $id => $values) { |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * @param EntityInterface $entity |
252 | 252 | * @return int ID |
253 | 253 | */ |
254 | - public function save (EntityInterface $entity): int { |
|
254 | + public function save(EntityInterface $entity): int { |
|
255 | 255 | if (!$entity->getId()) { |
256 | 256 | $this->saveInsert($entity); |
257 | 257 | } |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | /** |
266 | 266 | * @param EntityInterface $entity |
267 | 267 | */ |
268 | - protected function saveEav (EntityInterface $entity): void { |
|
268 | + protected function saveEav(EntityInterface $entity): void { |
|
269 | 269 | $id = $entity->getId(); |
270 | 270 | foreach ($this->eav as $name => $eav) { |
271 | 271 | // may be null to skip |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | * |
282 | 282 | * @param EntityInterface $entity |
283 | 283 | */ |
284 | - protected function saveInsert (EntityInterface $entity): void { |
|
284 | + protected function saveInsert(EntityInterface $entity): void { |
|
285 | 285 | $statement = $this->cache(__FUNCTION__, function() { |
286 | 286 | $slots = SQL::slots(array_keys($this->columns)); |
287 | 287 | unset($slots['id']); |
@@ -300,7 +300,7 @@ discard block |
||
300 | 300 | * |
301 | 301 | * @param EntityInterface $entity |
302 | 302 | */ |
303 | - protected function saveUpdate (EntityInterface $entity): void { |
|
303 | + protected function saveUpdate(EntityInterface $entity): void { |
|
304 | 304 | $statement = $this->cache(__FUNCTION__, function() { |
305 | 305 | $slots = SQL::slotsEqual(array_keys($this->columns)); |
306 | 306 | unset($slots['id']); |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | * @param array $columns Defaults to all columns. |
318 | 318 | * @return Select|EntityInterface[] |
319 | 319 | */ |
320 | - public function select (array $columns = []) { |
|
320 | + public function select(array $columns = []) { |
|
321 | 321 | return parent::select($columns)->setFetcher(function(Statement $statement) { |
322 | 322 | yield from $this->getEach($statement); |
323 | 323 | }); |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | * @param EntityInterface $proto |
328 | 328 | * @return $this |
329 | 329 | */ |
330 | - public function setProto (EntityInterface $proto) { |
|
330 | + public function setProto(EntityInterface $proto) { |
|
331 | 331 | $this->proto = $proto; |
332 | 332 | return $this; |
333 | 333 | } |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | * @param EntityInterface $entity |
337 | 337 | * @param array $values |
338 | 338 | */ |
339 | - protected function setValues (EntityInterface $entity, array $values): void { |
|
339 | + protected function setValues(EntityInterface $entity, array $values): void { |
|
340 | 340 | foreach ($values as $name => $value) { |
341 | 341 | if (isset($this->properties[$name])) { |
342 | 342 | settype($value, $this->types[$name]); |
@@ -75,8 +75,7 @@ discard block |
||
75 | 75 | foreach ($rClass->getProperties() as $rProp) { |
76 | 76 | if (preg_match('/@col(umn)?[\s$]/', $rProp->getDocComment())) { |
77 | 77 | $columns[] = $rProp->getName(); |
78 | - } |
|
79 | - elseif (preg_match('/@eav\s+(?<table>\S+)/', $rProp->getDocComment(), $attr)) { |
|
78 | + } elseif (preg_match('/@eav\s+(?<table>\S+)/', $rProp->getDocComment(), $attr)) { |
|
80 | 79 | $eav[$rProp->getName()] = EAV::factory($db, $attr['table']); |
81 | 80 | } |
82 | 81 | } |
@@ -254,8 +253,7 @@ discard block |
||
254 | 253 | public function save (EntityInterface $entity): int { |
255 | 254 | if (!$entity->getId()) { |
256 | 255 | $this->saveInsert($entity); |
257 | - } |
|
258 | - else { |
|
256 | + } else { |
|
259 | 257 | $this->saveUpdate($entity); |
260 | 258 | } |
261 | 259 | $this->saveEav($entity); |
@@ -341,8 +339,7 @@ discard block |
||
341 | 339 | if (isset($this->properties[$name])) { |
342 | 340 | settype($value, $this->types[$name]); |
343 | 341 | $this->properties[$name]->setValue($entity, $value); |
344 | - } |
|
345 | - else { |
|
342 | + } else { |
|
346 | 343 | $entity->{$name} = $value; |
347 | 344 | } |
348 | 345 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param string $name |
42 | 42 | * @param string $qualifier |
43 | 43 | */ |
44 | - public function __construct (DB $db, string $name, string $qualifier = '') { |
|
44 | + public function __construct(DB $db, string $name, string $qualifier = '') { |
|
45 | 45 | $this->db = $db; |
46 | 46 | $this->name = $name; |
47 | 47 | $this->qualifier = $qualifier; |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return string |
54 | 54 | */ |
55 | - public function __toString (): string { |
|
55 | + public function __toString(): string { |
|
56 | 56 | if (strlen($this->qualifier)) { |
57 | 57 | return "{$this->qualifier}.{$this->name}"; |
58 | 58 | } |
@@ -62,14 +62,14 @@ discard block |
||
62 | 62 | /** |
63 | 63 | * @return string |
64 | 64 | */ |
65 | - final public function getName (): string { |
|
65 | + final public function getName(): string { |
|
66 | 66 | return $this->name; |
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
70 | 70 | * @return string |
71 | 71 | */ |
72 | - final public function getQualifier (): string { |
|
72 | + final public function getQualifier(): string { |
|
73 | 73 | return $this->qualifier; |
74 | 74 | } |
75 | 75 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return Select|scalar[] |
80 | 80 | */ |
81 | - public function select () { |
|
81 | + public function select() { |
|
82 | 82 | return Select::factory($this->db, $this->qualifier, [$this->name]) |
83 | 83 | ->setFetcher(function(Statement $statement) { |
84 | 84 | while (false !== $value = $statement->fetchColumn()) { |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @param string $name |
92 | 92 | * @return $this |
93 | 93 | */ |
94 | - public function setName (string $name) { |
|
94 | + public function setName(string $name) { |
|
95 | 95 | $clone = clone $this; |
96 | 96 | $clone->name = $name; |
97 | 97 | return $clone; |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * @param string $qualifier |
102 | 102 | * @return $this |
103 | 103 | */ |
104 | - public function setQualifier (string $qualifier) { |
|
104 | + public function setQualifier(string $qualifier) { |
|
105 | 105 | $clone = clone $this; |
106 | 106 | $clone->qualifier = $qualifier; |
107 | 107 | return $clone; |