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
|
|
|
* Stop's observer execution on the actual row. |
51
|
|
|
* |
52
|
|
|
* @param boolean $skip The flag to skip row exection or not |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
1 |
|
public function skipRow($skip = true) |
57
|
|
|
{ |
58
|
1 |
|
$this->skipRow = $skip; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Query's whether or not the observer execution for the given row has to be skipped. |
63
|
|
|
* |
64
|
|
|
* @return boolean TRUE if the observer execution has to be skipped, else FALSE |
65
|
|
|
*/ |
66
|
|
|
public function rowHasToBeSkipped() |
67
|
|
|
{ |
68
|
|
|
return $this->skipRow; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set's the actual row, that has to be processed. |
73
|
|
|
* |
74
|
|
|
* @param array $row The row |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
10 |
|
public function setRow(array $row) |
79
|
|
|
{ |
80
|
10 |
|
$this->row = $row; |
81
|
10 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return's the actual row. |
85
|
|
|
* |
86
|
|
|
* @return array The actual row |
87
|
|
|
*/ |
88
|
14 |
|
public function getRow() |
89
|
|
|
{ |
90
|
14 |
|
return $this->row; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Query whether or not a value for the column with the passed name exists. |
95
|
|
|
* |
96
|
|
|
* @param string $name The column name to query for a valid value |
97
|
|
|
* |
98
|
|
|
* @return boolean TRUE if the value is set, else FALSE |
99
|
|
|
*/ |
100
|
2 |
|
public function hasValue($name) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
// query whether or not the header is available |
104
|
2 |
|
if ($this->hasHeader($name)) { |
|
|
|
|
105
|
|
|
// load the key for the row |
106
|
1 |
|
$headerValue = $this->getHeader($name); |
|
|
|
|
107
|
|
|
|
108
|
|
|
// query whether the rows column has a vaild value |
109
|
1 |
|
return (isset($this->row[$headerValue]) && $this->row[$headerValue] !== ''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// return FALSE if not |
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the value in the passed column name. |
118
|
|
|
* |
119
|
|
|
* @param string $name The column name to set the value for |
120
|
|
|
* @param mixed $value The value to set |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
4 |
|
protected function setValue($name, $value) |
125
|
|
|
{ |
126
|
4 |
|
$this->row[$this->getHeader($name)] = $value; |
|
|
|
|
127
|
4 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Resolve's the value with the passed colum name from the actual row. If a callback will |
131
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
132
|
|
|
* the value is NULL or empty, the default value will be returned. |
133
|
|
|
* |
134
|
|
|
* @param string $name The name of the column to return the value for |
135
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
136
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
137
|
|
|
* |
138
|
|
|
* @return mixed|null The, almost formatted, value |
139
|
|
|
*/ |
140
|
15 |
|
public function getValue($name, $default = null, callable $callback = null) |
141
|
|
|
{ |
142
|
|
|
|
143
|
|
|
// initialize the value |
144
|
15 |
|
$value = null; |
145
|
|
|
|
146
|
|
|
// query whether or not the header is available |
147
|
15 |
|
if ($this->hasHeader($name)) { |
|
|
|
|
148
|
|
|
// load the header value |
149
|
7 |
|
$headerValue = $this->getHeader($name); |
|
|
|
|
150
|
|
|
// query wheter or not, the value with the requested key is available |
151
|
7 |
|
if ((isset($this->row[$headerValue]) && $this->row[$headerValue] !== '')) { |
152
|
7 |
|
$value = $this->row[$headerValue]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// query whether or not, a callback has been passed |
157
|
15 |
|
if ($value != null && is_callable($callback)) { |
158
|
1 |
|
$value = call_user_func($callback, $value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// query whether or not |
162
|
15 |
|
if ($value == null && $default !== null) { |
163
|
1 |
|
$value = $default; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// return the value |
167
|
15 |
|
return $value; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.