1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\RowTrait |
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; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A trait that provides row handling. |
25
|
|
|
* |
26
|
|
|
* @author Tim Wagner <[email protected]> |
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link https://github.com/techdivision/import |
30
|
|
|
* @link http://www.techdivision.com |
31
|
|
|
*/ |
32
|
|
|
trait RowTrait |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The actual row, that has to be processed. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $row = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The flag that stop's overserver execution on the actual row. |
44
|
|
|
* |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
protected $skipRow = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The number of rows that has been skipped. |
51
|
|
|
* |
52
|
|
|
* @var integer |
53
|
|
|
*/ |
54
|
|
|
protected $skippedRows = 0; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Stop's observer execution on the actual row. |
58
|
|
|
* |
59
|
|
|
* @param boolean $skip The flag to skip row exection or not |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
1 |
|
public function skipRow($skip = true) |
64
|
|
|
{ |
65
|
|
|
|
66
|
|
|
// raise the counter for the skipped rows |
67
|
1 |
|
if ($skip === true) { |
68
|
1 |
|
$this->skippedRows++; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// set the the flag to signal the row has been skipped |
72
|
1 |
|
$this->skipRow = $skip; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Query's whether or not the observer execution for the given row has to be skipped. |
77
|
|
|
* |
78
|
|
|
* @return boolean TRUE if the observer execution has to be skipped, else FALSE |
79
|
|
|
*/ |
80
|
|
|
public function rowHasToBeSkipped() |
81
|
|
|
{ |
82
|
|
|
return $this->skipRow; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set's the actual row, that has to be processed. |
87
|
|
|
* |
88
|
|
|
* @param array $row The row |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
10 |
|
public function setRow(array $row) |
93
|
|
|
{ |
94
|
10 |
|
$this->row = $row; |
95
|
10 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return's the actual row. |
99
|
|
|
* |
100
|
|
|
* @return array The actual row |
101
|
|
|
*/ |
102
|
14 |
|
public function getRow() |
103
|
|
|
{ |
104
|
14 |
|
return $this->row; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Query whether or not a value for the column with the passed name exists. |
109
|
|
|
* |
110
|
|
|
* @param string $name The column name to query for a valid value |
111
|
|
|
* |
112
|
|
|
* @return boolean TRUE if the value is set, else FALSE |
113
|
|
|
*/ |
114
|
2 |
|
public function hasValue($name) |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
// query whether or not the header is available |
118
|
2 |
|
if ($this->hasHeader($name)) { |
119
|
|
|
// load the key for the row |
120
|
1 |
|
$headerValue = $this->getHeader($name); |
121
|
|
|
|
122
|
|
|
// query whether the rows column has a vaild value |
123
|
1 |
|
return (isset($this->row[$headerValue]) && $this->row[$headerValue] !== ''); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// return FALSE if not |
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the value in the passed column name. |
132
|
|
|
* |
133
|
|
|
* @param string $name The column name to set the value for |
134
|
|
|
* @param mixed $value The value to set |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
4 |
|
protected function setValue($name, $value) |
139
|
|
|
{ |
140
|
4 |
|
$this->row[$this->getHeader($name)] = $value; |
141
|
4 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Resolve's the value with the passed colum name from the actual row. If a callback will |
145
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
146
|
|
|
* the value is NULL or empty, the default value will be returned. |
147
|
|
|
* |
148
|
|
|
* @param string $name The name of the column to return the value for |
149
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
150
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
151
|
|
|
* |
152
|
|
|
* @return mixed|null The, almost formatted, value |
153
|
|
|
*/ |
154
|
15 |
|
public function getValue($name, $default = null, callable $callback = null) |
155
|
|
|
{ |
156
|
|
|
|
157
|
|
|
// initialize the value |
158
|
15 |
|
$value = null; |
159
|
|
|
|
160
|
|
|
// query whether or not the header is available |
161
|
15 |
|
if ($this->hasHeader($name)) { |
162
|
|
|
// load the header value |
163
|
7 |
|
$headerValue = $this->getHeader($name); |
164
|
|
|
// query wheter or not, the value with the requested key is available |
165
|
7 |
|
if ((isset($this->row[$headerValue]) && $this->row[$headerValue] !== '')) { |
166
|
7 |
|
$value = $this->row[$headerValue]; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// query whether or not, a callback has been passed |
171
|
15 |
|
if ($value != null && is_callable($callback)) { |
172
|
1 |
|
$value = call_user_func($callback, $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// query whether or not |
176
|
15 |
|
if ($value == null && $default !== null) { |
177
|
1 |
|
$value = $default; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// return the value |
181
|
15 |
|
return $value; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return's the number of skipped rows. |
186
|
|
|
* |
187
|
|
|
* @return int The number of skipped rows |
188
|
|
|
*/ |
189
|
4 |
|
public function getSkippedRows() |
190
|
|
|
{ |
191
|
4 |
|
return $this->skippedRows; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* This method queries whether or not the column with the passed name is available or |
196
|
|
|
* not. This method uses the `isset()` function to make sure the column is available |
197
|
|
|
* and has not been removed somehow before, because it has an empty value for example. |
198
|
|
|
* |
199
|
|
|
* @param string $name The name of the column to query for |
200
|
|
|
* |
201
|
|
|
* @return boolean TRUE if the columen is available, FALSE otherwise |
202
|
|
|
*/ |
203
|
|
|
public function hasColumn(string $name) : bool |
204
|
|
|
{ |
205
|
|
|
|
206
|
|
|
// query whether or not the header is available, if yes try |
207
|
|
|
// to load the key and query whether the column is available |
208
|
|
|
if ($this->hasHeader($name)) { |
209
|
|
|
return isset($this->row[$this->getHeader($name)]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// return FALSE if not |
213
|
|
|
return false; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Queries whether or not the header with the passed name is available. |
218
|
|
|
* |
219
|
|
|
* @param string $name The header name to query |
220
|
|
|
* |
221
|
|
|
* @return boolean TRUE if the header is available, else FALSE |
222
|
|
|
*/ |
223
|
|
|
abstract public function hasHeader($name); |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return's the header value for the passed name. |
227
|
|
|
* |
228
|
|
|
* @param string $name The name of the header to return the value for |
229
|
|
|
* |
230
|
|
|
* @return mixed The header value |
231
|
|
|
* @throws \InvalidArgumentException Is thrown, if the header with the passed name is NOT available |
232
|
|
|
*/ |
233
|
|
|
abstract public function getHeader($name); |
234
|
|
|
} |
235
|
|
|
|