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
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* YAML strategy of storage. |
18
|
|
|
* |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class YamlStorage implements Storage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The data collection. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param string $yamlFile The dir path of YAML file |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
public function __construct($yamlFile = null) |
36
|
|
|
{ |
37
|
|
|
if (null === $yamlFile) { |
38
|
|
|
$yamlFile = __DIR__ . '/../../../../../../../../wp_symfony_form_email_log.yml'; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
$this->data = Yaml::parse(file_get_contents($yamlFile)); |
|
|
|
|
41
|
|
|
if (null === $this->data) { |
42
|
|
|
$this->data = []; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function findAll($limit, $offset) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$data = $this->data; |
52
|
|
|
foreach ($data as $key => $item) { |
53
|
|
|
if (array_key_exists('ID', $item)) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
$data[$key]['ID'] = $key; |
57
|
|
|
} |
58
|
|
|
usort($data, [$this, 'sort']); |
59
|
|
|
|
60
|
|
|
return $this->paginate($data, $limit, $offset); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function query(array $criteria, $limit, $offset) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$data = []; |
69
|
|
|
foreach ($this->data as $key => $item) { |
70
|
|
|
foreach ($criteria as $name => $singleCriteria) { |
71
|
|
|
if ($item[$name] === $singleCriteria) { |
72
|
|
|
$data[$key] = $item; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
if (array_key_exists('ID', $item)) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
$data[$key]['ID'] = $key; |
79
|
|
|
} |
80
|
|
|
usort($data, [$this, 'sort']); |
81
|
|
|
|
82
|
|
|
return $this->paginate($data, $limit, $offset); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function properties() |
89
|
|
|
{ |
90
|
|
|
return isset($this->data[0]) ? array_keys($this->data[0]) : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function size() |
97
|
|
|
{ |
98
|
|
|
return count($this->data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Paginates the given collection of data. |
103
|
|
|
* |
104
|
|
|
* @param array $data The collection data |
105
|
|
|
* @param int $limit Logs per page |
106
|
|
|
* @param int $offset The number of the page |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private function paginate($data, $limit, $offset) |
111
|
|
|
{ |
112
|
|
|
return array_slice($data, (($offset - 1) * $limit), $limit); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Compares the given two items. |
117
|
|
|
* |
118
|
|
|
* @param mixed $item1 The first item |
119
|
|
|
* @param mixed $item2 The second item |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
private function sort($item1, $item2) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$orderBy = 'name'; |
126
|
|
|
$order = 'asc'; |
127
|
|
|
|
128
|
|
|
if (!empty($_GET['orderby'])) { |
129
|
|
|
$orderBy = $_GET['orderby']; |
130
|
|
|
} |
131
|
|
|
if (!empty($_GET['order'])) { |
132
|
|
|
$order = $_GET['order']; |
133
|
|
|
} |
134
|
|
|
$result = strcmp($item1[$orderBy], $item2[$orderBy]); |
135
|
|
|
if ('asc' === $order) { |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return -$result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.