@@ -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 | } |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
| 25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 25 | + else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 26 | 26 | |
| 27 | 27 | return ['args' => $args, 'sql' => $sql]; |
| 28 | 28 | } |
@@ -21,8 +21,11 @@ |
||
| 21 | 21 | $notText = ' NOT'; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
|
| 25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 24 | + if (count($inSql) == 0) { |
|
| 25 | + return ['args' => [], 'sql' => '']; |
|
| 26 | + } else { |
|
| 27 | + $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 28 | + } |
|
| 26 | 29 | |
| 27 | 30 | return ['args' => $args, 'sql' => $sql]; |
| 28 | 31 | } |
@@ -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 | |
@@ -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); |
@@ -101,15 +109,20 @@ discard block |
||
| 101 | 109 | } |
| 102 | 110 | |
| 103 | 111 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
| 104 | - foreach ($this->relations as $name => $relation) |
|
| 105 | - if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 112 | + foreach ($this->relations as $name => $relation) { |
|
| 113 | + if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 114 | + } |
|
| 106 | 115 | |
| 107 | - if (empty($obj->{$pk[0]})) unset($obj->{$pk[0]}); |
|
| 116 | + if (empty($obj->{$pk[0]})) { |
|
| 117 | + unset($obj->{$pk[0]}); |
|
| 118 | + } |
|
| 108 | 119 | return $obj; |
| 109 | 120 | } |
| 110 | 121 | |
| 111 | 122 | public function offsetExists($offset) { |
| 112 | - 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 | + } |
|
| 113 | 126 | if (!empty($this->settings['filter'])) { |
| 114 | 127 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 115 | 128 | return isset($data[0]); |
@@ -122,7 +135,9 @@ discard block |
||
| 122 | 135 | } |
| 123 | 136 | |
| 124 | 137 | public function offsetGet($offset) { |
| 125 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 138 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 139 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 140 | + } |
|
| 126 | 141 | if (!empty($this->settings['filter'])) { |
| 127 | 142 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 128 | 143 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -133,11 +148,15 @@ discard block |
||
| 133 | 148 | public function __call($method, $args) { |
| 134 | 149 | if (array_key_exists($method, $this->settings)) { |
| 135 | 150 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 136 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 137 | - else $maphper->settings[$method] = $args[0]; |
|
| 151 | + if (is_array($maphper->settings[$method])) { |
|
| 152 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 153 | + } else { |
|
| 154 | + $maphper->settings[$method] = $args[0]; |
|
| 155 | + } |
|
| 138 | 156 | return $maphper; |
| 157 | + } else { |
|
| 158 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 139 | 159 | } |
| 140 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 141 | 160 | } |
| 142 | 161 | |
| 143 | 162 | public function findAggregate($function, $field, $group = null) { |