Passed
Pull Request — develop_3.0 (#506)
by Adrien
03:07
created

Cell::detectType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 5
eloc 10
nc 5
nop 1
crap 5
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
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
     * Error cell type
41
     */
42
    const TYPE_ERROR = 5;
43
44
    /**
45
     * The value of this cell
46
     * @var mixed|null
47
     */
48
    protected $value;
49
50
    /**
51
     * The cell type
52
     * @var int|null
53
     */
54
    protected $type;
55
56
    /**
57
     * The cell style
58
     * @var Style
59
     */
60
    protected $style;
61
62
    /**
63
     * @param $value mixed
64
     * @param Style|null $style
65
     */
66 90
    public function __construct($value, Style $style = null)
67
    {
68 90
        $this->setValue($value);
69 90
        $this->setStyle($style);
70 90
    }
71
72
    /**
73
     * @param mixed|null $value
74
     */
75 90
    public function setValue($value)
76
    {
77 90
        $this->value = $value;
78 90
        $this->type = $this->detectType($value);
79 90
    }
80
81
    /**
82
     * @return mixed|null
83
     */
84 87
    public function getValue()
85
    {
86 87
        return $this->value;
87
    }
88
89
    /**
90
     * @param Style|null $style
91
     */
92 90
    public function setStyle($style)
93
    {
94 90
        $this->style = $style ?: new Style();
95 90
    }
96
97
    /**
98
     * @return Style
99
     */
100 66
    public function getStyle()
101
    {
102 66
        return $this->style;
103
    }
104
105
    /**
106
     * @return int|null
107
     */
108
    public function getType()
109
    {
110
        return $this->type;
111
    }
112
113
    /**
114
     * Get the current value type
115
     *
116
     * @param mixed|null $value
117
     * @return int
118
     */
119 90
    protected function detectType($value)
120
    {
121 90
        if (CellHelper::isBoolean($value)) {
122 4
            return self::TYPE_BOOLEAN;
123
        }
124 88
        if (CellHelper::isEmpty($value)) {
125 8
            return self::TYPE_EMPTY;
126
        }
127 85
        if (CellHelper::isNumeric($this->getValue())) {
128 4
            return self::TYPE_NUMERIC;
129
        }
130 83
        if (CellHelper::isNonEmptyString($value)) {
131 80
            return self::TYPE_STRING;
132
        }
133
134 5
        return self::TYPE_ERROR;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140 11
    public function isBoolean()
141
    {
142 11
        return $this->type === self::TYPE_BOOLEAN;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148 23
    public function isEmpty()
149
    {
150 23
        return $this->type === self::TYPE_EMPTY;
151
    }
152
153
    /**
154
     * Not used at the moment
155
     *
156
     * @return bool
157
     */
158
    public function isFormula()
159
    {
160
        return $this->type === self::TYPE_FORMULA;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 10
    public function isNumeric()
167
    {
168 10
        return $this->type === self::TYPE_NUMERIC;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174 65
    public function isString()
175
    {
176 65
        return $this->type === self::TYPE_STRING;
177
    }
178
179
    /**
180
     * @return bool
181
     */
182 1
    public function isError()
183
    {
184 1
        return $this->type === self::TYPE_ERROR;
185
    }
186
187
    /**
188
     * @return string
189
     */
190 7
    public function __toString()
191
    {
192 7
        return (string) $this->value;
193
    }
194
}
195