1
|
|
|
<?php |
2
|
|
|
namespace Maphper\DataSource; |
3
|
|
|
use Maphper\Maphper; |
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
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
34
|
|
|
return $function($this->findByField($criteria)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function deleteById($id) { |
38
|
|
|
unset($this->data[$id]); |
39
|
|
|
} |
40
|
|
|
|
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
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.