Completed
Pull Request — develop_3.0 (#431)
by Adrien
02:35
created

Cell::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Box\Spout\Writer\Common\Entity;
4
5
use Box\Spout\Writer\Common\Helper\CellHelper;
6
7
/**
8
 * Class Cell
9
 *
10
 * @package Box\Spout\Writer\Common\Entity
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 = null;
50
51
    /**
52
     * The cell type
53
     * @var int|null
54
     */
55
    protected $type = null;
56
57
    /**
58
     * Cell constructor.
59
     * @param $value mixed
60
     */
61 65
    public function __construct($value)
62
    {
63 65
        $this->setValue($value);
64 65
    }
65
66
    /**
67
     * @param $value mixed|null
68
     */
69 65
    public function setValue($value)
70
    {
71 65
        $this->value = $value;
72 65
        $this->type = $this->detectType($value);
73 65
    }
74
75
    /**
76
     * @return mixed|null
77
     */
78 64
    public function getValue()
79
    {
80 64
        return $this->value;
81
    }
82
83
    /**
84
     * @return int|null
85
     */
86
    public function getType()
87
    {
88
        return $this->type;
89
    }
90
91
    /**
92
     * Get the current value type
93
     * @param mixed|null $value
94
     * @return int
95
     */
96 65
    protected function detectType($value)
97
    {
98 65
        if (CellHelper::isBoolean($value)) {
99 5
            return self::TYPE_BOOLEAN;
100 64
        } elseif (CellHelper::isEmpty($value)) {
101 4
            return self::TYPE_EMPTY;
102 63
        } elseif (CellHelper::isNumeric($this->getValue())) {
103 6
            return self::TYPE_NUMERIC;
104 62
        } elseif (CellHelper::isNonEmptyString($value)) {
105 60
            return self::TYPE_STRING;
106
        } else {
107 4
            return self::TYPE_ERROR;
108
        }
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 12
    public function isBoolean()
115
    {
116 12
        return $this->type === self::TYPE_BOOLEAN;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122 8
    public function isEmpty()
123
    {
124 8
        return $this->type === self::TYPE_EMPTY;
125
    }
126
127
    /**
128
     * Not used at the moment
129
     * @return bool
130
     */
131
    public function isFormula()
132
    {
133
        return $this->type === self::TYPE_FORMULA;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139 11
    public function isNumeric()
140
    {
141 11
        return $this->type === self::TYPE_NUMERIC;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 64
    public function isString()
148
    {
149 64
        return $this->type === self::TYPE_STRING;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isError()
156
    {
157
        return $this->type === self::TYPE_ERROR;
158
    }
159
160
    /**
161
     * @return string
162
     */
163 1
    public function __toString()
164
    {
165 1
        return (string)$this->value;
166
    }
167
}
168