|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LIN3S\WPSymfonyForm\Admin\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* In memory strategy of storage. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Beñat Espiña <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class InMemoryStorage implements Storage |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The data collection. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $data; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param mixed $data The data |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($data) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->data = $data; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
View Code Duplication |
public function findAll($limit, $offset) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$data = $this->data; |
|
37
|
|
|
foreach ($data as $key => $item) { |
|
38
|
|
|
if (array_key_exists('ID', $item)) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
$data[$key]['ID'] = $key; |
|
42
|
|
|
} |
|
43
|
|
|
usort($data, [$this, 'sort']); |
|
44
|
|
|
|
|
45
|
|
|
return $this->paginate($data, $limit, $offset); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function query(array $criteria, $limit, $offset) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$data = []; |
|
54
|
|
|
foreach ($this->data as $key => $item) { |
|
55
|
|
|
foreach ($criteria as $name => $singleCriteria) { |
|
56
|
|
|
if ($item[$name] === $singleCriteria) { |
|
57
|
|
|
$data[$key] = $item; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
if (array_key_exists('ID', $item)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
$data[$key]['ID'] = $key; |
|
64
|
|
|
} |
|
65
|
|
|
usort($data, [$this, 'sort']); |
|
66
|
|
|
|
|
67
|
|
|
return $this->paginate($data, $limit, $offset); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function properties() |
|
74
|
|
|
{ |
|
75
|
|
|
if (is_array($this->data[0])) { |
|
76
|
|
|
return array_keys($this->data[0]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return array_keys($this->data); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function size() |
|
86
|
|
|
{ |
|
87
|
|
|
return count($this->data); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Paginates the given collection of data. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $data The collection data |
|
94
|
|
|
* @param int $limit Logs per page |
|
95
|
|
|
* @param int $offset The number of the page |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
private function paginate($data, $limit, $offset) |
|
100
|
|
|
{ |
|
101
|
|
|
return array_slice($data, (($offset - 1) * $limit), $limit); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Compares the given two items. |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $item1 The first item |
|
108
|
|
|
* @param mixed $item2 The second item |
|
109
|
|
|
* |
|
110
|
|
|
* @return int |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
private function sort($item1, $item2) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$orderBy = 'name'; |
|
115
|
|
|
$order = 'asc'; |
|
116
|
|
|
|
|
117
|
|
|
if (!empty($_GET['orderby'])) { |
|
118
|
|
|
$orderBy = $_GET['orderby']; |
|
119
|
|
|
} |
|
120
|
|
|
if (!empty($_GET['order'])) { |
|
121
|
|
|
$order = $_GET['order']; |
|
122
|
|
|
} |
|
123
|
|
|
$result = strcmp($item1[$orderBy], $item2[$orderBy]); |
|
124
|
|
|
if ('asc' === $order) { |
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return -$result; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..