@@ -2,55 +2,55 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class GeneralEditDatabase { |
5 | - private $pdo; |
|
6 | - private $dataTypes = [ |
|
7 | - 'datetime' => 'DATETIME', |
|
8 | - 'int' => 'INT(11)', |
|
9 | - 'decimal' => 'DECIMAL', |
|
10 | - 'short_string' => 'VARCHAR', |
|
11 | - 'long_string' => 'LONGBLOG', |
|
12 | - 'short_string_max_len' => 255, |
|
13 | - 'other' => 'VARCHAR(255)', |
|
5 | + private $pdo; |
|
6 | + private $dataTypes = [ |
|
7 | + 'datetime' => 'DATETIME', |
|
8 | + 'int' => 'INT(11)', |
|
9 | + 'decimal' => 'DECIMAL', |
|
10 | + 'short_string' => 'VARCHAR', |
|
11 | + 'long_string' => 'LONGBLOG', |
|
12 | + 'short_string_max_len' => 255, |
|
13 | + 'other' => 'VARCHAR(255)', |
|
14 | 14 | |
15 | - 'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT' |
|
16 | - ]; |
|
15 | + 'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT' |
|
16 | + ]; |
|
17 | 17 | |
18 | - public function __construct(\PDO $pdo, array $dataTypes) { |
|
19 | - $this->pdo = $pdo; |
|
20 | - $this->dataTypes = array_merge($this->dataTypes, $dataTypes); |
|
21 | - } |
|
18 | + public function __construct(\PDO $pdo, array $dataTypes) { |
|
19 | + $this->pdo = $pdo; |
|
20 | + $this->dataTypes = array_merge($this->dataTypes, $dataTypes); |
|
21 | + } |
|
22 | 22 | |
23 | - public function quote($str) { |
|
23 | + public function quote($str) { |
|
24 | 24 | return '`' . str_replace('.', '`.`', trim($str, '`')) . '`'; |
25 | 25 | } |
26 | 26 | |
27 | - public function getType($val) { |
|
27 | + public function getType($val) { |
|
28 | 28 | if ($val instanceof \DateTimeInterface) return $this->dataTypes['datetime']; |
29 | 29 | else if ($result = $this->doNumberTypes($val)) return $result; |
30 | 30 | else if ($result = $this->doStringTypes($val)) return $result; |
31 | 31 | else return $this->dataTypes['other']; |
32 | 32 | } |
33 | 33 | |
34 | - private function doNumberTypes($val) { |
|
35 | - if (is_int($val)) return $this->dataTypes['int']; |
|
34 | + private function doNumberTypes($val) { |
|
35 | + if (is_int($val)) return $this->dataTypes['int']; |
|
36 | 36 | else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . (strlen($val) - strrpos($val, '.') - 1) . ')'; |
37 | - else return false; |
|
38 | - } |
|
37 | + else return false; |
|
38 | + } |
|
39 | 39 | |
40 | - private function doStringTypes($val) { |
|
41 | - if (!is_string($val)) return false; |
|
42 | - if (strlen($val) <= $this->dataTypes['short_string_max_len']) |
|
43 | - return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
40 | + private function doStringTypes($val) { |
|
41 | + if (!is_string($val)) return false; |
|
42 | + if (strlen($val) <= $this->dataTypes['short_string_max_len']) |
|
43 | + return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
44 | 44 | else return $this->dataTypes['long_string']; |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | - public function isNotSavableType($value, $key, $primaryKey) { |
|
48 | - return is_array($value) || (is_object($value) && !($value instanceof \DateTimeInterface)) || |
|
49 | - in_array($key, $primaryKey); |
|
50 | - } |
|
47 | + public function isNotSavableType($value, $key, $primaryKey) { |
|
48 | + return is_array($value) || (is_object($value) && !($value instanceof \DateTimeInterface)) || |
|
49 | + in_array($key, $primaryKey); |
|
50 | + } |
|
51 | 51 | |
52 | - //Alter the database so that it can store $data |
|
53 | - public function createTable($table, array $primaryKey, $data) { |
|
52 | + //Alter the database so that it can store $data |
|
53 | + public function createTable($table, array $primaryKey, $data) { |
|
54 | 54 | $parts = []; |
55 | 55 | foreach ($primaryKey as $key) { |
56 | 56 | $pk = $data->$key; |
@@ -25,23 +25,36 @@ discard block |
||
25 | 25 | } |
26 | 26 | |
27 | 27 | public function getType($val) { |
28 | - if ($val instanceof \DateTimeInterface) return $this->dataTypes['datetime']; |
|
29 | - else if ($result = $this->doNumberTypes($val)) return $result; |
|
30 | - else if ($result = $this->doStringTypes($val)) return $result; |
|
31 | - else return $this->dataTypes['other']; |
|
28 | + if ($val instanceof \DateTimeInterface) { |
|
29 | + return $this->dataTypes['datetime']; |
|
30 | + } else if ($result = $this->doNumberTypes($val)) { |
|
31 | + return $result; |
|
32 | + } else if ($result = $this->doStringTypes($val)) { |
|
33 | + return $result; |
|
34 | + } else { |
|
35 | + return $this->dataTypes['other']; |
|
36 | + } |
|
32 | 37 | } |
33 | 38 | |
34 | 39 | private function doNumberTypes($val) { |
35 | - if (is_int($val)) return $this->dataTypes['int']; |
|
36 | - else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . (strlen($val) - strrpos($val, '.') - 1) . ')'; |
|
37 | - else return false; |
|
40 | + if (is_int($val)) { |
|
41 | + return $this->dataTypes['int']; |
|
42 | + } else if (is_double($val)) { |
|
43 | + return $this->dataTypes['decimal'] . '(9,' . (strlen($val) - strrpos($val, '.') - 1) . ')'; |
|
44 | + } else { |
|
45 | + return false; |
|
46 | + } |
|
38 | 47 | } |
39 | 48 | |
40 | 49 | private function doStringTypes($val) { |
41 | - if (!is_string($val)) return false; |
|
42 | - if (strlen($val) <= $this->dataTypes['short_string_max_len']) |
|
43 | - return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
44 | - else return $this->dataTypes['long_string']; |
|
50 | + if (!is_string($val)) { |
|
51 | + return false; |
|
52 | + } |
|
53 | + if (strlen($val) <= $this->dataTypes['short_string_max_len']) { |
|
54 | + return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
55 | + } else { |
|
56 | + return $this->dataTypes['long_string']; |
|
57 | + } |
|
45 | 58 | } |
46 | 59 | |
47 | 60 | public function isNotSavableType($value, $key, $primaryKey) { |
@@ -54,8 +67,11 @@ discard block |
||
54 | 67 | $parts = []; |
55 | 68 | foreach ($primaryKey as $key) { |
56 | 69 | $pk = $data->$key; |
57 | - if ($pk == null) $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
58 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
70 | + if ($pk == null) { |
|
71 | + $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
72 | + } else { |
|
73 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
74 | + } |
|
59 | 75 | } |
60 | 76 | |
61 | 77 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -20,11 +20,11 @@ discard block |
||
20 | 20 | //For dates with times set, search on time, if the time is not set, search on date only. |
21 | 21 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
22 | 22 | if ($value instanceof \DateTimeInterface) { |
23 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
23 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
24 | 24 | else $value = $value->format('Y-m-d H:i:s'); |
25 | 25 | } |
26 | 26 | if (is_object($value)) continue; |
27 | - if ($prependField){ |
|
27 | + if ($prependField) { |
|
28 | 28 | $sql[] = $this->quote($field) . ' = :' . $field; |
29 | 29 | } else { |
30 | 30 | $sql[] = ':' . $field; |
@@ -36,13 +36,13 @@ discard block |
||
36 | 36 | |
37 | 37 | public function insert($table, $data) { |
38 | 38 | $query = $this->buildSaveQuery($data); |
39 | - return new Query('INSERT INTO ' . $this->quote($table) . ' (' .implode(', ', array_keys($query['args'])).') VALUES ( ' . implode(', ', $query['sql']). ' )', $query['args']); |
|
39 | + return new Query('INSERT INTO ' . $this->quote($table) . ' (' . implode(', ', array_keys($query['args'])) . ') VALUES ( ' . implode(', ', $query['sql']) . ' )', $query['args']); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | public function update($table, array $primaryKey, $data) { |
43 | 43 | $query = $this->buildSaveQuery($data, true); |
44 | 44 | $where = []; |
45 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
46 | - return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
|
45 | + foreach ($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
46 | + return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']) . ' WHERE ' . implode(' AND ', $where), $query['args']); |
|
47 | 47 | } |
48 | 48 | } |
@@ -20,10 +20,15 @@ discard block |
||
20 | 20 | //For dates with times set, search on time, if the time is not set, search on date only. |
21 | 21 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
22 | 22 | if ($value instanceof \DateTimeInterface) { |
23 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
24 | - else $value = $value->format('Y-m-d H:i:s'); |
|
23 | + if ($value->format('H:i:s') == '00:00:00') { |
|
24 | + $value = $value->format('Y-m-d'); |
|
25 | + } else { |
|
26 | + $value = $value->format('Y-m-d H:i:s'); |
|
27 | + } |
|
28 | + } |
|
29 | + if (is_object($value)) { |
|
30 | + continue; |
|
25 | 31 | } |
26 | - if (is_object($value)) continue; |
|
27 | 32 | if ($prependField){ |
28 | 33 | $sql[] = $this->quote($field) . ' = :' . $field; |
29 | 34 | } else { |
@@ -42,7 +47,9 @@ discard block |
||
42 | 47 | public function update($table, array $primaryKey, $data) { |
43 | 48 | $query = $this->buildSaveQuery($data, true); |
44 | 49 | $where = []; |
45 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
50 | + foreach($primaryKey as $field) { |
|
51 | + $where[] = $this->quote($field) . ' = :' . $field; |
|
52 | + } |
|
46 | 53 | return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
47 | 54 | } |
48 | 55 | } |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | return $obj; |
19 | 19 | } |
20 | 20 | |
21 | - private function tryToGetDateObjFromString($obj) { |
|
22 | - try { |
|
23 | - $date = new \DateTimeImmutable($obj); |
|
21 | + private function tryToGetDateObjFromString($obj) { |
|
22 | + try { |
|
23 | + $date = new \DateTimeImmutable($obj); |
|
24 | 24 | if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
25 | - } |
|
26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | - } |
|
28 | - return $obj; |
|
29 | - } |
|
25 | + } |
|
26 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | + } |
|
28 | + return $obj; |
|
29 | + } |
|
30 | 30 | |
31 | 31 | private function dateMatchesFormats($date, $str) { |
32 | 32 | foreach ($this->dateFormats as list($format, $len)) { |
@@ -35,17 +35,17 @@ discard block |
||
35 | 35 | return false; |
36 | 36 | } |
37 | 37 | |
38 | - private function isIterable($obj) { |
|
39 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | - } |
|
38 | + private function isIterable($obj) { |
|
39 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | + } |
|
41 | 41 | |
42 | - private function isPossiblyDateString($obj) { |
|
43 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | - } |
|
42 | + private function isPossiblyDateString($obj) { |
|
43 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | private function checkCache($obj, $reset) { |
47 | 47 | if ($reset) $this->processCache = new \SplObjectStorage(); |
48 | - if (!is_object($obj)) return false; |
|
48 | + if (!is_object($obj)) return false; |
|
49 | 49 | |
50 | 50 | if ($this->processCache->contains($obj)) return $obj; |
51 | 51 | else $this->processCache->attach($obj, true); |
@@ -11,26 +11,35 @@ discard block |
||
11 | 11 | |
12 | 12 | public function replaceDates($obj, $reset = true) { |
13 | 13 | //prevent infinite recursion, only process each object once |
14 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
14 | + if ($this->checkCache($obj, $reset)) { |
|
15 | + return $obj; |
|
16 | + } |
|
15 | 17 | |
16 | - if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
17 | - if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj); |
|
18 | + if ($this->isIterable($obj)) { |
|
19 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
20 | + } |
|
21 | + if ($this->isPossiblyDateString($obj)) { |
|
22 | + $obj = $this->tryToGetDateObjFromString($obj); |
|
23 | + } |
|
18 | 24 | return $obj; |
19 | 25 | } |
20 | 26 | |
21 | 27 | private function tryToGetDateObjFromString($obj) { |
22 | 28 | try { |
23 | 29 | $date = new \DateTimeImmutable($obj); |
24 | - if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
|
25 | - } |
|
26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
30 | + if ($this->dateMatchesFormats($date, $obj)) { |
|
31 | + $obj = $date; |
|
32 | + } |
|
33 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | 34 | } |
28 | 35 | return $obj; |
29 | 36 | } |
30 | 37 | |
31 | 38 | private function dateMatchesFormats($date, $str) { |
32 | 39 | foreach ($this->dateFormats as list($format, $len)) { |
33 | - if ($date->format($format) == substr($str, 0, $len)) return true; |
|
40 | + if ($date->format($format) == substr($str, 0, $len)) { |
|
41 | + return true; |
|
42 | + } |
|
34 | 43 | } |
35 | 44 | return false; |
36 | 45 | } |
@@ -44,11 +53,18 @@ discard block |
||
44 | 53 | } |
45 | 54 | |
46 | 55 | private function checkCache($obj, $reset) { |
47 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
48 | - if (!is_object($obj)) return false; |
|
56 | + if ($reset) { |
|
57 | + $this->processCache = new \SplObjectStorage(); |
|
58 | + } |
|
59 | + if (!is_object($obj)) { |
|
60 | + return false; |
|
61 | + } |
|
49 | 62 | |
50 | - if ($this->processCache->contains($obj)) return $obj; |
|
51 | - else $this->processCache->attach($obj, true); |
|
63 | + if ($this->processCache->contains($obj)) { |
|
64 | + return $obj; |
|
65 | + } else { |
|
66 | + $this->processCache->attach($obj, true); |
|
67 | + } |
|
52 | 68 | |
53 | 69 | return false; |
54 | 70 | } |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | 12 | } |
13 | 13 | else { |
14 | - $visOverride = $this; |
|
14 | + $visOverride = $this; |
|
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function isReturnableDataType($v) { |
|
27 | - return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
28 | - } |
|
26 | + public function isReturnableDataType($v) { |
|
27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | public function getProperties() { |
31 | 31 | return ($this->readClosure)(); |