Passed
Pull Request — develop_3.0 (#510)
by Adrien
02:39
created

Cell::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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