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 |
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
|
87 |
|
public function __construct($value, Style $style = null) |
73
|
|
|
{ |
74
|
87 |
|
$this->setValue($value); |
75
|
87 |
|
$this->setStyle($style); |
76
|
87 |
|
$this->styleMerger = new StyleMerger(); |
77
|
87 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed|null $value |
81
|
|
|
*/ |
82
|
87 |
|
public function setValue($value) |
83
|
|
|
{ |
84
|
87 |
|
$this->value = $value; |
85
|
87 |
|
$this->type = $this->detectType($value); |
86
|
87 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return mixed|null |
90
|
|
|
*/ |
91
|
84 |
|
public function getValue() |
92
|
|
|
{ |
93
|
84 |
|
return $this->value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Style|null $style |
98
|
|
|
*/ |
99
|
87 |
|
public function setStyle($style) |
100
|
|
|
{ |
101
|
87 |
|
$this->style = $style ?: new Style(); |
102
|
87 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Style |
106
|
|
|
*/ |
107
|
64 |
|
public function getStyle() |
108
|
|
|
{ |
109
|
64 |
|
return $this->style; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int|null |
114
|
|
|
*/ |
115
|
|
|
public function getType() |
116
|
|
|
{ |
117
|
|
|
return $this->type; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the current value type |
122
|
|
|
* |
123
|
|
|
* @param mixed|null $value |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
87 |
|
protected function detectType($value) |
127
|
|
|
{ |
128
|
87 |
|
if (CellHelper::isBoolean($value)) { |
129
|
4 |
|
return self::TYPE_BOOLEAN; |
130
|
|
|
} |
131
|
85 |
|
if (CellHelper::isEmpty($value)) { |
132
|
8 |
|
return self::TYPE_EMPTY; |
133
|
|
|
} |
134
|
82 |
|
if (CellHelper::isNumeric($this->getValue())) { |
135
|
4 |
|
return self::TYPE_NUMERIC; |
136
|
|
|
} |
137
|
80 |
|
if (CellHelper::isNonEmptyString($value)) { |
138
|
77 |
|
return self::TYPE_STRING; |
139
|
|
|
} |
140
|
|
|
|
141
|
5 |
|
return self::TYPE_ERROR; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
11 |
|
public function isBoolean() |
148
|
|
|
{ |
149
|
11 |
|
return $this->type === self::TYPE_BOOLEAN; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
23 |
|
public function isEmpty() |
156
|
|
|
{ |
157
|
23 |
|
return $this->type === self::TYPE_EMPTY; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Not used at the moment |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function isFormula() |
166
|
|
|
{ |
167
|
|
|
return $this->type === self::TYPE_FORMULA; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
10 |
|
public function isNumeric() |
174
|
|
|
{ |
175
|
10 |
|
return $this->type === self::TYPE_NUMERIC; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
63 |
|
public function isString() |
182
|
|
|
{ |
183
|
63 |
|
return $this->type === self::TYPE_STRING; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
1 |
|
public function isError() |
190
|
|
|
{ |
191
|
1 |
|
return $this->type === self::TYPE_ERROR; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
6 |
|
public function __toString() |
198
|
|
|
{ |
199
|
6 |
|
return (string) $this->value; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|