@@ -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}; |
@@ -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,15 +108,20 @@ 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) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 111 | + foreach ($this->relations as $name => $relation) { |
|
| 112 | + if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 113 | + } |
|
| 105 | 114 | |
| 106 | - if (empty($obj->{$pk[0]})) unset($obj->{$pk[0]}); |
|
| 115 | + if (empty($obj->{$pk[0]})) { |
|
| 116 | + unset($obj->{$pk[0]}); |
|
| 117 | + } |
|
| 107 | 118 | return $obj; |
| 108 | 119 | } |
| 109 | 120 | |
| 110 | 121 | public function offsetExists($offset) { |
| 111 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 122 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 123 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 124 | + } |
|
| 112 | 125 | if (!empty($this->settings['filter'])) { |
| 113 | 126 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 114 | 127 | return isset($data[0]); |
@@ -121,7 +134,9 @@ discard block |
||
| 121 | 134 | } |
| 122 | 135 | |
| 123 | 136 | public function offsetGet($offset) { |
| 124 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 137 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 138 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 139 | + } |
|
| 125 | 140 | if (!empty($this->settings['filter'])) { |
| 126 | 141 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 127 | 142 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -132,11 +147,15 @@ discard block |
||
| 132 | 147 | public function __call($method, $args) { |
| 133 | 148 | if (array_key_exists($method, $this->settings)) { |
| 134 | 149 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 135 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 136 | - else $maphper->settings[$method] = $args[0]; |
|
| 150 | + if (is_array($maphper->settings[$method])) { |
|
| 151 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 152 | + } else { |
|
| 153 | + $maphper->settings[$method] = $args[0]; |
|
| 154 | + } |
|
| 137 | 155 | return $maphper; |
| 156 | + } else { |
|
| 157 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 138 | 158 | } |
| 139 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 140 | 159 | } |
| 141 | 160 | |
| 142 | 161 | public function findAggregate($function, $field, $group = null) { |