@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | private $siblings = []; |
10 | 10 | |
11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
12 | + if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
13 | 13 | $this->mapper = $mapper; |
14 | 14 | $this->parentField = $parentField; |
15 | 15 | $this->localField = $localField; |
@@ -48,17 +48,17 @@ discard block |
||
48 | 48 | //Fetch the results so they're in the cache for the corresponding maphper object |
49 | 49 | $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
50 | 50 | |
51 | - $this->loadDataIntoSiblings($results); |
|
51 | + $this->loadDataIntoSiblings($results); |
|
52 | 52 | } |
53 | 53 | |
54 | - private function loadDataIntoSiblings($results) { |
|
55 | - $cache = []; |
|
54 | + private function loadDataIntoSiblings($results) { |
|
55 | + $cache = []; |
|
56 | 56 | foreach ($results as $result) { |
57 | 57 | $cache[$result->{$this->localField}] = $result; |
58 | 58 | } |
59 | 59 | |
60 | 60 | foreach ($this->siblings as $sibling) { |
61 | - if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
61 | + if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
62 | 62 | } |
63 | 63 | /* |
64 | 64 | foreach ($this->siblings as $sibling) { |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0); |
67 | 67 | } |
68 | 68 | */ |
69 | - } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | public function __call($func, array $args = []) { |
72 | 72 | if ($this->lazyLoad() == null) return ''; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | public function __get($name) { |
77 | 77 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
78 | - else return null; |
|
78 | + else return null; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | public function __isset($name) { |
@@ -83,9 +83,9 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | public function overwrite($parentObject, &$data) { |
86 | - $this->mapper[] = $data; |
|
86 | + $this->mapper[] = $data; |
|
87 | 87 | |
88 | - if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
88 | + if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
89 | 89 | $parentObject->{$this->parentField} = $data->{$this->localField}; |
90 | 90 | //Trigger an update of the parent object |
91 | 91 | return true; |
@@ -9,7 +9,9 @@ discard block |
||
9 | 9 | private $siblings = []; |
10 | 10 | |
11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
12 | + if ($criteria) { |
|
13 | + $mapper = $mapper->filter($this->criteira); |
|
14 | + } |
|
13 | 15 | $this->mapper = $mapper; |
14 | 16 | $this->parentField = $parentField; |
15 | 17 | $this->localField = $localField; |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | private function lazyLoad() { |
30 | 32 | if (!isset($this->data)) { |
31 | 33 | |
32 | - if ($this->parentObject == null) throw new \Exception('Error, no object set'); |
|
34 | + if ($this->parentObject == null) { |
|
35 | + throw new \Exception('Error, no object set'); |
|
36 | + } |
|
33 | 37 | |
34 | 38 | $this->eagerLoad(); |
35 | 39 | |
@@ -41,8 +45,10 @@ discard block |
||
41 | 45 | $recordsToLoad = []; |
42 | 46 | //Get a list of records by FK to eager load |
43 | 47 | foreach ($this->siblings as $sibling) { |
44 | - if ($sibling->parentField === $this->parentField) // Ensure that it is only loading records from the same type of relation |
|
48 | + if ($sibling->parentField === $this->parentField) { |
|
49 | + // Ensure that it is only loading records from the same type of relation |
|
45 | 50 | $recordsToLoad[] = $sibling->parentObject->{$sibling->parentField}; |
51 | + } |
|
46 | 52 | } |
47 | 53 | |
48 | 54 | $recordsToLoad = array_unique($recordsToLoad); |
@@ -59,7 +65,9 @@ discard block |
||
59 | 65 | } |
60 | 66 | |
61 | 67 | foreach ($this->siblings as $sibling) { |
62 | - if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
68 | + if (isset($cache[$sibling->parentObject->{$this->parentField}])) { |
|
69 | + $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
70 | + } |
|
63 | 71 | } |
64 | 72 | /* |
65 | 73 | foreach ($this->siblings as $sibling) { |
@@ -70,13 +78,18 @@ discard block |
||
70 | 78 | } |
71 | 79 | |
72 | 80 | public function __call($func, array $args = []) { |
73 | - if ($this->lazyLoad() == null) return ''; |
|
81 | + if ($this->lazyLoad() == null) { |
|
82 | + return ''; |
|
83 | + } |
|
74 | 84 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
75 | 85 | } |
76 | 86 | |
77 | 87 | public function __get($name) { |
78 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
79 | - else return null; |
|
88 | + if ($this->lazyLoad()) { |
|
89 | + return $this->lazyLoad()->$name; |
|
90 | + } else { |
|
91 | + return null; |
|
92 | + } |
|
80 | 93 | } |
81 | 94 | |
82 | 95 | public function __isset($name) { |
@@ -7,17 +7,17 @@ |
||
7 | 7 | private $intermediateName; |
8 | 8 | |
9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
10 | - $this->iterator = $iterator; |
|
11 | - $this->intermediateName = $intermediateName; |
|
10 | + $this->iterator = $iterator; |
|
11 | + $this->intermediateName = $intermediateName; |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
16 | 16 | return $this->iterator->current(); |
17 | 17 | } |
18 | 18 | |
19 | 19 | public function key() { |
20 | - return $this->iterator->key(); |
|
20 | + return $this->iterator->key(); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function next() { |
@@ -12,7 +12,9 @@ |
||
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) { |
|
16 | + return $this->iterator->current()->{$this->intermediateName}; |
|
17 | + } |
|
16 | 18 | return $this->iterator->current(); |
17 | 19 | } |
18 | 20 |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
38 | 38 | private function getOtherFieldNameInfo() { |
39 | 39 | if ($this->otherInfo == null) { |
40 | - $propertyReader = function($name) {return $this->$name; }; |
|
40 | + $propertyReader = function($name) {return $this->$name; }; |
|
41 | 41 | |
42 | 42 | $reader = $propertyReader->bindTo($this->intermediateMapper, $this->intermediateMapper); |
43 | 43 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $propertyReader = $propertyReader->bindTo($relation, $relation); |
46 | 46 | if ($propertyReader('parentField') != $this->parentField) { |
47 | 47 | $relation = $relation->getData($this->object); |
48 | - $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
48 | + $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | |
104 | 104 | private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
105 | 105 | $record = new \stdClass; |
106 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
106 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
107 | 107 | $record->$valueField = $this->object->{$relatedField}; |
108 | 108 | $this->intermediateMapper[] = $record; |
109 | 109 | } |
@@ -32,8 +32,12 @@ discard block |
||
32 | 32 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
33 | 33 | $this->results = $data; |
34 | 34 | $this->object = $parentObject; |
35 | - if (empty($parentObject->{$relatedField})) return; |
|
36 | - foreach ($data as $dt) $this[] = $dt; |
|
35 | + if (empty($parentObject->{$relatedField})) { |
|
36 | + return; |
|
37 | + } |
|
38 | + foreach ($data as $dt) { |
|
39 | + $this[] = $dt; |
|
40 | + } |
|
37 | 41 | } |
38 | 42 | |
39 | 43 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -88,8 +92,9 @@ discard block |
||
88 | 92 | |
89 | 93 | public function offsetSet($name, $value) { |
90 | 94 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
91 | - if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
92 | - else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
95 | + if ($this->autoTraverse) { |
|
96 | + $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
97 | + } else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
93 | 98 | $record = $value; |
94 | 99 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
95 | 100 | $record->$valueField = $this->object->{$relatedField}; |
@@ -90,26 +90,26 @@ |
||
90 | 90 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
91 | 91 | if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
92 | 92 | else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
93 | - $record = $value; |
|
93 | + $record = $value; |
|
94 | 94 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
95 | 95 | $record->$valueField = $this->object->{$relatedField}; |
96 | 96 | $this->intermediateMapper[] = $record; |
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
100 | - private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
101 | - return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
102 | - isset($record->$valueField) && isset($this->object->{$relatedField}) && |
|
103 | - $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
104 | - $record->$valueField == $this->object->{$relatedField}); |
|
105 | - } |
|
106 | - |
|
107 | - private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
108 | - $record = new \stdClass; |
|
109 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
110 | - $record->$valueField = $this->object->{$relatedField}; |
|
111 | - $this->intermediateMapper[] = $record; |
|
112 | - } |
|
100 | + private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
101 | + return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
102 | + isset($record->$valueField) && isset($this->object->{$relatedField}) && |
|
103 | + $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
104 | + $record->$valueField == $this->object->{$relatedField}); |
|
105 | + } |
|
106 | + |
|
107 | + private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
108 | + $record = new \stdClass; |
|
109 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
110 | + $record->$valueField = $this->object->{$relatedField}; |
|
111 | + $this->intermediateMapper[] = $record; |
|
112 | + } |
|
113 | 113 | |
114 | 114 | public function offsetUnset($id) { |
115 | 115 | //$this->relation->mapper->filter([$relatedField => $this->object->$valueField, $this->relation->parentField => $id])->delete(); |
@@ -6,7 +6,9 @@ discard block |
||
6 | 6 | private $localField; |
7 | 7 | |
8 | 8 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $critiera = []) { |
9 | - if ($critiera) $mapper->filter($critiera); |
|
9 | + if ($critiera) { |
|
10 | + $mapper->filter($critiera); |
|
11 | + } |
|
10 | 12 | $this->mapper = $mapper; |
11 | 13 | $this->parentField = $parentField; |
12 | 14 | $this->localField = $localField; |
@@ -14,17 +16,24 @@ discard block |
||
14 | 16 | |
15 | 17 | |
16 | 18 | public function getData($parentObject) { |
17 | - if (!isset($parentObject->{$this->parentField})) $mapper = $this->mapper; |
|
18 | - else $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
19 | + if (!isset($parentObject->{$this->parentField})) { |
|
20 | + $mapper = $this->mapper; |
|
21 | + } else { |
|
22 | + $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
23 | + } |
|
19 | 24 | |
20 | 25 | return $mapper; |
21 | 26 | } |
22 | 27 | |
23 | 28 | |
24 | 29 | public function overwrite($key, &$mapper) { |
25 | - if (!isset($key->{$this->parentField})) return false; |
|
30 | + if (!isset($key->{$this->parentField})) { |
|
31 | + return false; |
|
32 | + } |
|
26 | 33 | foreach ($mapper as $k => $val) { |
27 | - if (!empty($val->{$this->localField}) && $val->{$this->localField} != $key->{$this->parentField}) continue; |
|
34 | + if (!empty($val->{$this->localField}) && $val->{$this->localField} != $key->{$this->parentField}) { |
|
35 | + continue; |
|
36 | + } |
|
28 | 37 | $val->{$this->localField} = $key->{$this->parentField}; |
29 | 38 | $this->mapper[] = $val; |
30 | 39 | } |
@@ -2,28 +2,28 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class In implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return !is_numeric($key) && is_array($value); |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return !is_numeric($key) && is_array($value); |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - $args = []; |
|
11 | - $inSql = []; |
|
12 | - $count = count($value); |
|
13 | - $value = array_values($value); // fix numeric index being different than $i |
|
14 | - for ($i = 0; $i < $count; $i++) { |
|
15 | - $args[$key . $i] = $value[$i]; |
|
16 | - $inSql[] = ':' . $key . $i; |
|
17 | - } |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + $args = []; |
|
11 | + $inSql = []; |
|
12 | + $count = count($value); |
|
13 | + $value = array_values($value); // fix numeric index being different than $i |
|
14 | + for ($i = 0; $i < $count; $i++) { |
|
15 | + $args[$key . $i] = $value[$i]; |
|
16 | + $inSql[] = ':' . $key . $i; |
|
17 | + } |
|
18 | 18 | |
19 | - $notText = ''; |
|
20 | - if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
21 | - $notText = ' NOT'; |
|
22 | - } |
|
19 | + $notText = ''; |
|
20 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
21 | + $notText = ' NOT'; |
|
22 | + } |
|
23 | 23 | |
24 | - if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
|
25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
24 | + if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
|
25 | + else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
26 | 26 | |
27 | - return ['args' => $args, 'sql' => $sql]; |
|
28 | - } |
|
27 | + return ['args' => $args, 'sql' => $sql]; |
|
28 | + } |
|
29 | 29 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | } |
23 | 23 | |
24 | 24 | if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
25 | + else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
26 | 26 | |
27 | 27 | return ['args' => $args, 'sql' => $sql]; |
28 | 28 | } |
@@ -21,8 +21,11 @@ |
||
21 | 21 | $notText = ' NOT'; |
22 | 22 | } |
23 | 23 | |
24 | - if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
|
25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
24 | + if (count($inSql) == 0) { |
|
25 | + return ['args' => [], 'sql' => '']; |
|
26 | + } else { |
|
27 | + $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
28 | + } |
|
26 | 29 | |
27 | 30 | return ['args' => $args, 'sql' => $sql]; |
28 | 31 | } |
@@ -2,100 +2,100 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | use Maphper\Maphper; |
4 | 4 | class Mock implements \Maphper\DataSource { |
5 | - private $data; |
|
6 | - private $id; |
|
7 | - |
|
8 | - public function __construct(\ArrayObject $data, $id) { |
|
9 | - $this->data = $data; |
|
10 | - $this->id = is_array($id) ? $id : [$id]; |
|
11 | - } |
|
12 | - |
|
13 | - public function getPrimaryKey() { |
|
14 | - return $this->id; |
|
15 | - } |
|
16 | - |
|
17 | - public function findById($id) { |
|
18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | - } |
|
20 | - |
|
21 | - public function findByField(array $fields, $options = []) { |
|
22 | - $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | - $filteredArray = $arrayFilter->filter($fields); |
|
24 | - if (isset($options['order'])) { |
|
25 | - list($columns, $order) = explode(' ', $options['order']); |
|
26 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | - } |
|
28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | - return $filteredArray; |
|
31 | - } |
|
5 | + private $data; |
|
6 | + private $id; |
|
7 | + |
|
8 | + public function __construct(\ArrayObject $data, $id) { |
|
9 | + $this->data = $data; |
|
10 | + $this->id = is_array($id) ? $id : [$id]; |
|
11 | + } |
|
12 | + |
|
13 | + public function getPrimaryKey() { |
|
14 | + return $this->id; |
|
15 | + } |
|
16 | + |
|
17 | + public function findById($id) { |
|
18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | + } |
|
20 | + |
|
21 | + public function findByField(array $fields, $options = []) { |
|
22 | + $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | + $filteredArray = $arrayFilter->filter($fields); |
|
24 | + if (isset($options['order'])) { |
|
25 | + list($columns, $order) = explode(' ', $options['order']); |
|
26 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | + } |
|
28 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | + return $filteredArray; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
34 | - return $function($this->findByField($criteria)); |
|
35 | - } |
|
34 | + return $function($this->findByField($criteria)); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | public function deleteById($id) { |
38 | - unset($this->data[$id]); |
|
39 | - } |
|
38 | + unset($this->data[$id]); |
|
39 | + } |
|
40 | 40 | |
41 | 41 | public function deleteByField(array $fields, array $options) { |
42 | - foreach ($this->findByField($fields, $options) as $val) { |
|
43 | - if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
44 | - else $id = $val->{$this->id[0]}; |
|
45 | - |
|
46 | - unset($this->data[$id]); |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - public function save($data) { |
|
51 | - if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
52 | - else return $this->saveSinglePk($data); |
|
53 | - } |
|
54 | - |
|
55 | - private function saveSinglePk($data) { |
|
56 | - if (isset($data->{$this->id[0]})) { |
|
57 | - $id = $data->{$this->id[0]}; |
|
58 | - } |
|
59 | - else { |
|
60 | - $id = count($this->data); |
|
61 | - $data->{$this->id[0]} = $id; |
|
62 | - } |
|
63 | - |
|
64 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
65 | - } |
|
66 | - |
|
67 | - private function saveMultiPk($data) { |
|
68 | - $saveId = $this->getMultiPkSaveId($data); |
|
69 | - |
|
70 | - $this->data[$saveId] = (object)array_merge($this->findById($saveId), (array)$data); |
|
71 | - } |
|
72 | - |
|
73 | - private function getMultiPkSaveId($data) { |
|
74 | - $keyVals = []; |
|
75 | - foreach ($this->id as $keyName) { |
|
76 | - $keyVals[] = $data->$keyName; |
|
77 | - } |
|
78 | - return implode(',', $keyVals); |
|
79 | - } |
|
80 | - |
|
81 | - public function getErrors() { |
|
82 | - return []; |
|
83 | - } |
|
84 | - |
|
85 | - private function getOrderFunction($order, $columns) { |
|
86 | - return function($a, $b) use ($order, $columns) { |
|
87 | - foreach (explode(',', $columns) as $column) { |
|
88 | - $aColumn = $a->$column; |
|
89 | - $bColumn = $b->$column; |
|
90 | - if ($aColumn === $bColumn) { |
|
91 | - $sortVal = 0; |
|
92 | - continue; |
|
93 | - } |
|
94 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
95 | - break; |
|
96 | - } |
|
97 | - if ($order === 'desc') return -$sortVal; |
|
98 | - else return $sortVal; |
|
99 | - }; |
|
100 | - } |
|
42 | + foreach ($this->findByField($fields, $options) as $val) { |
|
43 | + if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
44 | + else $id = $val->{$this->id[0]}; |
|
45 | + |
|
46 | + unset($this->data[$id]); |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + public function save($data) { |
|
51 | + if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
52 | + else return $this->saveSinglePk($data); |
|
53 | + } |
|
54 | + |
|
55 | + private function saveSinglePk($data) { |
|
56 | + if (isset($data->{$this->id[0]})) { |
|
57 | + $id = $data->{$this->id[0]}; |
|
58 | + } |
|
59 | + else { |
|
60 | + $id = count($this->data); |
|
61 | + $data->{$this->id[0]} = $id; |
|
62 | + } |
|
63 | + |
|
64 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
65 | + } |
|
66 | + |
|
67 | + private function saveMultiPk($data) { |
|
68 | + $saveId = $this->getMultiPkSaveId($data); |
|
69 | + |
|
70 | + $this->data[$saveId] = (object)array_merge($this->findById($saveId), (array)$data); |
|
71 | + } |
|
72 | + |
|
73 | + private function getMultiPkSaveId($data) { |
|
74 | + $keyVals = []; |
|
75 | + foreach ($this->id as $keyName) { |
|
76 | + $keyVals[] = $data->$keyName; |
|
77 | + } |
|
78 | + return implode(',', $keyVals); |
|
79 | + } |
|
80 | + |
|
81 | + public function getErrors() { |
|
82 | + return []; |
|
83 | + } |
|
84 | + |
|
85 | + private function getOrderFunction($order, $columns) { |
|
86 | + return function($a, $b) use ($order, $columns) { |
|
87 | + foreach (explode(',', $columns) as $column) { |
|
88 | + $aColumn = $a->$column; |
|
89 | + $bColumn = $b->$column; |
|
90 | + if ($aColumn === $bColumn) { |
|
91 | + $sortVal = 0; |
|
92 | + continue; |
|
93 | + } |
|
94 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
95 | + break; |
|
96 | + } |
|
97 | + if ($order === 'desc') return -$sortVal; |
|
98 | + else return $sortVal; |
|
99 | + }; |
|
100 | + } |
|
101 | 101 | } |
@@ -25,8 +25,12 @@ discard block |
||
25 | 25 | list($columns, $order) = explode(' ', $options['order']); |
26 | 26 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
27 | 27 | } |
28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
28 | + if (isset($options['offset'])) { |
|
29 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
30 | + } |
|
31 | + if (isset($options['limit'])) { |
|
32 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
33 | + } |
|
30 | 34 | return $filteredArray; |
31 | 35 | } |
32 | 36 | |
@@ -40,23 +44,28 @@ discard block |
||
40 | 44 | |
41 | 45 | public function deleteByField(array $fields, array $options) { |
42 | 46 | foreach ($this->findByField($fields, $options) as $val) { |
43 | - if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
44 | - else $id = $val->{$this->id[0]}; |
|
47 | + if (count($this->id) > 1) { |
|
48 | + $id = $this->getMultiPkSaveId($val); |
|
49 | + } else { |
|
50 | + $id = $val->{$this->id[0]}; |
|
51 | + } |
|
45 | 52 | |
46 | 53 | unset($this->data[$id]); |
47 | 54 | } |
48 | 55 | } |
49 | 56 | |
50 | 57 | public function save($data) { |
51 | - if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
52 | - else return $this->saveSinglePk($data); |
|
58 | + if (count($this->id) > 1) { |
|
59 | + return $this->saveMultiPk($data); |
|
60 | + } else { |
|
61 | + return $this->saveSinglePk($data); |
|
62 | + } |
|
53 | 63 | } |
54 | 64 | |
55 | 65 | private function saveSinglePk($data) { |
56 | 66 | if (isset($data->{$this->id[0]})) { |
57 | 67 | $id = $data->{$this->id[0]}; |
58 | - } |
|
59 | - else { |
|
68 | + } else { |
|
60 | 69 | $id = count($this->data); |
61 | 70 | $data->{$this->id[0]} = $id; |
62 | 71 | } |
@@ -90,12 +99,16 @@ discard block |
||
90 | 99 | if ($aColumn === $bColumn) { |
91 | 100 | $sortVal = 0; |
92 | 101 | continue; |
102 | + } else { |
|
103 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
93 | 104 | } |
94 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
95 | 105 | break; |
96 | 106 | } |
97 | - if ($order === 'desc') return -$sortVal; |
|
98 | - else return $sortVal; |
|
107 | + if ($order === 'desc') { |
|
108 | + return -$sortVal; |
|
109 | + } else { |
|
110 | + return $sortVal; |
|
111 | + } |
|
99 | 112 | }; |
100 | 113 | } |
101 | 114 | } |
@@ -10,7 +10,9 @@ discard block |
||
10 | 10 | } |
11 | 11 | |
12 | 12 | public function getAdapter() { |
13 | - if ($this->db instanceof \Maphper\DataSource) return $this->db; |
|
13 | + if ($this->db instanceof \Maphper\DataSource) { |
|
14 | + return $this->db; |
|
15 | + } |
|
14 | 16 | |
15 | 17 | $adapter = '\\Maphper\\DataSource\\' . ucfirst($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
16 | 18 | |
@@ -18,7 +20,9 @@ discard block |
||
18 | 20 | } |
19 | 21 | |
20 | 22 | public function getEditMode() { |
21 | - if (!isset($this->options['editmode'])) return false; |
|
23 | + if (!isset($this->options['editmode'])) { |
|
24 | + return false; |
|
25 | + } |
|
22 | 26 | |
23 | 27 | return $this->options['editmode'] === true ? Database::EDIT_STRUCTURE | Database::EDIT_INDEX | Database::EDIT_OPTIMISE : $this->options['editmode']; |
24 | 28 | } |
@@ -24,7 +24,7 @@ |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | public function getCacheMode() { |
27 | - return (int) ($this->options['cachemode'] ?? 0); |
|
27 | + return (int)($this->options['cachemode'] ?? 0); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | public function read($option) { |
@@ -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) . ')'; |
@@ -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 | } |