@@ -1,19 +1,19 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Maphper; |
| 3 | 3 | class Maphper implements \Countable, \ArrayAccess, \IteratorAggregate { |
| 4 | - const FIND_EXACT = 0x1; |
|
| 5 | - const FIND_LIKE = 0x2; |
|
| 6 | - const FIND_STARTS = 0x4; |
|
| 7 | - const FIND_ENDS = 0x8; |
|
| 8 | - const FIND_BIT = 0x10; |
|
| 9 | - const FIND_GREATER = 0x20; |
|
| 10 | - const FIND_LESS = 0x40; |
|
| 11 | - const FIND_EXPRESSION = 0x80; |
|
| 12 | - const FIND_AND = 0x100; |
|
| 13 | - const FIND_OR = 0x200; |
|
| 14 | - const FIND_NOT = 0x400; |
|
| 15 | - const FIND_BETWEEN = 0x800; |
|
| 16 | - const FIND_NOCASE = 0x1000; |
|
| 4 | + const FIND_EXACT = 0x1; |
|
| 5 | + const FIND_LIKE = 0x2; |
|
| 6 | + const FIND_STARTS = 0x4; |
|
| 7 | + const FIND_ENDS = 0x8; |
|
| 8 | + const FIND_BIT = 0x10; |
|
| 9 | + const FIND_GREATER = 0x20; |
|
| 10 | + const FIND_LESS = 0x40; |
|
| 11 | + const FIND_EXPRESSION = 0x80; |
|
| 12 | + const FIND_AND = 0x100; |
|
| 13 | + const FIND_OR = 0x200; |
|
| 14 | + const FIND_NOT = 0x400; |
|
| 15 | + const FIND_BETWEEN = 0x800; |
|
| 16 | + const FIND_NOCASE = 0x1000; |
|
| 17 | 17 | |
| 18 | 18 | private $dataSource; |
| 19 | 19 | private $relations = []; |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | $results = $this->dataSource->findByField($this->settings['filter'], |
| 57 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
| 57 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
| 58 | 58 | |
| 59 | 59 | $siblings = new \ArrayObject(); |
| 60 | 60 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | $valueCopy = clone $value; |
| 95 | 95 | $value = $this->wrap($value); |
| 96 | 96 | $this->dataSource->save($value); |
| 97 | - $value = $this->wrap((object) array_merge((array)$value, (array)$valueCopy)); |
|
| 97 | + $value = $this->wrap((object)array_merge((array)$value, (array)$valueCopy)); |
|
| 98 | 98 | $this->createNew($value, $valueObj); |
| 99 | 99 | } |
| 100 | 100 | |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 105 | 105 | return isset($data[0]); |
| 106 | 106 | } |
| 107 | - return (bool) $this->dataSource->findById($offset); |
|
| 107 | + return (bool)$this->dataSource->findById($offset); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | public function offsetUnset($id) { |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | private function wrap($object, $key = null, $siblings = null) { |
| 140 | 140 | //see if any relations need overwriting |
| 141 | 141 | foreach ($this->relations as $name => $relation) { |
| 142 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
| 142 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
| 143 | 143 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
| 144 | 144 | if ($relation->overwrite($object, $object->$name)) $this[] = $object; |
| 145 | 145 | } |
@@ -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) { |
@@ -4,10 +4,10 @@ |
||
| 4 | 4 | private $closure; |
| 5 | 5 | public function __construct($object) { |
| 6 | 6 | if ($object instanceof \stdclass) { |
| 7 | - $this->closure = function ($field, $value) use ($object) { $object->$field = $value; }; |
|
| 7 | + $this->closure = function($field, $value) use ($object) { $object->$field = $value; }; |
|
| 8 | 8 | } |
| 9 | 9 | else { |
| 10 | - $this->closure = function ($field, $value) { $this->$field = $value; }; |
|
| 10 | + $this->closure = function($field, $value) { $this->$field = $value; }; |
|
| 11 | 11 | $this->closure = $this->closure->bindTo($object, $object); |
| 12 | 12 | } |
| 13 | 13 | } |
@@ -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 | } |