Passed
Push — develop_3.0 ( fec27e...727422 )
by Adrien
02:46
created

Cell::applyStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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