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