@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
| 38 | 38 | private function getOtherFieldNameInfo() { |
| 39 | 39 | if ($this->otherInfo == null) { |
| 40 | - $propertyReader = function($name) {return $this->$name; }; |
|
| 40 | + $propertyReader = function($name) {return $this->$name; }; |
|
| 41 | 41 | |
| 42 | 42 | $reader = $propertyReader->bindTo($this->intermediateMapper, $this->intermediateMapper); |
| 43 | 43 | |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | $propertyReader = $propertyReader->bindTo($relation, $relation); |
| 46 | 46 | if ($propertyReader('parentField') != $this->parentField) { |
| 47 | 47 | $relation = $relation->getData($this->object); |
| 48 | - $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
| 48 | + $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
| 89 | 89 | if ($this->autoTraverse) { |
| 90 | 90 | $record = new \stdClass; |
| 91 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
| 91 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
| 92 | 92 | $record->$valueField = $this->object->{$relatedField}; |
| 93 | 93 | $this->intermediateMapper[] = $record; |
| 94 | 94 | |
@@ -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; |
@@ -74,7 +74,7 @@ |
||
| 74 | 74 | |
| 75 | 75 | public function __get($name) { |
| 76 | 76 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
| 77 | - else return null; |
|
| 77 | + else return null; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | public function __isset($name) { |
@@ -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) { |
@@ -7,17 +7,17 @@ |
||
| 7 | 7 | private $intermediateName; |
| 8 | 8 | |
| 9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
| 10 | - $this->iterator = $iterator; |
|
| 11 | - $this->intermediateName = $intermediateName; |
|
| 10 | + $this->iterator = $iterator; |
|
| 11 | + $this->intermediateName = $intermediateName; |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function current() { |
| 15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 16 | 16 | return $this->iterator->current(); |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function key() { |
| 20 | - return $this->iterator->key(); |
|
| 20 | + return $this->iterator->key(); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public function next() { |
@@ -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 | |
@@ -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 | } |
@@ -4,10 +4,10 @@ discard block |
||
| 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 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | public function write($data) { |
| 16 | 16 | if ($data != null) { |
| 17 | 17 | foreach ($data as $key => $value) { |
| 18 | - ($this->closure)($key, $this->processDates($value)); |
|
| 18 | + ($this->closure)($key, $this->processDates($value)); |
|
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | } |
@@ -6,25 +6,35 @@ |
||
| 6 | 6 | |
| 7 | 7 | public function replaceDates($obj, $reset = true) { |
| 8 | 8 | //prevent infinite recursion, only process each object once |
| 9 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
| 9 | + if ($this->checkCache($obj, $reset)) { |
|
| 10 | + return $obj; |
|
| 11 | + } |
|
| 10 | 12 | |
| 11 | - if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 13 | + if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) { |
|
| 14 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 15 | + } |
|
| 12 | 16 | if (is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20) { |
| 13 | 17 | try { |
| 14 | 18 | $date = new \DateTime($obj); |
| 15 | - if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
| 16 | - } |
|
| 17 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 19 | + if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) { |
|
| 20 | + $obj = $date; |
|
| 21 | + } |
|
| 22 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 18 | 23 | } |
| 19 | 24 | } |
| 20 | 25 | return $obj; |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | 28 | private function checkCache($obj, $reset) { |
| 24 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
| 29 | + if ($reset) { |
|
| 30 | + $this->processCache = new \SplObjectStorage(); |
|
| 31 | + } |
|
| 25 | 32 | |
| 26 | - if (is_object($obj) && $this->processCache->contains($obj)) return $obj; |
|
| 27 | - else if (is_object($obj)) $this->processCache->attach($obj, true); |
|
| 33 | + if (is_object($obj) && $this->processCache->contains($obj)) { |
|
| 34 | + return $obj; |
|
| 35 | + } else if (is_object($obj)) { |
|
| 36 | + $this->processCache->attach($obj, true); |
|
| 37 | + } |
|
| 28 | 38 | |
| 29 | 39 | return false; |
| 30 | 40 | } |
@@ -104,10 +104,10 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | public function offsetExists($offset) { |
| 106 | 106 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 107 | - if (!empty($this->settings['filter'])) { |
|
| 108 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 109 | - return isset($data[0]); |
|
| 110 | - } |
|
| 107 | + if (!empty($this->settings['filter'])) { |
|
| 108 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 109 | + return isset($data[0]); |
|
| 110 | + } |
|
| 111 | 111 | return (bool) $this->dataSource->findById($offset); |
| 112 | 112 | } |
| 113 | 113 | |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | |
| 118 | 118 | public function offsetGet($offset) { |
| 119 | 119 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 120 | - if (!empty($this->settings['filter'])) { |
|
| 121 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 122 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 123 | - } |
|
| 120 | + if (!empty($this->settings['filter'])) { |
|
| 121 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 122 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 123 | + } |
|
| 124 | 124 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
| 125 | 125 | } |
| 126 | 126 | |
@@ -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 = []; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | $results = $this->dataSource->findByField($this->settings['filter'], |
| 60 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
| 60 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
| 61 | 61 | |
| 62 | 62 | $siblings = new \ArrayObject(); |
| 63 | 63 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 109 | 109 | return isset($data[0]); |
| 110 | 110 | } |
| 111 | - return (bool) $this->dataSource->findById($offset); |
|
| 111 | + return (bool)$this->dataSource->findById($offset); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | public function offsetUnset($id) { |
@@ -61,7 +61,9 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | $siblings = new \ArrayObject(); |
| 63 | 63 | |
| 64 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 64 | + foreach ($results as &$result) { |
|
| 65 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 66 | + } |
|
| 65 | 67 | |
| 66 | 68 | return $results; |
| 67 | 69 | } |
@@ -78,13 +80,17 @@ discard block |
||
| 78 | 80 | private function processFilters($value) { |
| 79 | 81 | //When saving to a mapper with filters, write the filters back into the object being stored |
| 80 | 82 | foreach ($this->settings['filter'] as $key => $filterValue) { |
| 81 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
| 83 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
| 84 | + $value->$key = $filterValue; |
|
| 85 | + } |
|
| 82 | 86 | } |
| 83 | 87 | return $value; |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | public function offsetSet($offset, $valueObj) { |
| 87 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 91 | + if ($valueObj instanceof \Maphper\Relation) { |
|
| 92 | + throw new \Exception(); |
|
| 93 | + } |
|
| 88 | 94 | |
| 89 | 95 | //Extract private properties from the object |
| 90 | 96 | $propertyReader = new \Maphper\Lib\VisibilityOverride(); |
@@ -92,7 +98,9 @@ discard block |
||
| 92 | 98 | |
| 93 | 99 | $value = $this->processFilters($value); |
| 94 | 100 | $pk = $this->dataSource->getPrimaryKey(); |
| 95 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 101 | + if ($offset !== null) { |
|
| 102 | + $value->{$pk[0]} = $offset; |
|
| 103 | + } |
|
| 96 | 104 | $valueCopy = clone $value; |
| 97 | 105 | $value = $this->entity->wrap($this->relations, $value); |
| 98 | 106 | $this->dataSource->save($value); |
@@ -103,7 +111,9 @@ discard block |
||
| 103 | 111 | } |
| 104 | 112 | |
| 105 | 113 | public function offsetExists($offset) { |
| 106 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 114 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 115 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 116 | + } |
|
| 107 | 117 | if (!empty($this->settings['filter'])) { |
| 108 | 118 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 109 | 119 | return isset($data[0]); |
@@ -116,7 +126,9 @@ discard block |
||
| 116 | 126 | } |
| 117 | 127 | |
| 118 | 128 | public function offsetGet($offset) { |
| 119 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 129 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 130 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 131 | + } |
|
| 120 | 132 | if (!empty($this->settings['filter'])) { |
| 121 | 133 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 122 | 134 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -127,11 +139,15 @@ discard block |
||
| 127 | 139 | public function __call($method, $args) { |
| 128 | 140 | if (array_key_exists($method, $this->settings)) { |
| 129 | 141 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 130 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 131 | - else $maphper->settings[$method] = $args[0]; |
|
| 142 | + if (is_array($maphper->settings[$method])) { |
|
| 143 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 144 | + } else { |
|
| 145 | + $maphper->settings[$method] = $args[0]; |
|
| 146 | + } |
|
| 132 | 147 | return $maphper; |
| 148 | + } else { |
|
| 149 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 133 | 150 | } |
| 134 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 135 | 151 | } |
| 136 | 152 | |
| 137 | 153 | public function findAggregate($function, $field, $group = null) { |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | public function wrap($relations, $object, $siblings = []) { |
| 22 | 22 | //see if any relations need overwriting |
| 23 | 23 | foreach ($relations as $name => $relation) { |
| 24 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
| 24 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
| 25 | 25 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
| 26 | 26 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
| 27 | 27 | } |
@@ -23,7 +23,9 @@ |
||
| 23 | 23 | foreach ($relations as $name => $relation) { |
| 24 | 24 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
| 25 | 25 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
| 26 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
| 26 | + if ($relation->overwrite($object, $object->$name)) { |
|
| 27 | + $this->parent[] = $object; |
|
| 28 | + } |
|
| 27 | 29 | } |
| 28 | 30 | |
| 29 | 31 | $object->$name = $relation->getData($object, $siblings); |