Passed
Branch master (f23e33)
by Stefan
03:05
created

Style::setSurroundingBorder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace OneSheet\Style;
4
5
use OneSheet\Xml\StyleXml;
6
7
/**
8
 * Class Style
9
 *
10
 * @package OneSheet
11
 */
12
class Style implements Component
13
{
14
    /**
15
     * @var int
16
     */
17
    private $id;
18
19
    /**
20
     * @var Font
21
     */
22
    private $font;
23
24
    /**
25
     * @var Fill
26
     */
27
    private $fill;
28
29
    /**
30
     * @var Border
31
     */
32
    private $border;
33
34
    /**
35
     * @var bool
36
     */
37
    private $isLocked = false;
38
39
    /**
40
     * Style constructor.
41
     */
42 15
    public function __construct()
43
    {
44 15
        $this->font = new Font();
45 15
        $this->fill = new Fill();
46 15
        $this->border = new Border();
47 15
    }
48
49
    /**
50
     * @return int|null
51
     */
52 11
    public function getId()
53
    {
54 11
        return $this->id;
55
    }
56
57
    /**
58
     * @param int $id
59
     * @return Style
60
     */
61 8
    public function setId($id)
62
    {
63 8
        $this->id = $id;
64 8
        return $this;
65
    }
66
67
    /**
68
     * @return Font
69
     */
70 12
    public function getFont()
71
    {
72 12
        if ($this->isLocked) {
73 3
            return clone $this->font;
74
        }
75 12
        return $this->font;
76
    }
77
78
    /**
79
     * @return Fill
80
     */
81 9
    public function getFill()
82
    {
83 9
        if ($this->isLocked) {
84 1
            return clone $this->fill;
85
        }
86 9
        return $this->fill;
87
    }
88
89
    /**
90
     * @return Border
91
     */
92 10
    public function getBorder()
93
    {
94 10
        if ($this->isLocked) {
95
            return clone $this->border;
96
        }
97 10
        return $this->border;
98
    }
99
100
    /**
101
     * @param string $name
102
     * @return Style
103
     */
104 2
    public function setFontName($name)
105
    {
106 2
        $this->getFont()->setName($name);
107 2
        return $this;
108
    }
109
110
    /**
111
     * @param string $size
112
     * @return Style
113
     */
114 2
    public function setFontSize($size)
115
    {
116 2
        $this->getFont()->setSize($size);
117 2
        return $this;
118
    }
119
120
    /**
121
     * @param string $color
122
     * @return Style
123
     */
124 1
    public function setFontColor($color)
125
    {
126 1
        $this->getFont()->setColor($color);
127 1
        return $this;
128
    }
129
130
    /**
131
     * @return Style
132
     */
133 2
    public function setFontBold()
134
    {
135 2
        $this->getFont()->setBold();
136 2
        return $this;
137
    }
138
139
    /**
140
     * @return Style
141
     */
142 1
    public function setFontItalic()
143
    {
144 1
        $this->getFont()->setItalic();
145 1
        return $this;
146
    }
147
148
    /**
149
     * @return Style
150
     */
151 1
    public function setFontUnderline()
152
    {
153 1
        $this->getFont()->setUnderline();
154 1
        return $this;
155
    }
156
157
    /**
158
     * @return Style
159
     */
160 1
    public function setFontStrikethrough()
161
    {
162 1
        $this->getFont()->setStrikethrough();
163 1
        return $this;
164
    }
165
166
    /**
167
     * @param string $color
168
     * @return Style
169
     */
170 2
    public function setFillColor($color)
171
    {
172 2
        $this->getFill()->setColor($color);
173 2
        return $this;
174
    }
175
176
    /**
177
     * @param string $pattern
178
     * @return Style
179
     */
180 9
    public function setFillPattern($pattern)
181
    {
182 9
        $this->getFill()->setPattern($pattern);
183 9
        return $this;
184
    }
185
186
    /**
187
     * Convenience method to set all borders at once.
188
     *
189
     * @param string $style
190
     * @param string $color
191
     * @return Style
192
     */
193 1
    public function setSurroundingBorder($style = BorderStyle::THIN, $color = '000000')
194
    {
195 1
        $this->setBorderLeft($style, $color);
196 1
        $this->setBorderRight($style, $color);
197 1
        $this->setBorderTop($style, $color);
198 1
        $this->setBorderBottom($style, $color);
199 1
        return $this;
200
    }
201
    
202
    /**
203
     * @param string $style
204
     * @param string $color
205
     * @return Style
206
     */
207 1
    public function setBorderLeft($style = BorderStyle::THIN, $color = '000000')
208
    {
209 1
        $this->getBorder()->set(BorderType::LEFT, $style, $color);
210 1
        return $this;
211
    }
212
213
    /**
214
     * @param string $style
215
     * @param string $color
216
     * @return Style
217
     */
218 1
    public function setBorderRight($style = BorderStyle::THIN, $color = '000000')
219
    {
220 1
        $this->getBorder()->set(BorderType::RIGHT, $style, $color);
221 1
        return $this;
222
    }
223
224
    /**
225
     * @param string $style
226
     * @param string $color
227
     * @return Style
228
     */
229 1
    public function setBorderTop($style = BorderStyle::THIN, $color = '000000')
230
    {
231 1
        $this->getBorder()->set(BorderType::TOP, $style, $color);
232 1
        return $this;
233
    }
234
235
    /**
236
     * @param string $style
237
     * @param string $color
238
     * @return Style
239
     */
240 1
    public function setBorderBottom($style = BorderStyle::THIN, $color = '000000')
241
    {
242 1
        $this->getBorder()->set(BorderType::BOTTOM, $style, $color);
243 1
        return $this;
244
    }
245
246
    /**
247
     * @param string $style
248
     * @param string $color
249
     * @return Style
250
     */
251 1
    public function setBorderDiagonalUp($style = BorderStyle::THIN, $color = '000000')
252
    {
253 1
        $this->getBorder()->set(BorderType::DIAGONAL, $style, $color, BorderType::DIRECTION_UP);
254 1
        return $this;
255
    }
256
257
    /**
258
     * @param string $style
259
     * @param string $color
260
     * @return Style
261
     */
262 1
    public function setBorderDiagonalDown($style = BorderStyle::THIN, $color = '000000')
263
    {
264 1
        $this->getBorder()->set(BorderType::DIAGONAL, $style, $color, BorderType::DIRECTION_DOWN);
265 1
        return $this;
266
    }
267
268
    /**
269
     * Lock current style to prevent overwriting of existing styles.
270
     *
271
     * @return Style
272
     */
273 8
    public function lock()
274
    {
275 8
        $this->isLocked = true;
276 8
        return $this;
277
    }
278
279
    /**
280
     * Return single <xf> string for current style.
281
     *
282
     * @return string
283
     */
284 8
    public function asXml()
285
    {
286 8
        return sprintf(
287 8
            StyleXml::DEFAULT_XF_XML,
288 8
            $this->font->getId(),
289 8
            $this->fill->getId(),
290 8
            $this->border->getId()
291 8
        );
292
    }
293
}
294