|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Subjects\FileResolver\BunchFileResolver |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects\FileResolver; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\BunchKeys; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Plugin that processes the subjects. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class SimpleFileResolver extends AbstractFileResolver |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The regular expression used to load the files with. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $regex = '/^.*\/%s\\.%s$/'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The matches for the last processed CSV filename. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $matches = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the regular expression used to load the files with. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string The regular expression |
|
55
|
|
|
*/ |
|
56
|
3 |
|
protected function getRegex() |
|
57
|
|
|
{ |
|
58
|
3 |
|
return $this->regex; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the number of matches found. |
|
63
|
|
|
* |
|
64
|
|
|
* @return integer The number of matches |
|
65
|
|
|
*/ |
|
66
|
3 |
|
protected function countMatches() |
|
67
|
|
|
{ |
|
68
|
3 |
|
return sizeof($this->matches); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Adds the passed match to the array with the matches. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $match The match itself |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function addMatch(array $match) |
|
79
|
|
|
{ |
|
80
|
|
|
|
|
81
|
|
|
// lowercase all values of the passed match |
|
82
|
3 |
|
array_walk($match, function (&$val, &$key) { |
|
83
|
3 |
|
strtolower($key); |
|
84
|
3 |
|
}); |
|
85
|
|
|
|
|
86
|
|
|
// add the match |
|
87
|
3 |
|
$this->matches[] = $match; |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the match with the passed name. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name The name of the match to return |
|
94
|
|
|
* |
|
95
|
|
|
* @return string|null The match itself |
|
96
|
|
|
*/ |
|
97
|
3 |
|
protected function getMatch($name) |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
// create the key of the last match |
|
101
|
3 |
|
$key = sizeof($this->matches) - 1; |
|
102
|
|
|
|
|
103
|
|
|
// query whether or not a match is available |
|
104
|
3 |
|
if (isset($this->matches[$key])) { |
|
105
|
|
|
// load the last match |
|
106
|
3 |
|
$lastMatch = $this->matches[$key]; |
|
107
|
|
|
|
|
108
|
|
|
// query whether or not a value with the passed key is available |
|
109
|
3 |
|
if (isset($lastMatch[$name])) { |
|
110
|
3 |
|
return $lastMatch[$name]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the elements the filenames consists of, converted to lowercase. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array The array with the filename elements |
|
119
|
|
|
*/ |
|
120
|
3 |
|
protected function getPatternKeys() |
|
121
|
|
|
{ |
|
122
|
|
|
|
|
123
|
|
|
// load the pattern keys from the configuration |
|
124
|
3 |
|
$patternKeys = $this->getPatternElements(); |
|
125
|
|
|
|
|
126
|
|
|
// make sure that they are all lowercase |
|
127
|
3 |
|
array_walk($patternKeys, function (&$value) { |
|
128
|
3 |
|
$value = strtolower($value); |
|
129
|
3 |
|
}); |
|
130
|
|
|
|
|
131
|
|
|
// return the pattern keys |
|
132
|
3 |
|
return $patternKeys; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns the values to create the regex pattern from. |
|
137
|
|
|
* |
|
138
|
|
|
* @return array The array with the pattern values |
|
139
|
|
|
*/ |
|
140
|
3 |
|
protected function resolvePatternValues() |
|
141
|
|
|
{ |
|
142
|
|
|
|
|
143
|
|
|
// initialize the array |
|
144
|
3 |
|
$elements = array(); |
|
145
|
|
|
|
|
146
|
|
|
// load the pattern keys |
|
147
|
3 |
|
$patternKeys = $this->getPatternKeys(); |
|
148
|
|
|
|
|
149
|
|
|
// prepare the pattern values |
|
150
|
3 |
|
foreach ($patternKeys as $element) { |
|
151
|
3 |
|
$elements[] = sprintf('(?<%s>%s)', $element, $this->resolvePatternValue($element)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// return the pattern values |
|
155
|
3 |
|
return $elements; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Resolves the pattern value for the given element name. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $element The element name to resolve the pattern value for |
|
162
|
|
|
* |
|
163
|
|
|
* @return string|null The resolved pattern value |
|
164
|
|
|
*/ |
|
165
|
3 |
|
protected function resolvePatternValue($element) |
|
166
|
|
|
{ |
|
167
|
|
|
|
|
168
|
|
|
// query whether or not matches has been found OR the counter element has been passed |
|
169
|
3 |
|
if ($this->countMatches() === 0 || BunchKeys::COUNTER === $element) { |
|
170
|
|
|
// prepare the method name for the callback to load the pattern value with |
|
171
|
3 |
|
$methodName = sprintf('get%s', ucfirst($element)); |
|
172
|
|
|
|
|
173
|
|
|
// load the pattern value |
|
174
|
3 |
|
if (in_array($methodName, get_class_methods($this->getFileResolverConfiguration()))) { |
|
175
|
3 |
|
return call_user_func(array($this->getFileResolverConfiguration(), $methodName)); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// stop processing |
|
179
|
|
|
return; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// try to load the pattern value from the matches |
|
183
|
3 |
|
return $this->getMatch($element); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Prepares and returns the pattern for the regex to load the files from the |
|
188
|
|
|
* source directory for the passed subject. |
|
189
|
|
|
* |
|
190
|
|
|
* @return string The prepared regex pattern |
|
191
|
|
|
*/ |
|
192
|
3 |
|
protected function preparePattern() |
|
193
|
|
|
{ |
|
194
|
3 |
|
return sprintf($this->getRegex(), implode($this->getElementSeparator(), $this->resolvePatternValues()), $this->getSuffix()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Queries whether or not, the passed filename should be handled by the subject. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $filename The filename to query for |
|
201
|
|
|
* |
|
202
|
|
|
* @return boolean TRUE if the file should be handled, else FALSE |
|
203
|
|
|
*/ |
|
204
|
3 |
|
protected function shouldBeHandled($filename) |
|
205
|
|
|
{ |
|
206
|
|
|
|
|
207
|
|
|
// initialize the array with the matches |
|
208
|
3 |
|
$matches = array(); |
|
209
|
|
|
|
|
210
|
|
|
// update the matches, if the pattern matches |
|
211
|
3 |
|
if ($result = preg_match($this->preparePattern(), $filename, $matches)) { |
|
212
|
3 |
|
$this->addMatch($matches); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// stop processing, because the filename doesn't match the subjects pattern |
|
216
|
3 |
|
return (boolean) $result; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Resets the file resolver to parse another source directory for new files. |
|
221
|
|
|
* |
|
222
|
|
|
* @return void |
|
223
|
|
|
*/ |
|
224
|
1 |
|
public function reset() |
|
225
|
|
|
{ |
|
226
|
1 |
|
$this->matches = array(); |
|
227
|
1 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns the matches. |
|
231
|
|
|
* |
|
232
|
|
|
* @return array The array with the matches |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getMatches() |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->matches; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|