@@ -2,100 +2,100 @@ |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | use Maphper\Maphper; |
| 4 | 4 | class Mock implements \Maphper\DataSource { |
| 5 | - private $data; |
|
| 6 | - private $id; |
|
| 7 | - |
|
| 8 | - public function __construct(\ArrayObject $data, $id) { |
|
| 9 | - $this->data = $data; |
|
| 10 | - $this->id = is_array($id) ? $id : [$id]; |
|
| 11 | - } |
|
| 12 | - |
|
| 13 | - public function getPrimaryKey() { |
|
| 14 | - return $this->id; |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public function findById($id) { |
|
| 18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public function findByField(array $fields, $options = []) { |
|
| 22 | - $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
| 23 | - $filteredArray = $arrayFilter->filter($fields); |
|
| 24 | - if (isset($options['order'])) { |
|
| 25 | - list($columns, $order) = explode(' ', $options['order']); |
|
| 26 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
| 27 | - } |
|
| 28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 30 | - return $filteredArray; |
|
| 31 | - } |
|
| 5 | + private $data; |
|
| 6 | + private $id; |
|
| 7 | + |
|
| 8 | + public function __construct(\ArrayObject $data, $id) { |
|
| 9 | + $this->data = $data; |
|
| 10 | + $this->id = is_array($id) ? $id : [$id]; |
|
| 11 | + } |
|
| 12 | + |
|
| 13 | + public function getPrimaryKey() { |
|
| 14 | + return $this->id; |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public function findById($id) { |
|
| 18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public function findByField(array $fields, $options = []) { |
|
| 22 | + $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
| 23 | + $filteredArray = $arrayFilter->filter($fields); |
|
| 24 | + if (isset($options['order'])) { |
|
| 25 | + list($columns, $order) = explode(' ', $options['order']); |
|
| 26 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
| 27 | + } |
|
| 28 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 29 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 30 | + return $filteredArray; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 34 | - return $function($this->findByField($criteria)); |
|
| 35 | - } |
|
| 34 | + return $function($this->findByField($criteria)); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | public function deleteById($id) { |
| 38 | - unset($this->data[$id]); |
|
| 39 | - } |
|
| 38 | + unset($this->data[$id]); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | 41 | public function deleteByField(array $fields, array $options) { |
| 42 | - foreach ($this->findByField($fields, $options) as $val) { |
|
| 43 | - if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
| 44 | - else $id = $val->{$this->id[0]}; |
|
| 45 | - |
|
| 46 | - unset($this->data[$id]); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function save($data) { |
|
| 51 | - if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
| 52 | - else return $this->saveSinglePk($data); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - private function saveSinglePk($data) { |
|
| 56 | - if (isset($data->{$this->id[0]})) { |
|
| 57 | - $id = $data->{$this->id[0]}; |
|
| 58 | - } |
|
| 59 | - else { |
|
| 60 | - $id = count($this->data); |
|
| 61 | - $data->{$this->id[0]} = $id; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - private function saveMultiPk($data) { |
|
| 68 | - $saveId = $this->getMultiPkSaveId($data); |
|
| 69 | - |
|
| 70 | - $this->data[$saveId] = (object)array_merge($this->findById($saveId), (array)$data); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - private function getMultiPkSaveId($data) { |
|
| 74 | - $keyVals = []; |
|
| 75 | - foreach ($this->id as $keyName) { |
|
| 76 | - $keyVals[] = $data->$keyName; |
|
| 77 | - } |
|
| 78 | - return implode(',', $keyVals); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getErrors() { |
|
| 82 | - return []; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - private function getOrderFunction($order, $columns) { |
|
| 86 | - return function($a, $b) use ($order, $columns) { |
|
| 87 | - foreach (explode(',', $columns) as $column) { |
|
| 88 | - $aColumn = $a->$column; |
|
| 89 | - $bColumn = $b->$column; |
|
| 90 | - if ($aColumn === $bColumn) { |
|
| 91 | - $sortVal = 0; |
|
| 92 | - continue; |
|
| 93 | - } |
|
| 94 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 95 | - break; |
|
| 96 | - } |
|
| 97 | - if ($order === 'desc') return -$sortVal; |
|
| 98 | - else return $sortVal; |
|
| 99 | - }; |
|
| 100 | - } |
|
| 42 | + foreach ($this->findByField($fields, $options) as $val) { |
|
| 43 | + if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
| 44 | + else $id = $val->{$this->id[0]}; |
|
| 45 | + |
|
| 46 | + unset($this->data[$id]); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function save($data) { |
|
| 51 | + if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
| 52 | + else return $this->saveSinglePk($data); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + private function saveSinglePk($data) { |
|
| 56 | + if (isset($data->{$this->id[0]})) { |
|
| 57 | + $id = $data->{$this->id[0]}; |
|
| 58 | + } |
|
| 59 | + else { |
|
| 60 | + $id = count($this->data); |
|
| 61 | + $data->{$this->id[0]} = $id; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + private function saveMultiPk($data) { |
|
| 68 | + $saveId = $this->getMultiPkSaveId($data); |
|
| 69 | + |
|
| 70 | + $this->data[$saveId] = (object)array_merge($this->findById($saveId), (array)$data); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + private function getMultiPkSaveId($data) { |
|
| 74 | + $keyVals = []; |
|
| 75 | + foreach ($this->id as $keyName) { |
|
| 76 | + $keyVals[] = $data->$keyName; |
|
| 77 | + } |
|
| 78 | + return implode(',', $keyVals); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getErrors() { |
|
| 82 | + return []; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + private function getOrderFunction($order, $columns) { |
|
| 86 | + return function($a, $b) use ($order, $columns) { |
|
| 87 | + foreach (explode(',', $columns) as $column) { |
|
| 88 | + $aColumn = $a->$column; |
|
| 89 | + $bColumn = $b->$column; |
|
| 90 | + if ($aColumn === $bColumn) { |
|
| 91 | + $sortVal = 0; |
|
| 92 | + continue; |
|
| 93 | + } |
|
| 94 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 95 | + break; |
|
| 96 | + } |
|
| 97 | + if ($order === 'desc') return -$sortVal; |
|
| 98 | + else return $sortVal; |
|
| 99 | + }; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -25,8 +25,12 @@ discard block |
||
| 25 | 25 | list($columns, $order) = explode(' ', $options['order']); |
| 26 | 26 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
| 27 | 27 | } |
| 28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 28 | + if (isset($options['offset'])) { |
|
| 29 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 30 | + } |
|
| 31 | + if (isset($options['limit'])) { |
|
| 32 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 33 | + } |
|
| 30 | 34 | return $filteredArray; |
| 31 | 35 | } |
| 32 | 36 | |
@@ -40,23 +44,28 @@ discard block |
||
| 40 | 44 | |
| 41 | 45 | public function deleteByField(array $fields, array $options) { |
| 42 | 46 | foreach ($this->findByField($fields, $options) as $val) { |
| 43 | - if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
| 44 | - else $id = $val->{$this->id[0]}; |
|
| 47 | + if (count($this->id) > 1) { |
|
| 48 | + $id = $this->getMultiPkSaveId($val); |
|
| 49 | + } else { |
|
| 50 | + $id = $val->{$this->id[0]}; |
|
| 51 | + } |
|
| 45 | 52 | |
| 46 | 53 | unset($this->data[$id]); |
| 47 | 54 | } |
| 48 | 55 | } |
| 49 | 56 | |
| 50 | 57 | public function save($data) { |
| 51 | - if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
| 52 | - else return $this->saveSinglePk($data); |
|
| 58 | + if (count($this->id) > 1) { |
|
| 59 | + return $this->saveMultiPk($data); |
|
| 60 | + } else { |
|
| 61 | + return $this->saveSinglePk($data); |
|
| 62 | + } |
|
| 53 | 63 | } |
| 54 | 64 | |
| 55 | 65 | private function saveSinglePk($data) { |
| 56 | 66 | if (isset($data->{$this->id[0]})) { |
| 57 | 67 | $id = $data->{$this->id[0]}; |
| 58 | - } |
|
| 59 | - else { |
|
| 68 | + } else { |
|
| 60 | 69 | $id = count($this->data); |
| 61 | 70 | $data->{$this->id[0]} = $id; |
| 62 | 71 | } |
@@ -90,12 +99,16 @@ discard block |
||
| 90 | 99 | if ($aColumn === $bColumn) { |
| 91 | 100 | $sortVal = 0; |
| 92 | 101 | continue; |
| 102 | + } else { |
|
| 103 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 93 | 104 | } |
| 94 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 95 | 105 | break; |
| 96 | 106 | } |
| 97 | - if ($order === 'desc') return -$sortVal; |
|
| 98 | - else return $sortVal; |
|
| 107 | + if ($order === 'desc') { |
|
| 108 | + return -$sortVal; |
|
| 109 | + } else { |
|
| 110 | + return $sortVal; |
|
| 111 | + } |
|
| 99 | 112 | }; |
| 100 | 113 | } |
| 101 | 114 | } |