@@ -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; |
@@ -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() { |
@@ -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(); |
@@ -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 | } |
@@ -83,21 +83,21 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | public function offsetSet($offset, $valueObj) { |
86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
86 | + if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
87 | 87 | |
88 | - //Extract private properties from the object |
|
89 | - $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
90 | - $value = $visibilityOverride->getProperties($valueObj); |
|
88 | + //Extract private properties from the object |
|
89 | + $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
90 | + $value = $visibilityOverride->getProperties($valueObj); |
|
91 | 91 | |
92 | - $value = $this->processFilters($value); |
|
93 | - $pk = $this->dataSource->getPrimaryKey(); |
|
94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
95 | - $valueCopy = $this->removeRelations(clone $value, $pk); |
|
96 | - $value = $this->entity->wrap($this->relations, $value); |
|
97 | - $this->dataSource->save($value); |
|
98 | - $visibilityOverride->write($value); |
|
99 | - $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | - $visibilityOverride->write($value); |
|
92 | + $value = $this->processFilters($value); |
|
93 | + $pk = $this->dataSource->getPrimaryKey(); |
|
94 | + if ($offset !== null) $value->{$pk[0]} = $offset; |
|
95 | + $valueCopy = $this->removeRelations(clone $value, $pk); |
|
96 | + $value = $this->entity->wrap($this->relations, $value); |
|
97 | + $this->dataSource->save($value); |
|
98 | + $visibilityOverride->write($value); |
|
99 | + $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | + $visibilityOverride->write($value); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
@@ -110,10 +110,10 @@ discard block |
||
110 | 110 | |
111 | 111 | public function offsetExists($offset) { |
112 | 112 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
113 | - if (!empty($this->settings['filter'])) { |
|
114 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
115 | - return isset($data[0]); |
|
116 | - } |
|
113 | + if (!empty($this->settings['filter'])) { |
|
114 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
115 | + return isset($data[0]); |
|
116 | + } |
|
117 | 117 | return (bool) $this->dataSource->findById($offset); |
118 | 118 | } |
119 | 119 | |
@@ -123,10 +123,10 @@ discard block |
||
123 | 123 | |
124 | 124 | public function offsetGet($offset) { |
125 | 125 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
126 | - if (!empty($this->settings['filter'])) { |
|
127 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
128 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
129 | - } |
|
126 | + if (!empty($this->settings['filter'])) { |
|
127 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
128 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
129 | + } |
|
130 | 130 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
131 | 131 | } |
132 | 132 |
@@ -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 | } |