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