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