|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fwlib\Html\ListView; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Html\ListView\Exception\InvalidFitModeException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @copyright Copyright 2015 Fwolf |
|
8
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
9
|
|
|
*/ |
|
10
|
|
|
class Fitter implements FitterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Empty filler |
|
14
|
|
|
* |
|
15
|
|
|
* In body fit process, newly created key will use this as value. This |
|
16
|
|
|
* will not affect head, which will use key as filler. |
|
17
|
|
|
* |
|
18
|
|
|
* Default value also set in ListView configs with key 'fitEmptyFiller'. |
|
19
|
|
|
* |
|
20
|
|
|
* @see ListView::getDefaultConfigs() |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $emptyFiller = ' '; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fit mode |
|
28
|
|
|
* |
|
29
|
|
|
* Default value also set in ListView configs with key 'fitMode'. |
|
30
|
|
|
* |
|
31
|
|
|
* @see ListView::getDefaultConfigs() |
|
32
|
|
|
* @see FitMode |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $mode = FitMode::TO_TITLE; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
* |
|
42
|
|
|
* Skip if their key are same, or either is empty. |
|
43
|
|
|
* |
|
44
|
|
|
* @throws InvalidFitModeException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function fit(ListDto $listDto) |
|
47
|
|
|
{ |
|
48
|
|
|
$listBody = $listDto->getBody(); |
|
49
|
|
|
$listHead = $listDto->getHead(); |
|
50
|
|
|
|
|
51
|
|
|
if (empty($listBody) || empty($listHead)) { |
|
52
|
|
|
return $listDto; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$bodyKeys = array_keys(current($listBody)); |
|
56
|
|
|
$headKeys = array_keys($listHead); |
|
57
|
|
|
|
|
58
|
|
|
if ($bodyKeys == $headKeys) { |
|
59
|
|
|
return $listDto; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
switch ($this->mode) { |
|
|
|
|
|
|
63
|
|
|
case FitMode::TO_TITLE: |
|
64
|
|
|
$fittedKeys = $headKeys; |
|
65
|
|
|
break; |
|
66
|
|
|
|
|
67
|
|
|
case FitMode::TO_DATA: |
|
68
|
|
|
$fittedKeys = $bodyKeys; |
|
69
|
|
|
break; |
|
70
|
|
|
|
|
71
|
|
|
case FitMode::INTERSECTION: |
|
72
|
|
|
$fittedKeys = array_intersect($headKeys, $bodyKeys); |
|
73
|
|
|
break; |
|
74
|
|
|
|
|
75
|
|
|
case FitMode::UNION: |
|
76
|
|
|
$fittedKeys = |
|
77
|
|
|
array_unique(array_merge($headKeys, $bodyKeys)); |
|
78
|
|
|
break; |
|
79
|
|
|
|
|
80
|
|
|
default: |
|
81
|
|
|
throw new InvalidFitModeException; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->fitHead($listDto, $fittedKeys); |
|
85
|
|
|
$this->fitBody($listDto, $fittedKeys); |
|
86
|
|
|
|
|
87
|
|
|
return $listDto; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Fit each row in body with given keys |
|
93
|
|
|
* |
|
94
|
|
|
* If row index is not in given keys, it will be dropped. If given keys is |
|
95
|
|
|
* not in row index, it will be created with filling value. |
|
96
|
|
|
* |
|
97
|
|
|
* @param ListDto $listDto |
|
98
|
|
|
* @param array $keys |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function fitBody(ListDto $listDto, array $keys) |
|
101
|
|
|
{ |
|
102
|
|
|
$listBody = $listDto->getBody(); |
|
103
|
|
|
|
|
104
|
|
|
// Use first row in body as sample, need not scan all rows |
|
105
|
|
|
$sampleRow = current($listBody); |
|
106
|
|
|
|
|
107
|
|
|
$keysToDel = array_diff(array_keys($sampleRow), $keys); |
|
108
|
|
|
|
|
109
|
|
|
$keysToAdd = array_diff($keys, array_keys($sampleRow)); |
|
110
|
|
|
|
|
111
|
|
|
if (empty($keysToAdd) && empty($keysToDel)) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$deleteDummy = array_fill_keys($keys, null); |
|
116
|
|
|
$addDummy = array_fill_keys($keysToAdd, $this->emptyFiller); |
|
117
|
|
|
foreach ($listBody as &$row) { |
|
118
|
|
|
$row = array_intersect_key($row, $deleteDummy); |
|
119
|
|
|
$row = array_merge($row, $addDummy); |
|
120
|
|
|
} |
|
121
|
|
|
unset($row); |
|
122
|
|
|
|
|
123
|
|
|
$listDto->setBody($listBody); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Fit head with given keys |
|
129
|
|
|
* |
|
130
|
|
|
* Drop head key not in given keys, and create new if given keys is not |
|
131
|
|
|
* exists in head array. |
|
132
|
|
|
* |
|
133
|
|
|
* @param ListDto $listDto |
|
134
|
|
|
* @param array $keys |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function fitHead(ListDto $listDto, array $keys) |
|
137
|
|
|
{ |
|
138
|
|
|
$listHead = $listDto->getHead(); |
|
139
|
|
|
|
|
140
|
|
|
// Head index not in keys list |
|
141
|
|
|
$listHead = array_intersect_key( |
|
142
|
|
|
$listHead, |
|
143
|
|
|
array_fill_keys($keys, null) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
// Add keys not exist in head |
|
147
|
|
|
foreach ($keys as $key) { |
|
148
|
|
|
if (!array_key_exists($key, $listHead)) { |
|
149
|
|
|
$listHead[$key] = ucfirst($key); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$listDto->setHead($listHead); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setEmptyFiller($emptyFiller) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->emptyFiller = $emptyFiller; |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setMode($mode) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->mode = $mode; |
|
174
|
|
|
|
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.