Completed
Push — master ( eb88bb...7964da )
by Adrien
01:59
created

Cell::getValueEvenIfError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 184
    public function __construct($value, Style $style = null)
72
    {
73 184
        $this->setValue($value);
74 184
        $this->setStyle($style);
75 184
    }
76
77
    /**
78
     * @param mixed|null $value
79
     */
80 184
    public function setValue($value)
81
    {
82 184
        $this->value = $value;
83 184
        $this->type = $this->detectType($value);
84 184
    }
85
86
    /**
87
     * @return mixed|null
88
     */
89 159
    public function getValue()
90
    {
91 159
        return !$this->isError() ? $this->value : null;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97 6
    public function getValueEvenIfError()
98
    {
99 6
        return $this->value;
100
    }
101
102
    /**
103
     * @param Style|null $style
104
     */
105 184
    public function setStyle($style)
106
    {
107 184
        $this->style = $style ?: new Style();
108 184
    }
109
110
    /**
111
     * @return Style
112
     */
113 71
    public function getStyle()
114
    {
115 71
        return $this->style;
116
    }
117
118
    /**
119
     * @return int|null
120
     */
121
    public function getType()
122
    {
123
        return $this->type;
124
    }
125
126
    /**
127
     * @param int $type
128
     */
129 4
    public function setType($type)
130
    {
131 4
        $this->type = $type;
132 4
    }
133
134
    /**
135
     * Get the current value type
136
     *
137
     * @param mixed|null $value
138
     * @return int
139
     */
140 184
    protected function detectType($value)
141
    {
142 184
        if (CellTypeHelper::isBoolean($value)) {
143 7
            return self::TYPE_BOOLEAN;
144
        }
145 182
        if (CellTypeHelper::isEmpty($value)) {
146 58
            return self::TYPE_EMPTY;
147
        }
148 177
        if (CellTypeHelper::isNumeric($value)) {
149 19
            return self::TYPE_NUMERIC;
150
        }
151 173
        if (CellTypeHelper::isDateTimeOrDateInterval($value)) {
152 7
            return self::TYPE_DATE;
153
        }
154 168
        if (CellTypeHelper::isNonEmptyString($value)) {
155 164
            return self::TYPE_STRING;
156
        }
157
158 6
        return self::TYPE_ERROR;
159
    }
160
161
    /**
162
     * @return bool
163
     */
164 14
    public function isBoolean()
165
    {
166 14
        return $this->type === self::TYPE_BOOLEAN;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172 108
    public function isEmpty()
173
    {
174 108
        return $this->type === self::TYPE_EMPTY;
175
    }
176
177
    /**
178
     * @return bool
179
     */
180 13
    public function isNumeric()
181
    {
182 13
        return $this->type === self::TYPE_NUMERIC;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188 70
    public function isString()
189
    {
190 70
        return $this->type === self::TYPE_STRING;
191
    }
192
193
    /**
194
     * @return bool
195
     */
196 1
    public function isDate()
197
    {
198 1
        return $this->type === self::TYPE_DATE;
199
    }
200
201
    /**
202
     * @return bool
203
     */
204 162
    public function isError()
205
    {
206 162
        return $this->type === self::TYPE_ERROR;
207
    }
208
209
    /**
210
     * @return string
211
     */
212 7
    public function __toString()
213
    {
214 7
        return (string) $this->getValue();
215
    }
216
}
217