@@ -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 | } |
@@ -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; |
@@ -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) { |
@@ -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 | |
@@ -76,13 +76,17 @@ discard block |
||
| 76 | 76 | private function processFilters($value) { |
| 77 | 77 | //When saving to a mapper with filters, write the filters back into the object being stored |
| 78 | 78 | foreach ($this->settings['filter'] as $key => $filterValue) { |
| 79 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
| 79 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
| 80 | + $value->$key = $filterValue; |
|
| 81 | + } |
|
| 80 | 82 | } |
| 81 | 83 | return $value; |
| 82 | 84 | } |
| 83 | 85 | |
| 84 | 86 | public function offsetSet($offset, $valueObj) { |
| 85 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 87 | + if ($valueObj instanceof \Maphper\Relation) { |
|
| 88 | + throw new \Exception(); |
|
| 89 | + } |
|
| 86 | 90 | |
| 87 | 91 | //Extract private properties from the object |
| 88 | 92 | $propertyReader = new \Maphper\Lib\VisibilityOverride(); |
@@ -90,7 +94,9 @@ discard block |
||
| 90 | 94 | |
| 91 | 95 | $value = $this->processFilters($value); |
| 92 | 96 | $pk = $this->dataSource->getPrimaryKey(); |
| 93 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 97 | + if ($offset !== null) { |
|
| 98 | + $value->{$pk[0]} = $offset; |
|
| 99 | + } |
|
| 94 | 100 | $valueCopy = clone $value; |
| 95 | 101 | $value = $this->wrap($value); |
| 96 | 102 | $this->dataSource->save($value); |
@@ -99,7 +105,9 @@ discard block |
||
| 99 | 105 | } |
| 100 | 106 | |
| 101 | 107 | public function offsetExists($offset) { |
| 102 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 108 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 109 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 110 | + } |
|
| 103 | 111 | if (!empty($this->settings['filter'])) { |
| 104 | 112 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 105 | 113 | return isset($data[0]); |
@@ -112,7 +120,9 @@ discard block |
||
| 112 | 120 | } |
| 113 | 121 | |
| 114 | 122 | public function offsetGet($offset) { |
| 115 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 123 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 124 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 125 | + } |
|
| 116 | 126 | if (!empty($this->settings['filter'])) { |
| 117 | 127 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 118 | 128 | return $this->wrap($this->createNew(isset($data[0]) ? $data[0] : null)); |
@@ -121,7 +131,9 @@ discard block |
||
| 121 | 131 | } |
| 122 | 132 | |
| 123 | 133 | private function createNew($data = [], $obj = null) { |
| 124 | - if (!$obj) $obj = (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass']; |
|
| 134 | + if (!$obj) { |
|
| 135 | + $obj = (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass']; |
|
| 136 | + } |
|
| 125 | 137 | $writer = new Lib\PropertyWriter($obj); |
| 126 | 138 | if ($data != null) { |
| 127 | 139 | foreach ($data as $key => $value) { |
@@ -141,7 +153,9 @@ discard block |
||
| 141 | 153 | foreach ($this->relations as $name => $relation) { |
| 142 | 154 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
| 143 | 155 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
| 144 | - if ($relation->overwrite($object, $object->$name)) $this[] = $object; |
|
| 156 | + if ($relation->overwrite($object, $object->$name)) { |
|
| 157 | + $this[] = $object; |
|
| 158 | + } |
|
| 145 | 159 | } |
| 146 | 160 | |
| 147 | 161 | $object->$name = $relation->getData($object, $siblings); |
@@ -153,11 +167,15 @@ discard block |
||
| 153 | 167 | public function __call($method, $args) { |
| 154 | 168 | if (array_key_exists($method, $this->settings)) { |
| 155 | 169 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 156 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 157 | - else $maphper->settings[$method] = $args[0]; |
|
| 170 | + if (is_array($maphper->settings[$method])) { |
|
| 171 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 172 | + } else { |
|
| 173 | + $maphper->settings[$method] = $args[0]; |
|
| 174 | + } |
|
| 158 | 175 | return $maphper; |
| 176 | + } else { |
|
| 177 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 159 | 178 | } |
| 160 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 161 | 179 | } |
| 162 | 180 | |
| 163 | 181 | public function findAggregate($function, $field, $group = null) { |
@@ -5,8 +5,7 @@ |
||
| 5 | 5 | public function __construct($object) { |
| 6 | 6 | if ($object instanceof \stdclass) { |
| 7 | 7 | $this->closure = function ($field, $value) use ($object) { $object->$field = $value; }; |
| 8 | - } |
|
| 9 | - else { |
|
| 8 | + } else { |
|
| 10 | 9 | $this->closure = function ($field, $value) { $this->$field = $value; }; |
| 11 | 10 | $this->closure = $this->closure->bindTo($object, $object); |
| 12 | 11 | } |