PartitionResult::sort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Results;
6
7
use Closure;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class     PartitionResult
12
 *
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class PartitionResult extends Result
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * The custom label names.
24
     *
25
     * @var array
26
     */
27
    public $labels = [];
28
29
    /**
30
     * The custom label colors.
31
     *
32
     * @var array
33
     */
34
    public $colors = [];
35
36
    /**
37
     * The sort direction [asc, desc].
38
     *
39
     * @var string|null
40
     */
41
    private $sort;
42
43
    /* -----------------------------------------------------------------
44
     |  Setters
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Set the value.
50
     *
51
     * @param  mixed  $value
52
     *
53
     * @return $this|mixed
54
     */
55 60
    public function value($value)
56
    {
57 60
        return parent::value(
58 60
            Collection::make($value)
59
        );
60
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Getters & Setters
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Format the labels for the partition result.
69
     *
70
     * @param  \Closure  $callback
71
     *
72
     * @return $this
73
     */
74 4
    public function label(Closure $callback)
75
    {
76 4
        return $this->labels(
77 4
            $this->mapValue($callback)->toArray()
78
        );
79
    }
80
81
    /**
82
     * Set the labels for the partition result.
83
     *
84
     * @param  array  $labels
85
     *
86
     * @return $this
87
     */
88 8
    public function labels(array $labels)
89
    {
90 8
        $this->labels = $labels;
91
92 8
        return $this;
93
    }
94
95
    /**
96
     * Set the colors for the partition result.
97
     *
98
     * @param  \Closure  $callback
99
     *
100
     * @return $this
101
     */
102 4
    public function color(Closure $callback)
103
    {
104 4
        return $this->colors(
105 4
            $this->mapValue($callback)->toArray()
106
        );
107
    }
108
109
    /**
110
     * Set the custom label colors.
111
     *
112
     * @param  array  $colors
113
     *
114
     * @return $this
115
     */
116 8
    public function colors(array $colors)
117
    {
118 8
        $this->colors = $colors;
119
120 8
        return $this;
121
    }
122
123
    /**
124
     * Set the sort direction.
125
     *
126
     * @param  string  $direction
127
     *
128
     * @return $this
129
     */
130 4
    public function sort(string $direction = 'asc')
131
    {
132 4
        $direction = strtolower($direction);
133
134 4
        if (in_array($direction, ['asc', 'desc']))
135 4
            $this->sort = $direction;
136
137 4
        return $this;
138
    }
139
140
    /* -----------------------------------------------------------------
141
     |  Common Methods
142
     | -----------------------------------------------------------------
143
     */
144
145
    /**
146
     * Convert the metric object to array.
147
     *
148
     * @return array
149
     */
150 40
    public function toArray(): array
151
    {
152 40
        $value = $this->mapValue(function ($key, $value) {
153 40
            return array_filter($this->formatValue($key, $value), function ($value) {
154 40
                return ! is_null($value);
155 40
            });
156 40
        })->unless(is_null($this->sort), function (Collection $values) {
157 4
            return $values->sortBy('value', SORT_REGULAR, $this->sort === 'desc');
158 40
        })->values()->all();
159
160 40
        return array_merge(parent::toArray(), compact('value'));
161
    }
162
163
    /* -----------------------------------------------------------------
164
     |  Other Methods
165
     | -----------------------------------------------------------------
166
     */
167
168
    /**
169
     * Transform the value by the given callback
170
     *
171
     * @param  \Closure  $callback
172
     *
173
     * @return \Illuminate\Support\Collection
174
     */
175 40
    private function mapValue(Closure $callback)
176
    {
177 40
        return $this->value->map(function ($value, $key) use ($callback) {
178 40
            return $callback($key, $value);
179 40
        });
180
    }
181
182
    /**
183
     * Format the value.
184
     *
185
     * @param  mixed  $key
186
     * @param  mixed  $value
187
     *
188
     * @return array
189
     */
190 40
    protected function formatValue($key, $value): array
191
    {
192
        return [
193 40
            'color' => $this->colors[$key] ?? null,
194 40
            'label' => $this->labels[$key] ?? $key,
195 40
            'value' => $value,
196
        ];
197
    }
198
}
199