@@ -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 (!isset($parentObject->{$relatedField})) return; |
|
| 36 | - foreach ($data as $dt) $this[] = $dt; |
|
| 35 | + if (!isset($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}; |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $value = $this->entity->wrap($this->relations, $value); |
| 97 | 97 | $this->dataSource->save($value); |
| 98 | 98 | $visibilityOverride->write($value); |
| 99 | - $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 99 | + $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
@@ -107,10 +107,10 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | public function offsetExists($offset) { |
| 109 | 109 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 110 | - if (!empty($this->settings['filter'])) { |
|
| 111 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 112 | - return isset($data[0]); |
|
| 113 | - } |
|
| 110 | + if (!empty($this->settings['filter'])) { |
|
| 111 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 112 | + return isset($data[0]); |
|
| 113 | + } |
|
| 114 | 114 | return (bool) $this->dataSource->findById($offset); |
| 115 | 115 | } |
| 116 | 116 | |
@@ -120,10 +120,10 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | public function offsetGet($offset) { |
| 122 | 122 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 123 | - if (!empty($this->settings['filter'])) { |
|
| 124 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 125 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 126 | - } |
|
| 123 | + if (!empty($this->settings['filter'])) { |
|
| 124 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 125 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 126 | + } |
|
| 127 | 127 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
| 128 | 128 | } |
| 129 | 129 | |
@@ -60,7 +60,9 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | $siblings = new \ArrayObject(); |
| 62 | 62 | |
| 63 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 63 | + foreach ($results as &$result) { |
|
| 64 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 65 | + } |
|
| 64 | 66 | |
| 65 | 67 | return $results; |
| 66 | 68 | } |
@@ -77,13 +79,17 @@ discard block |
||
| 77 | 79 | private function processFilters($value) { |
| 78 | 80 | //When saving to a mapper with filters, write the filters back into the object being stored |
| 79 | 81 | foreach ($this->settings['filter'] as $key => $filterValue) { |
| 80 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
| 82 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
| 83 | + $value->$key = $filterValue; |
|
| 84 | + } |
|
| 81 | 85 | } |
| 82 | 86 | return $value; |
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | public function offsetSet($offset, $valueObj) { |
| 86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 90 | + if ($valueObj instanceof \Maphper\Relation) { |
|
| 91 | + throw new \Exception(); |
|
| 92 | + } |
|
| 87 | 93 | |
| 88 | 94 | //Extract private properties from the object |
| 89 | 95 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
@@ -91,7 +97,9 @@ discard block |
||
| 91 | 97 | |
| 92 | 98 | $value = $this->processFilters($value); |
| 93 | 99 | $pk = $this->dataSource->getPrimaryKey(); |
| 94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 100 | + if ($offset !== null) { |
|
| 101 | + $value->{$pk[0]} = $offset; |
|
| 102 | + } |
|
| 95 | 103 | $valueCopy = $this->removeRelations(clone $value, $pk); |
| 96 | 104 | $value = $this->entity->wrap($this->relations, $value); |
| 97 | 105 | $this->dataSource->save($value); |
@@ -100,13 +108,16 @@ discard block |
||
| 100 | 108 | } |
| 101 | 109 | |
| 102 | 110 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
| 103 | - foreach ($this->relations as $name => $relation) |
|
| 104 | - if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && isset($obj->{$pk[0]})) unset($obj->$name); |
|
| 111 | + foreach ($this->relations as $name => $relation) { |
|
| 112 | + if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && isset($obj->{$pk[0]})) unset($obj->$name); |
|
| 113 | + } |
|
| 105 | 114 | return $obj; |
| 106 | 115 | } |
| 107 | 116 | |
| 108 | 117 | public function offsetExists($offset) { |
| 109 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 118 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 119 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 120 | + } |
|
| 110 | 121 | if (!empty($this->settings['filter'])) { |
| 111 | 122 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 112 | 123 | return isset($data[0]); |
@@ -119,7 +130,9 @@ discard block |
||
| 119 | 130 | } |
| 120 | 131 | |
| 121 | 132 | public function offsetGet($offset) { |
| 122 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 133 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 134 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 135 | + } |
|
| 123 | 136 | if (!empty($this->settings['filter'])) { |
| 124 | 137 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 125 | 138 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -130,11 +143,15 @@ discard block |
||
| 130 | 143 | public function __call($method, $args) { |
| 131 | 144 | if (array_key_exists($method, $this->settings)) { |
| 132 | 145 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 133 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 134 | - else $maphper->settings[$method] = $args[0]; |
|
| 146 | + if (is_array($maphper->settings[$method])) { |
|
| 147 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 148 | + } else { |
|
| 149 | + $maphper->settings[$method] = $args[0]; |
|
| 150 | + } |
|
| 135 | 151 | return $maphper; |
| 152 | + } else { |
|
| 153 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 136 | 154 | } |
| 137 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 138 | 155 | } |
| 139 | 156 | |
| 140 | 157 | public function findAggregate($function, $field, $group = null) { |