1<?php
2
3namespace PhpOffice\PhpSpreadsheet\Chart;
4
5/**
6 * Created by PhpStorm.
7 * User: Wiktor Trzonkowski
8 * Date: 7/2/14
9 * Time: 2:36 PM.
10 */
11class GridLines extends Properties
12{
13    /**
14     * Properties of Class:
15     * Object State (State for Minor Tick Mark) @var bool
16     * Line Properties @var  array of mixed
17     * Shadow Properties @var  array of mixed
18     * Glow Properties @var  array of mixed
19     * Soft Properties @var  array of mixed.
20     */
21    private $objectState = false;
22
23    private $lineProperties = [
24        'color' => [
25            'type' => self::EXCEL_COLOR_TYPE_STANDARD,
26            'value' => null,
27            'alpha' => 0,
28        ],
29        'style' => [
30            'width' => '9525',
31            'compound' => self::LINE_STYLE_COMPOUND_SIMPLE,
32            'dash' => self::LINE_STYLE_DASH_SOLID,
33            'cap' => self::LINE_STYLE_CAP_FLAT,
34            'join' => self::LINE_STYLE_JOIN_BEVEL,
35            'arrow' => [
36                'head' => [
37                    'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
38                    'size' => self::LINE_STYLE_ARROW_SIZE_5,
39                ],
40                'end' => [
41                    'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
42                    'size' => self::LINE_STYLE_ARROW_SIZE_8,
43                ],
44            ],
45        ],
46    ];
47
48    private $shadowProperties = [
49        'presets' => self::SHADOW_PRESETS_NOSHADOW,
50        'effect' => null,
51        'color' => [
52            'type' => self::EXCEL_COLOR_TYPE_STANDARD,
53            'value' => 'black',
54            'alpha' => 85,
55        ],
56        'size' => [
57            'sx' => null,
58            'sy' => null,
59            'kx' => null,
60        ],
61        'blur' => null,
62        'direction' => null,
63        'distance' => null,
64        'algn' => null,
65        'rotWithShape' => null,
66    ];
67
68    private $glowProperties = [
69        'size' => null,
70        'color' => [
71            'type' => self::EXCEL_COLOR_TYPE_STANDARD,
72            'value' => 'black',
73            'alpha' => 40,
74        ],
75    ];
76
77    private $softEdges = [
78        'size' => null,
79    ];
80
81    /**
82     * Get Object State.
83     *
84     * @return bool
85     */
86    public function getObjectState()
87    {
88        return $this->objectState;
89    }
90
91    /**
92     * Change Object State to True.
93     *
94     * @return $this
95     */
96    private function activateObject()
97    {
98        $this->objectState = true;
99
100        return $this;
101    }
102
103    /**
104     * Set Line Color Properties.
105     *
106     * @param string $value
107     * @param int $alpha
108     * @param string $type
109     */
110    public function setLineColorProperties($value, $alpha = 0, $type = self::EXCEL_COLOR_TYPE_STANDARD): void
111    {
112        $this->activateObject()
113            ->lineProperties['color'] = $this->setColorProperties(
114                $value,
115                $alpha,
116                $type
117            );
118    }
119
120    /**
121     * Set Line Color Properties.
122     *
123     * @param float $line_width
124     * @param string $compound_type
125     * @param string $dash_type
126     * @param string $cap_type
127     * @param string $join_type
128     * @param string $head_arrow_type
129     * @param string $head_arrow_size
130     * @param string $end_arrow_type
131     * @param string $end_arrow_size
132     */
133    public function setLineStyleProperties($line_width = null, $compound_type = null, $dash_type = null, $cap_type = null, $join_type = null, $head_arrow_type = null, $head_arrow_size = null, $end_arrow_type = null, $end_arrow_size = null): void
134    {
135        $this->activateObject();
136        ($line_width !== null)
137                ? $this->lineProperties['style']['width'] = $this->getExcelPointsWidth((float) $line_width)
138                : null;
139        ($compound_type !== null)
140                ? $this->lineProperties['style']['compound'] = (string) $compound_type
141                : null;
142        ($dash_type !== null)
143                ? $this->lineProperties['style']['dash'] = (string) $dash_type
144                : null;
145        ($cap_type !== null)
146                ? $this->lineProperties['style']['cap'] = (string) $cap_type
147                : null;
148        ($join_type !== null)
149                ? $this->lineProperties['style']['join'] = (string) $join_type
150                : null;
151        ($head_arrow_type !== null)
152                ? $this->lineProperties['style']['arrow']['head']['type'] = (string) $head_arrow_type
153                : null;
154        ($head_arrow_size !== null)
155                ? $this->lineProperties['style']['arrow']['head']['size'] = (string) $head_arrow_size
156                : null;
157        ($end_arrow_type !== null)
158                ? $this->lineProperties['style']['arrow']['end']['type'] = (string) $end_arrow_type
159                : null;
160        ($end_arrow_size !== null)
161                ? $this->lineProperties['style']['arrow']['end']['size'] = (string) $end_arrow_size
162                : null;
163    }
164
165    /**
166     * Get Line Color Property.
167     *
168     * @param string $parameter
169     *
170     * @return string
171     */
172    public function getLineColorProperty($parameter)
173    {
174        return $this->lineProperties['color'][$parameter];
175    }
176
177    /**
178     * Get Line Style Property.
179     *
180     * @param array|string $elements
181     *
182     * @return string
183     */
184    public function getLineStyleProperty($elements)
185    {
186        return $this->getArrayElementsValue($this->lineProperties['style'], $elements);
187    }
188
189    /**
190     * Set Glow Properties.
191     *
192     * @param float $size
193     * @param string $color_value
194     * @param int $color_alpha
195     * @param string $color_type
196     */
197    public function setGlowProperties($size, $color_value = null, $color_alpha = null, $color_type = null): void
198    {
199        $this
200            ->activateObject()
201            ->setGlowSize($size)
202            ->setGlowColor($color_value, $color_alpha, $color_type);
203    }
204
205    /**
206     * Get Glow Color Property.
207     *
208     * @param string $property
209     *
210     * @return string
211     */
212    public function getGlowColor($property)
213    {
214        return $this->glowProperties['color'][$property];
215    }
216
217    /**
218     * Get Glow Size.
219     *
220     * @return string
221     */
222    public function getGlowSize()
223    {
224        return $this->glowProperties['size'];
225    }
226
227    /**
228     * Set Glow Size.
229     *
230     * @param float $size
231     *
232     * @return $this
233     */
234    private function setGlowSize($size)
235    {
236        $this->glowProperties['size'] = $this->getExcelPointsWidth((float) $size);
237
238        return $this;
239    }
240
241    /**
242     * Set Glow Color.
243     *
244     * @param string $color
245     * @param int $alpha
246     * @param string $type
247     *
248     * @return $this
249     */
250    private function setGlowColor($color, $alpha, $type)
251    {
252        if ($color !== null) {
253            $this->glowProperties['color']['value'] = (string) $color;
254        }
255        if ($alpha !== null) {
256            $this->glowProperties['color']['alpha'] = $this->getTrueAlpha((int) $alpha);
257        }
258        if ($type !== null) {
259            $this->glowProperties['color']['type'] = (string) $type;
260        }
261
262        return $this;
263    }
264
265    /**
266     * Get Line Style Arrow Parameters.
267     *
268     * @param string $arrow_selector
269     * @param string $property_selector
270     *
271     * @return string
272     */
273    public function getLineStyleArrowParameters($arrow_selector, $property_selector)
274    {
275        return $this->getLineStyleArrowSize($this->lineProperties['style']['arrow'][$arrow_selector]['size'], $property_selector);
276    }
277
278    /**
279     * Set Shadow Properties.
280     *
281     * @param int $sh_presets
282     * @param string $sh_color_value
283     * @param string $sh_color_type
284     * @param int $sh_color_alpha
285     * @param string $sh_blur
286     * @param int $sh_angle
287     * @param float $sh_distance
288     */
289    public function setShadowProperties($sh_presets, $sh_color_value = null, $sh_color_type = null, $sh_color_alpha = null, $sh_blur = null, $sh_angle = null, $sh_distance = null): void
290    {
291        $this->activateObject()
292            ->setShadowPresetsProperties((int) $sh_presets)
293            ->setShadowColor(
294                $sh_color_value ?? $this->shadowProperties['color']['value'],
295                $sh_color_alpha === null ? (int) $this->shadowProperties['color']['alpha'] : $this->getTrueAlpha($sh_color_alpha),
296                $sh_color_type ?? $this->shadowProperties['color']['type']
297            )
298            ->setShadowBlur($sh_blur)
299            ->setShadowAngle($sh_angle)
300            ->setShadowDistance($sh_distance);
301    }
302
303    /**
304     * Set Shadow Presets Properties.
305     *
306     * @param int $shadow_presets
307     *
308     * @return $this
309     */
310    private function setShadowPresetsProperties($shadow_presets)
311    {
312        $this->shadowProperties['presets'] = $shadow_presets;
313        $this->setShadowProperiesMapValues($this->getShadowPresetsMap($shadow_presets));
314
315        return $this;
316    }
317
318    /**
319     * Set Shadow Properties Values.
320     *
321     * @param mixed $reference
322     *
323     * @return $this
324     */
325    private function setShadowProperiesMapValues(array $properties_map, &$reference = null)
326    {
327        $base_reference = $reference;
328        foreach ($properties_map as $property_key => $property_val) {
329            if (is_array($property_val)) {
330                if ($reference === null) {
331                    $reference = &$this->shadowProperties[$property_key];
332                } else {
333                    $reference = &$reference[$property_key];
334                }
335                $this->setShadowProperiesMapValues($property_val, $reference);
336            } else {
337                if ($base_reference === null) {
338                    $this->shadowProperties[$property_key] = $property_val;
339                } else {
340                    $reference[$property_key] = $property_val;
341                }
342            }
343        }
344
345        return $this;
346    }
347
348    /**
349     * Set Shadow Color.
350     *
351     * @param string $color
352     * @param int $alpha
353     * @param string $type
354     *
355     * @return $this
356     */
357    private function setShadowColor($color, $alpha, $type)
358    {
359        if ($color !== null) {
360            $this->shadowProperties['color']['value'] = (string) $color;
361        }
362        if ($alpha !== null) {
363            $this->shadowProperties['color']['alpha'] = $this->getTrueAlpha((int) $alpha);
364        }
365        if ($type !== null) {
366            $this->shadowProperties['color']['type'] = (string) $type;
367        }
368
369        return $this;
370    }
371
372    /**
373     * Set Shadow Blur.
374     *
375     * @param float $blur
376     *
377     * @return $this
378     */
379    private function setShadowBlur($blur)
380    {
381        if ($blur !== null) {
382            $this->shadowProperties['blur'] = (string) $this->getExcelPointsWidth($blur);
383        }
384
385        return $this;
386    }
387
388    /**
389     * Set Shadow Angle.
390     *
391     * @param int $angle
392     *
393     * @return $this
394     */
395    private function setShadowAngle($angle)
396    {
397        if ($angle !== null) {
398            $this->shadowProperties['direction'] = (string) $this->getExcelPointsAngle($angle);
399        }
400
401        return $this;
402    }
403
404    /**
405     * Set Shadow Distance.
406     *
407     * @param float $distance
408     *
409     * @return $this
410     */
411    private function setShadowDistance($distance)
412    {
413        if ($distance !== null) {
414            $this->shadowProperties['distance'] = (string) $this->getExcelPointsWidth($distance);
415        }
416
417        return $this;
418    }
419
420    /**
421     * Get Shadow Property.
422     *
423     * @param string|string[] $elements
424     *
425     * @return string
426     */
427    public function getShadowProperty($elements)
428    {
429        return $this->getArrayElementsValue($this->shadowProperties, $elements);
430    }
431
432    /**
433     * Set Soft Edges Size.
434     *
435     * @param float $size
436     */
437    public function setSoftEdgesSize($size): void
438    {
439        if ($size !== null) {
440            $this->activateObject();
441            $this->softEdges['size'] = (string) $this->getExcelPointsWidth($size);
442        }
443    }
444
445    /**
446     * Get Soft Edges Size.
447     *
448     * @return string
449     */
450    public function getSoftEdgesSize()
451    {
452        return $this->softEdges['size'];
453    }
454}
455