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