Passed
Push — develop_3.0 ( 139f7f...a665b9 )
by Adrien
02:36
created

Cell::isString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
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
     * 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 31
    public function __construct($value)
59
    {
60 31
        $this->setValue($value);
61 31
    }
62
63
    /**
64
     * @param mixed|null $value
65
     */
66 31
    public function setValue($value)
67
    {
68 31
        $this->value = $value;
69 31
        $this->type = $this->detectType($value);
70 31
    }
71
72
    /**
73
     * @return mixed|null
74
     */
75 28
    public function getValue()
76
    {
77 28
        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 31
    protected function detectType($value)
95
    {
96 31
        if (CellTypeHelper::isBoolean($value)) {
97 1
            return self::TYPE_BOOLEAN;
98
        }
99 30
        if (CellTypeHelper::isEmpty($value)) {
100 5
            return self::TYPE_EMPTY;
101
        }
102 28
        if (CellTypeHelper::isNumeric($this->getValue())) {
103 1
            return self::TYPE_NUMERIC;
104
        }
105 27
        if (CellTypeHelper::isNonEmptyString($value)) {
106 26
            return self::TYPE_STRING;
107
        }
108
109 1
        return self::TYPE_ERROR;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 1
    public function isBoolean()
116
    {
117 1
        return $this->type === self::TYPE_BOOLEAN;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123 2
    public function isEmpty()
124
    {
125 2
        return $this->type === self::TYPE_EMPTY;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131 1
    public function isNumeric()
132
    {
133 1
        return $this->type === self::TYPE_NUMERIC;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139 1
    public function isString()
140
    {
141 1
        return $this->type === self::TYPE_STRING;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 1
    public function isError()
148
    {
149 1
        return $this->type === self::TYPE_ERROR;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function __toString()
156
    {
157
        return (string) $this->value;
158
    }
159
}
160