|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phperf\Pipeline\Rows; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use IteratorIterator; |
|
7
|
|
|
|
|
8
|
|
|
class Processor extends IteratorIterator |
|
9
|
|
|
{ |
|
10
|
|
|
protected $rows = array(); |
|
11
|
|
|
private $skipFields = array(); |
|
12
|
|
|
|
|
13
|
|
|
public function skipField($field) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->skipFields[$field] = $field; |
|
16
|
|
|
return $this; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
private $changeKeys = array(); |
|
20
|
|
|
|
|
21
|
|
|
public function changeKey($fieldFrom, $fieldTo) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->changeKeys[$fieldFrom] = $fieldTo; |
|
24
|
|
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
private $combineFields = array(); |
|
29
|
|
|
|
|
30
|
|
|
public function combine($fieldKey, $fieldValue) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->combineFields[$fieldKey] = $fieldValue; |
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @var \Closure */ |
|
37
|
|
|
private $callback; |
|
38
|
|
|
public function map(callable $callback) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->callback = $callback; |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private $combineOffset = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param integer $offsetKey |
|
48
|
|
|
* @param integer $offsetValue |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
|
|
public function combineOffset($offsetKey, $offsetValue) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->combineOffset [$offsetKey] = $offsetValue; |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Processor constructor. |
|
59
|
|
|
* @param array|\Iterator $rows |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct($rows) |
|
62
|
|
|
{ |
|
63
|
|
|
if (is_array($rows)) { |
|
64
|
|
|
parent::__construct(new ArrayIterator($rows)); |
|
65
|
|
|
} else { |
|
66
|
|
|
parent::__construct($rows); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function current() |
|
71
|
|
|
{ |
|
72
|
|
|
$row = parent::current(); |
|
73
|
|
|
if ($this->callback) { |
|
74
|
|
|
$row = $this->callback->__invoke($row); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->skipFields) { |
|
|
|
|
|
|
78
|
|
|
foreach ($this->skipFields as $field) { |
|
79
|
|
|
if (isset($row[$field])) { |
|
80
|
|
|
unset($row[$field]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$keys = null; |
|
86
|
|
|
|
|
87
|
|
|
if ($this->combineOffset) { |
|
|
|
|
|
|
88
|
|
|
foreach ($this->combineOffset as $offsetKey => $offsetValue) { |
|
89
|
|
|
$keys = array_keys($row); |
|
90
|
|
|
$row[$row[$keys[$offsetKey]]] = $row[$keys[$offsetValue]]; |
|
91
|
|
|
unset($row[$keys[$offsetKey]]); |
|
92
|
|
|
unset($row[$keys[$offsetValue]]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($this->combineFields) { |
|
|
|
|
|
|
97
|
|
|
foreach ($this->combineFields as $fieldKey => $fieldValue) { |
|
98
|
|
|
$row[$row[$fieldKey]] = $row[$fieldValue]; |
|
99
|
|
|
unset($row[$fieldKey]); |
|
100
|
|
|
unset($row[$fieldValue]); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($this->changeKeys) { |
|
|
|
|
|
|
105
|
|
|
if (!$keys) { |
|
106
|
|
|
$keys = array_keys($row); |
|
107
|
|
|
} |
|
108
|
|
|
foreach ($keys as &$key) { |
|
109
|
|
|
if (isset($this->changeKeys[$key])) { |
|
110
|
|
|
$key = $this->changeKeys[$key]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
unset($key); |
|
114
|
|
|
$row = array_combine($keys, $row); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $row; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array|\Iterator $rows |
|
123
|
|
|
* @return static |
|
124
|
|
|
*/ |
|
125
|
|
|
static function create($rows) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
return new static($rows); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
public function exportArray() |
|
132
|
|
|
{ |
|
133
|
|
|
$result = array(); |
|
134
|
|
|
foreach ($this as $row) { |
|
135
|
|
|
$result [] = $row; |
|
136
|
|
|
} |
|
137
|
|
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.