@@ -2,113 +2,113 @@ |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | use Maphper\Maphper; |
| 4 | 4 | class Mock implements \Maphper\DataSource { |
| 5 | - private $data; |
|
| 6 | - private $id; |
|
| 5 | + private $data; |
|
| 6 | + private $id; |
|
| 7 | 7 | |
| 8 | - public function __construct(\ArrayObject $data, $id) { |
|
| 9 | - $this->data = $data; |
|
| 10 | - $this->id = is_array($id) ? $id : [$id]; |
|
| 11 | - } |
|
| 8 | + public function __construct(\ArrayObject $data, $id) { |
|
| 9 | + $this->data = $data; |
|
| 10 | + $this->id = is_array($id) ? $id : [$id]; |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public function getPrimaryKey() { |
|
| 14 | - return $this->id; |
|
| 15 | - } |
|
| 13 | + public function getPrimaryKey() { |
|
| 14 | + return $this->id; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - public function findById($id) { |
|
| 18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
| 19 | - } |
|
| 17 | + public function findById($id) { |
|
| 18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function findByField(array $fields, $options = []) { |
|
| 22 | - $array = iterator_to_array($this->data->getIterator()); |
|
| 23 | - $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
| 24 | - // Need to reset indexes |
|
| 25 | - $filteredArray = array_values($filteredArray); |
|
| 26 | - if (isset($options['order'])) { |
|
| 27 | - list($columns, $order) = explode(' ', $options['order']); |
|
| 28 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
| 29 | - } |
|
| 30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 32 | - return $filteredArray; |
|
| 33 | - } |
|
| 21 | + public function findByField(array $fields, $options = []) { |
|
| 22 | + $array = iterator_to_array($this->data->getIterator()); |
|
| 23 | + $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
| 24 | + // Need to reset indexes |
|
| 25 | + $filteredArray = array_values($filteredArray); |
|
| 26 | + if (isset($options['order'])) { |
|
| 27 | + list($columns, $order) = explode(' ', $options['order']); |
|
| 28 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
| 29 | + } |
|
| 30 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 31 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 32 | + return $filteredArray; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - private function getSearchFieldFunction($fields, $mode) { |
|
| 36 | - return function ($data) use ($fields, $mode) { |
|
| 37 | - foreach ($fields as $key => $val) { |
|
| 38 | - $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
| 35 | + private function getSearchFieldFunction($fields, $mode) { |
|
| 36 | + return function ($data) use ($fields, $mode) { |
|
| 37 | + foreach ($fields as $key => $val) { |
|
| 38 | + $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
| 39 | 39 | |
| 40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
| 41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
| 42 | - } |
|
| 43 | - return !(Maphper::FIND_OR & $mode); |
|
| 44 | - }; |
|
| 45 | - } |
|
| 40 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
| 41 | + else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
| 42 | + } |
|
| 43 | + return !(Maphper::FIND_OR & $mode); |
|
| 44 | + }; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
| 48 | - if (is_numeric($key) && is_array($val)) { |
|
| 49 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
| 50 | - } |
|
| 51 | - else if (!isset($data->$key)) return false; |
|
| 52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
| 53 | - return in_array($data->$key, $val); |
|
| 54 | - else |
|
| 55 | - return $this->processFilter($mode, $val, $data->$key); |
|
| 56 | - } |
|
| 47 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
| 48 | + if (is_numeric($key) && is_array($val)) { |
|
| 49 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
| 50 | + } |
|
| 51 | + else if (!isset($data->$key)) return false; |
|
| 52 | + else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
| 53 | + return in_array($data->$key, $val); |
|
| 54 | + else |
|
| 55 | + return $this->processFilter($mode, $val, $data->$key); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 59 | - $array = iterator_to_array($this->data); |
|
| 60 | - return $function($this->findByField($criteria)); |
|
| 61 | - } |
|
| 59 | + $array = iterator_to_array($this->data); |
|
| 60 | + return $function($this->findByField($criteria)); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | public function deleteById($id) { |
| 64 | - unset($this->data[$id]); |
|
| 65 | - } |
|
| 64 | + unset($this->data[$id]); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | 67 | public function deleteByField(array $fields) { |
| 68 | - foreach ($this->findByField($fields) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
| 69 | - } |
|
| 68 | + foreach ($this->findByField($fields) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function save($data) { |
|
| 72 | - if (isset($data->{$this->id[0]})) { |
|
| 73 | - $id = $data->{$this->id[0]}; |
|
| 74 | - } |
|
| 75 | - else { |
|
| 76 | - $id = count($this->data); |
|
| 77 | - $data->{$this->id[0]} = $id; |
|
| 78 | - } |
|
| 71 | + public function save($data) { |
|
| 72 | + if (isset($data->{$this->id[0]})) { |
|
| 73 | + $id = $data->{$this->id[0]}; |
|
| 74 | + } |
|
| 75 | + else { |
|
| 76 | + $id = count($this->data); |
|
| 77 | + $data->{$this->id[0]} = $id; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
| 81 | - } |
|
| 80 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - public function getErrors() { |
|
| 84 | - return []; |
|
| 85 | - } |
|
| 83 | + public function getErrors() { |
|
| 84 | + return []; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - private function getOrderFunction($order, $columns) { |
|
| 88 | - return function($a, $b) use ($order, $columns) { |
|
| 89 | - foreach (explode(',', $columns) as $column) { |
|
| 90 | - $aColumn = $a->$column; |
|
| 91 | - $bColumn = $b->$column; |
|
| 92 | - if ($aColumn === $bColumn) { |
|
| 93 | - $sortVal = 0; |
|
| 94 | - continue; |
|
| 95 | - } |
|
| 96 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - if ($order === 'desc') return -$sortVal; |
|
| 100 | - else return $sortVal; |
|
| 101 | - }; |
|
| 102 | - } |
|
| 87 | + private function getOrderFunction($order, $columns) { |
|
| 88 | + return function($a, $b) use ($order, $columns) { |
|
| 89 | + foreach (explode(',', $columns) as $column) { |
|
| 90 | + $aColumn = $a->$column; |
|
| 91 | + $bColumn = $b->$column; |
|
| 92 | + if ($aColumn === $bColumn) { |
|
| 93 | + $sortVal = 0; |
|
| 94 | + continue; |
|
| 95 | + } |
|
| 96 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + if ($order === 'desc') return -$sortVal; |
|
| 100 | + else return $sortVal; |
|
| 101 | + }; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - private function processFilter($mode, $expected, $actual) { |
|
| 105 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
| 106 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
| 107 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
| 108 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
| 109 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
| 110 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
| 111 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
| 112 | - return $expected == $actual; |
|
| 113 | - } |
|
| 104 | + private function processFilter($mode, $expected, $actual) { |
|
| 105 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
| 106 | + else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
| 107 | + else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
| 108 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
| 109 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
| 110 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
| 111 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
| 112 | + return $expected == $actual; |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -80,17 +80,17 @@ |
||
| 80 | 80 | return ['args' => $args, 'sql' => [$query]]; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - private function getOperator($mode) { |
|
| 84 | - $operator = ""; |
|
| 83 | + private function getOperator($mode) { |
|
| 84 | + $operator = ""; |
|
| 85 | 85 | |
| 86 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 87 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 88 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 89 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 90 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 86 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 87 | + else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 88 | + else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 89 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 90 | + else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 91 | 91 | |
| 92 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 92 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 93 | 93 | |
| 94 | - return $operator; |
|
| 95 | - } |
|
| 94 | + return $operator; |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -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) { |
@@ -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() { |
@@ -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 | |
@@ -114,7 +114,7 @@ |
||
| 114 | 114 | else $limit = ''; |
| 115 | 115 | |
| 116 | 116 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 117 | - $query['sql'] = array_filter($query['sql']); |
|
| 117 | + $query['sql'] = array_filter($query['sql']); |
|
| 118 | 118 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
| 119 | 119 | $this->addIndex(array_keys($query['args'])); |
| 120 | 120 | |