Completed
Pull Request — master (#383)
by Hura
14:11
created

Cell::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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