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