Completed
Branch master (cfb1b5)
by Jean
03:31 queued 01:38
created

Trace::toArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 7
Ratio 35 %

Importance

Changes 0
Metric Value
dl 7
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 0
1
<?php
2
/**
3
 * Copyright 2017, Jean Traullé <[email protected]>
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2017, Jean Traullé <[email protected]>
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 *
11
 */
12
13
namespace CakeCharts\Utility;
14
15
/**
16
 * Class Trace
17
 * @package CakeCharts\Utility
18
 */
19
class Trace
20
{
21
    /**
22
     * @var array Values to be placed on X axis
23
     */
24
    private $x;
25
    /**
26
     * @var array Values to be placed on Y axis
27
     */
28
    private $y;
29
    /**
30
     * @var string Type of trace : can be either "bar", "pie" or "scatter"
31
     */
32
    private $type;
33
    /**
34
     * @var string Name of the series
35
     */
36
    private $name;
37
    /**
38
     * @var string Line type ("markers", "lines" or "markers+line")
39
     */
40
    private $mode;
41
42
    /**
43
     * Trace constructor.
44
     *
45
     * @param array $x Values to be placed on X axis
46
     * @param array $y Values to be placed on Y axis
47
     * @param string $type Type of trace : can be either "bar", "pie" or "scatter"
48
     * @param string|null $name Name of the series
49
     * @param string|null $mode Line type ("markers", "lines" or "markers+line")
50
     */
51
    public function __construct(array $x, array $y, string $type, string $name = null, string $mode = null)
52
    {
53
        $this->x = $x;
54
        $this->y = $y;
55
        $this->type = $type;
56
        $this->name = $name;
57
        $this->mode = $mode;
58
    }
59
60
    /**
61
     * Convert Trace object to keys/values array
62
     *
63
     * @return array Plotly.js compliant data series (also known as trace)
64
     * @throws \Exception If unexpected values occured
65
     */
66
    public function toArray()
67
    {
68
        $this->checkXequalsY();
69
        switch ($this->type) {
70 View Code Duplication
            case 'bar':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
                $trace = ['x' => $this->x, 'y' => $this->y, 'type' => $this->type, 'name' => $this->name];
72
                break;
73 View Code Duplication
            case 'scatter':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                $this->checkModeValue();
75
                $trace = ['x' => $this->x, 'y' => $this->y, 'type' => $this->type, 'name' => $this->name, 'mode' => $this->mode];
76
                break;
77
            case 'pie':
78
                $trace = ['labels' => $this->x, 'values' => $this->y, 'type' => $this->type];
79
                break;
80
            default:
81
                throw new \Exception('Unhandled chart type : can be either "bar", "scatter" or "pie" ; got "' . $this->type . '"');
82
        }
83
84
        return $trace;
85
    }
86
87
    /**
88
     * Makes sure that number of X values equals Y values
89
     *
90
     * @throws \Exception
91
     * @return void
92
     */
93
    private function checkXequalsY()
94
    {
95
        if (count($this->x) !== count($this->y)) {
96
            $name = isset($this->name) ? $this->name : 'unamed';
97
            throw new \Exception("Number of X axis values are not equal to number of Y axis values for <u>$name</u> series");
98
        }
99
    }
100
101
    /**
102
     * Makes sure that "mode" is a valid value
103
     *
104
     * @throws \Exception
105
     * @return void
106
     */
107
    private function checkModeValue()
108
    {
109
        if (!in_array($this->mode, ['markers', 'lines', 'lines+markers', null])) {
110
            throw new \Exception('Invalid trace mode : can be either "markers", "lines" or "markers+line" ; got "' . $this->mode . '"');
111
        }
112
    }
113
}
114