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 logs collection. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $logs; |
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->logs = Yaml::parse(file_get_contents($yamlFile)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function get($limit, $offset) |
38
|
|
|
{ |
39
|
|
|
$logs = $this->all(); |
40
|
|
|
usort($logs, [$this, 'sort']); |
41
|
|
|
|
42
|
|
|
return $this->paginate($logs, $limit, $offset); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function properties() |
49
|
|
|
{ |
50
|
|
|
return array_keys($this->logs[0]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function size() |
57
|
|
|
{ |
58
|
|
|
return count($this->logs); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieve all the logs from YAML storage. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
private function all() |
67
|
|
|
{ |
68
|
|
|
$logs = $this->logs; |
69
|
|
|
foreach ($logs as $key => $item) { |
70
|
|
|
if (array_key_exists('ID', $item)) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
$logs[$key]['ID'] = $key; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $logs; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Paginates the given collection of logs. |
81
|
|
|
* |
82
|
|
|
* @param array $logs The collection logs |
83
|
|
|
* @param int $limit Logs per page |
84
|
|
|
* @param int $offset The number of the page |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
private function paginate($logs, $limit, $offset) |
89
|
|
|
{ |
90
|
|
|
return array_slice($logs, (($offset - 1) * $limit), $limit); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Compares the given two logs. |
95
|
|
|
* |
96
|
|
|
* @param array $log1 The first log |
97
|
|
|
* @param array $log2 The second log |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
private function sort($log1, $log2) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$orderBy = 'date'; |
104
|
|
|
$order = 'asc'; |
105
|
|
|
|
106
|
|
|
if (!empty($_GET['orderby'])) { |
107
|
|
|
$orderBy = $_GET['orderby']; |
108
|
|
|
} |
109
|
|
|
if (!empty($_GET['order'])) { |
110
|
|
|
$order = $_GET['order']; |
111
|
|
|
} |
112
|
|
|
$result = strcmp($log1[$orderBy], $log2[$orderBy]); |
113
|
|
|
if ('asc' === $order) { |
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return -$result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.