@@ -33,7 +33,7 @@ |
||
33 | 33 | } |
34 | 34 | |
35 | 35 | private function getSearchFieldFunction($fields, $mode) { |
36 | - return function ($data) use ($fields, $mode) { |
|
36 | + return function($data) use ($fields, $mode) { |
|
37 | 37 | foreach ($fields as $key => $val) { |
38 | 38 | $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
39 | 39 |
@@ -2,112 +2,112 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | use Maphper\Maphper; |
4 | 4 | class Mock implements \Maphper\DataSource { |
5 | - private $data; |
|
6 | - private $id; |
|
5 | + private $data; |
|
6 | + private $id; |
|
7 | 7 | |
8 | - public function __construct(\ArrayObject $data, $id) { |
|
9 | - $this->data = $data; |
|
10 | - $this->id = is_array($id) ? $id : [$id]; |
|
11 | - } |
|
8 | + public function __construct(\ArrayObject $data, $id) { |
|
9 | + $this->data = $data; |
|
10 | + $this->id = is_array($id) ? $id : [$id]; |
|
11 | + } |
|
12 | 12 | |
13 | - public function getPrimaryKey() { |
|
14 | - return $this->id; |
|
15 | - } |
|
13 | + public function getPrimaryKey() { |
|
14 | + return $this->id; |
|
15 | + } |
|
16 | 16 | |
17 | - public function findById($id) { |
|
18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | - } |
|
17 | + public function findById($id) { |
|
18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | + } |
|
20 | 20 | |
21 | - public function findByField(array $fields, $options = []) { |
|
22 | - $array = iterator_to_array($this->data->getIterator()); |
|
23 | - $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
24 | - // Need to reset indexes |
|
25 | - $filteredArray = array_values($filteredArray); |
|
26 | - if (isset($options['order'])) { |
|
27 | - list($columns, $order) = explode(' ', $options['order']); |
|
28 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
29 | - } |
|
30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
32 | - return $filteredArray; |
|
33 | - } |
|
21 | + public function findByField(array $fields, $options = []) { |
|
22 | + $array = iterator_to_array($this->data->getIterator()); |
|
23 | + $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
24 | + // Need to reset indexes |
|
25 | + $filteredArray = array_values($filteredArray); |
|
26 | + if (isset($options['order'])) { |
|
27 | + list($columns, $order) = explode(' ', $options['order']); |
|
28 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
29 | + } |
|
30 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
32 | + return $filteredArray; |
|
33 | + } |
|
34 | 34 | |
35 | - private function getSearchFieldFunction($fields, $mode) { |
|
36 | - return function ($data) use ($fields, $mode) { |
|
37 | - foreach ($fields as $key => $val) { |
|
38 | - $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
35 | + private function getSearchFieldFunction($fields, $mode) { |
|
36 | + return function ($data) use ($fields, $mode) { |
|
37 | + foreach ($fields as $key => $val) { |
|
38 | + $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
39 | 39 | |
40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
42 | - } |
|
43 | - return !(Maphper::FIND_OR & $mode); |
|
44 | - }; |
|
45 | - } |
|
40 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | + else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
42 | + } |
|
43 | + return !(Maphper::FIND_OR & $mode); |
|
44 | + }; |
|
45 | + } |
|
46 | 46 | |
47 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
48 | - if (is_numeric($key) && is_array($val)) { |
|
49 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
50 | - } |
|
51 | - else if (!isset($data->$key)) return false; |
|
52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | - return in_array($data->$key, $val); |
|
54 | - else |
|
55 | - return $this->processFilter($mode, $val, $data->$key); |
|
56 | - } |
|
47 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
48 | + if (is_numeric($key) && is_array($val)) { |
|
49 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
50 | + } |
|
51 | + else if (!isset($data->$key)) return false; |
|
52 | + else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | + return in_array($data->$key, $val); |
|
54 | + else |
|
55 | + return $this->processFilter($mode, $val, $data->$key); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
59 | - return $function($this->findByField($criteria)); |
|
60 | - } |
|
59 | + return $function($this->findByField($criteria)); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | public function deleteById($id) { |
63 | - unset($this->data[$id]); |
|
64 | - } |
|
63 | + unset($this->data[$id]); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | public function deleteByField(array $fields, array $options) { |
67 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
68 | - } |
|
67 | + foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
68 | + } |
|
69 | 69 | |
70 | - public function save($data) { |
|
71 | - if (isset($data->{$this->id[0]})) { |
|
72 | - $id = $data->{$this->id[0]}; |
|
73 | - } |
|
74 | - else { |
|
75 | - $id = count($this->data); |
|
76 | - $data->{$this->id[0]} = $id; |
|
77 | - } |
|
70 | + public function save($data) { |
|
71 | + if (isset($data->{$this->id[0]})) { |
|
72 | + $id = $data->{$this->id[0]}; |
|
73 | + } |
|
74 | + else { |
|
75 | + $id = count($this->data); |
|
76 | + $data->{$this->id[0]} = $id; |
|
77 | + } |
|
78 | 78 | |
79 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
80 | - } |
|
79 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
80 | + } |
|
81 | 81 | |
82 | - public function getErrors() { |
|
83 | - return []; |
|
84 | - } |
|
82 | + public function getErrors() { |
|
83 | + return []; |
|
84 | + } |
|
85 | 85 | |
86 | - private function getOrderFunction($order, $columns) { |
|
87 | - return function($a, $b) use ($order, $columns) { |
|
88 | - foreach (explode(',', $columns) as $column) { |
|
89 | - $aColumn = $a->$column; |
|
90 | - $bColumn = $b->$column; |
|
91 | - if ($aColumn === $bColumn) { |
|
92 | - $sortVal = 0; |
|
93 | - continue; |
|
94 | - } |
|
95 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | - break; |
|
97 | - } |
|
98 | - if ($order === 'desc') return -$sortVal; |
|
99 | - else return $sortVal; |
|
100 | - }; |
|
101 | - } |
|
86 | + private function getOrderFunction($order, $columns) { |
|
87 | + return function($a, $b) use ($order, $columns) { |
|
88 | + foreach (explode(',', $columns) as $column) { |
|
89 | + $aColumn = $a->$column; |
|
90 | + $bColumn = $b->$column; |
|
91 | + if ($aColumn === $bColumn) { |
|
92 | + $sortVal = 0; |
|
93 | + continue; |
|
94 | + } |
|
95 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | + break; |
|
97 | + } |
|
98 | + if ($order === 'desc') return -$sortVal; |
|
99 | + else return $sortVal; |
|
100 | + }; |
|
101 | + } |
|
102 | 102 | |
103 | - private function processFilter($mode, $expected, $actual) { |
|
104 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
111 | - return $expected == $actual; |
|
112 | - } |
|
103 | + private function processFilter($mode, $expected, $actual) { |
|
104 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | + else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | + else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
111 | + return $expected == $actual; |
|
112 | + } |
|
113 | 113 | } |
@@ -27,8 +27,12 @@ discard block |
||
27 | 27 | list($columns, $order) = explode(' ', $options['order']); |
28 | 28 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
29 | 29 | } |
30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | + if (isset($options['offset'])) { |
|
31 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
32 | + } |
|
33 | + if (isset($options['limit'])) { |
|
34 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
35 | + } |
|
32 | 36 | return $filteredArray; |
33 | 37 | } |
34 | 38 | |
@@ -37,8 +41,11 @@ discard block |
||
37 | 41 | foreach ($fields as $key => $val) { |
38 | 42 | $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
39 | 43 | |
40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
44 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) { |
|
45 | + return true; |
|
46 | + } else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) { |
|
47 | + return false; |
|
48 | + } |
|
42 | 49 | } |
43 | 50 | return !(Maphper::FIND_OR & $mode); |
44 | 51 | }; |
@@ -47,12 +54,13 @@ discard block |
||
47 | 54 | private function getIfFieldMatches($key, $val, $data, $mode) { |
48 | 55 | if (is_numeric($key) && is_array($val)) { |
49 | 56 | return $this->getSearchFieldFunction($val, $key)($data); |
57 | + } else if (!isset($data->$key)) { |
|
58 | + return false; |
|
59 | + } else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) { |
|
60 | + return in_array($data->$key, $val); |
|
61 | + } else { |
|
62 | + return $this->processFilter($mode, $val, $data->$key); |
|
50 | 63 | } |
51 | - else if (!isset($data->$key)) return false; |
|
52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | - return in_array($data->$key, $val); |
|
54 | - else |
|
55 | - return $this->processFilter($mode, $val, $data->$key); |
|
56 | 64 | } |
57 | 65 | |
58 | 66 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
@@ -64,14 +72,15 @@ discard block |
||
64 | 72 | } |
65 | 73 | |
66 | 74 | public function deleteByField(array $fields, array $options) { |
67 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
75 | + foreach ($this->findByField($fields, $options) as $val) { |
|
76 | + unset($this->data[$val->{$this->id[0]}]); |
|
77 | + } |
|
68 | 78 | } |
69 | 79 | |
70 | 80 | public function save($data) { |
71 | 81 | if (isset($data->{$this->id[0]})) { |
72 | 82 | $id = $data->{$this->id[0]}; |
73 | - } |
|
74 | - else { |
|
83 | + } else { |
|
75 | 84 | $id = count($this->data); |
76 | 85 | $data->{$this->id[0]} = $id; |
77 | 86 | } |
@@ -91,23 +100,35 @@ discard block |
||
91 | 100 | if ($aColumn === $bColumn) { |
92 | 101 | $sortVal = 0; |
93 | 102 | continue; |
103 | + } else { |
|
104 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
94 | 105 | } |
95 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | 106 | break; |
97 | 107 | } |
98 | - if ($order === 'desc') return -$sortVal; |
|
99 | - else return $sortVal; |
|
108 | + if ($order === 'desc') { |
|
109 | + return -$sortVal; |
|
110 | + } else { |
|
111 | + return $sortVal; |
|
112 | + } |
|
100 | 113 | }; |
101 | 114 | } |
102 | 115 | |
103 | 116 | private function processFilter($mode, $expected, $actual) { |
104 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
117 | + if (Maphper::FIND_NOT & $mode) { |
|
118 | + return $expected != $actual; |
|
119 | + } else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) { |
|
120 | + return $expected <= $actual; |
|
121 | + } else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) { |
|
122 | + return $expected >= $actual; |
|
123 | + } else if (Maphper::FIND_GREATER & $mode) { |
|
124 | + return $expected < $actual; |
|
125 | + } else if (Maphper::FIND_LESS & $mode) { |
|
126 | + return $expected > $actual; |
|
127 | + } else if (Maphper::FIND_BETWEEN & $mode) { |
|
128 | + return $expected[0] <= $actual && $actual <= $expected[1]; |
|
129 | + } else if (Maphper::FIND_NOCASE & $mode) { |
|
130 | + return strtolower($expected) == strtolower($actual); |
|
131 | + } |
|
111 | 132 | return $expected == $actual; |
112 | 133 | } |
113 | 134 | } |
@@ -20,8 +20,7 @@ |
||
20 | 20 | if (count($this->pk) == 1) { |
21 | 21 | $pk = end($this->pk); |
22 | 22 | return $this->array[$this->iterator]->$pk; |
23 | - } |
|
24 | - else { |
|
23 | + } else { |
|
25 | 24 | $current = $this->current(); |
26 | 25 | return array_map(function($pkName) use ($current) { |
27 | 26 | return $current->$pkName; |
@@ -19,11 +19,11 @@ discard block |
||
19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
20 | 20 | //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 |
21 | 21 | if ($value instanceof \DateTime) { |
22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
22 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
23 | 23 | else $value = $value->format('Y-m-d H:i:s'); |
24 | 24 | } |
25 | 25 | if (is_object($value)) continue; |
26 | - if ($prependField){ |
|
26 | + if ($prependField) { |
|
27 | 27 | $sql[] = $this->quote($field) . ' = :' . $field; |
28 | 28 | } else { |
29 | 29 | $sql[] = ':' . $field; |
@@ -35,13 +35,13 @@ discard block |
||
35 | 35 | |
36 | 36 | public function insert($table, $data) { |
37 | 37 | $query = $this->buildSaveQuery($data); |
38 | - return new Query('INSERT INTO ' . $this->quote($table) . ' (' .implode(', ', array_keys($query['args'])).') VALUES ( ' . implode(', ', $query['sql']). ' )', $query['args']); |
|
38 | + return new Query('INSERT INTO ' . $this->quote($table) . ' (' . implode(', ', array_keys($query['args'])) . ') VALUES ( ' . implode(', ', $query['sql']) . ' )', $query['args']); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | public function update($table, array $primaryKey, $data) { |
42 | 42 | $query = $this->buildSaveQuery($data, true); |
43 | 43 | $where = []; |
44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
45 | - return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
|
44 | + foreach ($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
45 | + return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']) . ' WHERE ' . implode(' AND ', $where), $query['args']); |
|
46 | 46 | } |
47 | 47 | } |
@@ -19,10 +19,15 @@ discard block |
||
19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
20 | 20 | //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 |
21 | 21 | if ($value instanceof \DateTime) { |
22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
23 | - else $value = $value->format('Y-m-d H:i:s'); |
|
22 | + if ($value->format('H:i:s') == '00:00:00') { |
|
23 | + $value = $value->format('Y-m-d'); |
|
24 | + } else { |
|
25 | + $value = $value->format('Y-m-d H:i:s'); |
|
26 | + } |
|
27 | + } |
|
28 | + if (is_object($value)) { |
|
29 | + continue; |
|
24 | 30 | } |
25 | - if (is_object($value)) continue; |
|
26 | 31 | if ($prependField){ |
27 | 32 | $sql[] = $this->quote($field) . ' = :' . $field; |
28 | 33 | } else { |
@@ -41,7 +46,9 @@ discard block |
||
41 | 46 | public function update($table, array $primaryKey, $data) { |
42 | 47 | $query = $this->buildSaveQuery($data, true); |
43 | 48 | $where = []; |
44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
49 | + foreach($primaryKey as $field) { |
|
50 | + $where[] = $this->quote($field) . ' = :' . $field; |
|
51 | + } |
|
45 | 52 | return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
46 | 53 | } |
47 | 54 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | public function delete($table, $criteria, $args, $limit = null, $offset = null, $order = null) { |
9 | 9 | $limit = $limit ? ' LIMIT ' . $limit : ''; |
10 | 10 | $offset = $offset ? ' OFFSET ' . $offset : ''; |
11 | - $order = $order ? ' ORDER BY ' . $order : ''; |
|
11 | + $order = $order ? ' ORDER BY ' . $order : ''; |
|
12 | 12 | return new Query('DELETE FROM ' . $table . ' WHERE ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args); |
13 | 13 | } |
14 | 14 |
@@ -24,8 +24,8 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | public function offsetGet($key) { |
27 | - $depth = $this->getDepth()+1; |
|
28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
27 | + $depth = $this->getDepth() + 1; |
|
28 | + if (count($this->primaryKey) - 1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
29 | 29 | else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
30 | 30 | } |
31 | 31 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | |
45 | 45 | public function offsetUnset($key) { |
46 | 46 | $keys = $this->primaryKey; |
47 | - $this->mapper->filter([ array_pop($keys) => $key])->delete(); |
|
47 | + $this->mapper->filter([array_pop($keys) => $key])->delete(); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | public function offsetExists($key) { |
@@ -25,8 +25,11 @@ |
||
25 | 25 | |
26 | 26 | public function offsetGet($key) { |
27 | 27 | $depth = $this->getDepth()+1; |
28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
29 | - else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
|
28 | + if (count($this->primaryKey)-1 == $depth) { |
|
29 | + return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
30 | + } else { |
|
31 | + return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
|
32 | + } |
|
30 | 33 | } |
31 | 34 | |
32 | 35 | public function offsetSet($key, $value) { |
@@ -4,7 +4,7 @@ |
||
4 | 4 | private $parent; |
5 | 5 | private $primaryKey; |
6 | 6 | private $lookup; |
7 | - private $mapper; |
|
7 | + private $mapper; |
|
8 | 8 | |
9 | 9 | public function __construct(Maphper $mapper, $lookup, array $primaryKey, MultiPk $parent = null) { |
10 | 10 | $this->parent = $parent; |
@@ -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 | } |
@@ -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 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
89 | 89 | if ($this->autoTraverse) { |
90 | 90 | $record = new \stdClass; |
91 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
91 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
92 | 92 | $record->$valueField = $this->object->{$relatedField}; |
93 | 93 | $this->intermediateMapper[] = $record; |
94 | 94 |
@@ -31,7 +31,9 @@ discard block |
||
31 | 31 | public function overwrite($parentObject, &$data) { |
32 | 32 | $this->results = $data; |
33 | 33 | $this->object = $parentObject; |
34 | - foreach ($data as $dt) $this[] = $dt; |
|
34 | + foreach ($data as $dt) { |
|
35 | + $this[] = $dt; |
|
36 | + } |
|
35 | 37 | } |
36 | 38 | |
37 | 39 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -92,15 +94,13 @@ discard block |
||
92 | 94 | $record->$valueField = $this->object->{$relatedField}; |
93 | 95 | $this->intermediateMapper[] = $record; |
94 | 96 | |
95 | - } |
|
96 | - else { |
|
97 | + } else { |
|
97 | 98 | $record = $value; |
98 | 99 | if (isset($record->{$this->parentField}) && isset($value->{$this->intermediateName}) && |
99 | 100 | $record->{$this->parentField} == $value->{$this->intermediateName}->{$this->localField} && |
100 | 101 | $record->$valueField == $this->object->{$relatedField}) { |
101 | 102 | return; |
102 | - } |
|
103 | - else { |
|
103 | + } else { |
|
104 | 104 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
105 | 105 | $record->$valueField = $this->object->{$relatedField}; |
106 | 106 | $this->intermediateMapper[] = $record; |
@@ -74,7 +74,7 @@ |
||
74 | 74 | |
75 | 75 | public function __get($name) { |
76 | 76 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
77 | - else return null; |
|
77 | + else return null; |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | public function __isset($name) { |
@@ -30,7 +30,9 @@ discard block |
||
30 | 30 | private function lazyLoad() { |
31 | 31 | if (!isset($this->data)) { |
32 | 32 | |
33 | - if ($this->parentObject == null) throw new \Exception('Error, no object set'); |
|
33 | + if ($this->parentObject == null) { |
|
34 | + throw new \Exception('Error, no object set'); |
|
35 | + } |
|
34 | 36 | |
35 | 37 | $this->eagerLoad(); |
36 | 38 | |
@@ -47,8 +49,11 @@ discard block |
||
47 | 49 | |
48 | 50 | $recordsToLoad = array_unique($recordsToLoad); |
49 | 51 | //Fetch the results so they're in the cache for the corresponding maphper object |
50 | - if ($this->criteria) $results = $this->mapper->filter($this->criteira)->filter([$this->localField => $recordsToLoad]); |
|
51 | - else $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
|
52 | + if ($this->criteria) { |
|
53 | + $results = $this->mapper->filter($this->criteira)->filter([$this->localField => $recordsToLoad]); |
|
54 | + } else { |
|
55 | + $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
|
56 | + } |
|
52 | 57 | |
53 | 58 | $cache = []; |
54 | 59 | foreach ($results as $result) { |
@@ -68,13 +73,18 @@ discard block |
||
68 | 73 | } |
69 | 74 | |
70 | 75 | public function __call($func, array $args = []) { |
71 | - if ($this->lazyLoad() == null) return ''; |
|
76 | + if ($this->lazyLoad() == null) { |
|
77 | + return ''; |
|
78 | + } |
|
72 | 79 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
73 | 80 | } |
74 | 81 | |
75 | 82 | public function __get($name) { |
76 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
77 | - else return null; |
|
83 | + if ($this->lazyLoad()) { |
|
84 | + return $this->lazyLoad()->$name; |
|
85 | + } else { |
|
86 | + return null; |
|
87 | + } |
|
78 | 88 | } |
79 | 89 | |
80 | 90 | 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 |
@@ -6,25 +6,35 @@ |
||
6 | 6 | |
7 | 7 | public function replaceDates($obj, $reset = true) { |
8 | 8 | //prevent infinite recursion, only process each object once |
9 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
9 | + if ($this->checkCache($obj, $reset)) { |
|
10 | + return $obj; |
|
11 | + } |
|
10 | 12 | |
11 | - if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
13 | + if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) { |
|
14 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
15 | + } |
|
12 | 16 | if (is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20) { |
13 | 17 | try { |
14 | 18 | $date = new \DateTime($obj); |
15 | - if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
16 | - } |
|
17 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
19 | + if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) { |
|
20 | + $obj = $date; |
|
21 | + } |
|
22 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
18 | 23 | } |
19 | 24 | } |
20 | 25 | return $obj; |
21 | 26 | } |
22 | 27 | |
23 | 28 | private function checkCache($obj, $reset) { |
24 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
29 | + if ($reset) { |
|
30 | + $this->processCache = new \SplObjectStorage(); |
|
31 | + } |
|
25 | 32 | |
26 | - if (is_object($obj) && $this->processCache->contains($obj)) return $obj; |
|
27 | - else if (is_object($obj)) $this->processCache->attach($obj, true); |
|
33 | + if (is_object($obj) && $this->processCache->contains($obj)) { |
|
34 | + return $obj; |
|
35 | + } else if (is_object($obj)) { |
|
36 | + $this->processCache->attach($obj, true); |
|
37 | + } |
|
28 | 38 | |
29 | 39 | return false; |
30 | 40 | } |