|
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|null $formType The form type |
|
34
|
|
|
* @param string|null $yamlFile The dir path of YAML file |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($formType = null, $yamlFile = null) |
|
37
|
|
|
{ |
|
38
|
|
|
if (null === $yamlFile) { |
|
39
|
|
|
$yamlFile = __DIR__ . '/../../../../../../../../wp_symfony_form_email_log.yml'; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
$data = Yaml::parse(file_get_contents($yamlFile)); |
|
42
|
|
|
if (null === $formType) { |
|
43
|
|
|
$this->data = $data; |
|
|
|
|
|
|
44
|
|
|
} else { |
|
45
|
|
|
foreach ($data as $item) { |
|
|
|
|
|
|
46
|
|
|
if ($item['formType'] === $formType) { |
|
47
|
|
|
$this->data[] = $item; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (null === $this->data) { |
|
53
|
|
|
$this->data = []; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function findAll($limit, $offset) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$data = $this->data; |
|
63
|
|
|
foreach ($data as $key => $item) { |
|
64
|
|
|
if (array_key_exists('ID', $item)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
$data[$key]['ID'] = $key; |
|
68
|
|
|
} |
|
69
|
|
|
usort($data, [$this, 'sort']); |
|
70
|
|
|
|
|
71
|
|
|
return $this->paginate($data, $limit, $offset); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function query(array $criteria, $limit, $offset) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$data = []; |
|
80
|
|
|
foreach ($this->data as $key => $item) { |
|
81
|
|
|
foreach ($criteria as $name => $singleCriteria) { |
|
82
|
|
|
if ($item[$name] === $singleCriteria) { |
|
83
|
|
|
$data[$key] = $item; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
if (array_key_exists('ID', $item)) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
$data[$key]['ID'] = $key; |
|
90
|
|
|
} |
|
91
|
|
|
usort($data, [$this, 'sort']); |
|
92
|
|
|
|
|
93
|
|
|
return $this->paginate($data, $limit, $offset); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function properties() |
|
100
|
|
|
{ |
|
101
|
|
|
return isset($this->data[0]) ? array_keys($this->data[0]) : null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function size() |
|
108
|
|
|
{ |
|
109
|
|
|
return count($this->data); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Paginates the given collection of data. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $data The collection data |
|
116
|
|
|
* @param int $limit Logs per page |
|
117
|
|
|
* @param int $offset The number of the page |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
private function paginate($data, $limit, $offset) |
|
122
|
|
|
{ |
|
123
|
|
|
return array_slice($data, (($offset - 1) * $limit), $limit); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Compares the given two items. |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $item1 The first item |
|
130
|
|
|
* @param mixed $item2 The second item |
|
131
|
|
|
* |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
private function sort($item1, $item2) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
$orderBy = 'name'; |
|
137
|
|
|
$order = 'asc'; |
|
138
|
|
|
|
|
139
|
|
|
if (!empty($_GET['orderby'])) { |
|
140
|
|
|
$orderBy = $_GET['orderby']; |
|
141
|
|
|
} |
|
142
|
|
|
if (!empty($_GET['order'])) { |
|
143
|
|
|
$order = $_GET['order']; |
|
144
|
|
|
} |
|
145
|
|
|
$result = strcmp($item1[$orderBy], $item2[$orderBy]); |
|
146
|
|
|
if ('asc' === $order) { |
|
147
|
|
|
return $result; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return -$result; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|