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