1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Entity; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Writer\Common\Entity\Style\Style; |
6
|
|
|
use Box\Spout\Writer\Common\Helper\CellHelper; |
7
|
|
|
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Cell |
11
|
|
|
* |
12
|
|
|
* @package Box\Spout\Writer\Common\Entity |
13
|
|
|
*/ |
14
|
|
|
class Cell |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Numeric cell type (whole numbers, fractional numbers, dates) |
18
|
|
|
*/ |
19
|
|
|
const TYPE_NUMERIC = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* String (text) cell type |
23
|
|
|
*/ |
24
|
|
|
const TYPE_STRING = 1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Formula cell type |
28
|
|
|
* Not used at the moment |
29
|
|
|
*/ |
30
|
|
|
const TYPE_FORMULA = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Empty cell type |
34
|
|
|
*/ |
35
|
|
|
const TYPE_EMPTY = 3; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Boolean cell type |
39
|
|
|
*/ |
40
|
|
|
const TYPE_BOOLEAN = 4; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Error cell type |
44
|
|
|
*/ |
45
|
|
|
const TYPE_ERROR = 5; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The value of this cell |
49
|
|
|
* @var mixed|null |
50
|
|
|
*/ |
51
|
|
|
protected $value = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The cell type |
55
|
|
|
* @var int|null |
56
|
|
|
*/ |
57
|
|
|
protected $type = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The cell style |
61
|
|
|
* @var Style|null |
62
|
|
|
*/ |
63
|
|
|
protected $style = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var StyleMerger |
67
|
|
|
*/ |
68
|
|
|
protected $styleMerger; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Cell constructor. |
72
|
|
|
* @param $value mixed |
73
|
|
|
* @param $style|null Style |
74
|
|
|
*/ |
75
|
97 |
|
public function __construct($value, Style $style = null) |
76
|
|
|
{ |
77
|
97 |
|
$this->setValue($value); |
78
|
97 |
|
$this->setStyle($style); |
|
|
|
|
79
|
1 |
|
$this->styleMerger = new StyleMerger(); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $value mixed|null |
84
|
|
|
*/ |
85
|
97 |
|
public function setValue($value) |
86
|
|
|
{ |
87
|
97 |
|
$this->value = $value; |
88
|
97 |
|
$this->type = $this->detectType($value); |
89
|
97 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return mixed|null |
93
|
|
|
*/ |
94
|
91 |
|
public function getValue() |
95
|
|
|
{ |
96
|
91 |
|
return $this->value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Style $style |
101
|
|
|
*/ |
102
|
1 |
|
public function setStyle(Style $style) |
103
|
|
|
{ |
104
|
1 |
|
$this->style = $style; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Style|null |
109
|
|
|
*/ |
110
|
1 |
|
public function getStyle() |
111
|
|
|
{ |
112
|
1 |
|
if (!isset($this->style)) { |
113
|
|
|
$this->setStyle(new Style()); |
114
|
|
|
} |
115
|
1 |
|
return $this->style; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return int|null |
120
|
|
|
*/ |
121
|
|
|
public function getType() |
122
|
|
|
{ |
123
|
|
|
return $this->type; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the current value type |
128
|
|
|
* @param mixed|null $value |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
97 |
|
protected function detectType($value) |
132
|
|
|
{ |
133
|
97 |
|
if (CellHelper::isBoolean($value)) { |
134
|
2 |
|
return self::TYPE_BOOLEAN; |
135
|
95 |
|
} elseif (CellHelper::isEmpty($value)) { |
136
|
4 |
|
return self::TYPE_EMPTY; |
137
|
91 |
|
} elseif (CellHelper::isNumeric($this->getValue())) { |
138
|
2 |
|
return self::TYPE_NUMERIC; |
139
|
89 |
|
} elseif (CellHelper::isNonEmptyString($value)) { |
140
|
86 |
|
return self::TYPE_STRING; |
141
|
|
|
} else { |
142
|
3 |
|
return self::TYPE_ERROR; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function isBoolean() |
150
|
|
|
{ |
151
|
|
|
return $this->type === self::TYPE_BOOLEAN; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isEmpty() |
158
|
|
|
{ |
159
|
|
|
return $this->type === self::TYPE_EMPTY; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Not used at the moment |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function isFormula() |
167
|
|
|
{ |
168
|
|
|
return $this->type === self::TYPE_FORMULA; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function isNumeric() |
175
|
|
|
{ |
176
|
|
|
return $this->type === self::TYPE_NUMERIC; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
1 |
|
public function isString() |
183
|
|
|
{ |
184
|
1 |
|
return $this->type === self::TYPE_STRING; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function isError() |
191
|
|
|
{ |
192
|
|
|
return $this->type === self::TYPE_ERROR; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function __toString() |
199
|
|
|
{ |
200
|
|
|
return (string)$this->value; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param Style $style|null |
|
|
|
|
205
|
|
|
* @return Cell |
206
|
|
|
*/ |
207
|
|
|
public function applyStyle(Style $style = null) |
208
|
|
|
{ |
209
|
|
|
if ($style === null) { |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
$mergedStyle = $this->styleMerger->merge($this->getStyle(), $style); |
|
|
|
|
213
|
|
|
$this->setStyle($mergedStyle); |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):