@@ -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; |
@@ -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,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)(); |