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
|
28 |
|
public function value($value) |
53
|
|
|
{ |
54
|
28 |
|
return parent::value( |
55
|
28 |
|
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
|
4 |
|
public function labels(array $labels) |
86
|
|
|
{ |
87
|
4 |
|
$this->labels = $labels; |
88
|
|
|
|
89
|
4 |
|
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
|
4 |
|
public function colors(array $colors) |
114
|
|
|
{ |
115
|
4 |
|
$this->colors = $colors; |
116
|
|
|
|
117
|
4 |
|
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
|
28 |
|
public function toArray(): array |
146
|
|
|
{ |
147
|
|
|
$value = $this->mapValue(function ($key, $value) { |
148
|
28 |
|
return array_filter([ |
149
|
28 |
|
'color' => $this->colors[$key] ?? null, |
150
|
28 |
|
'label' => $this->labels[$key] ?? $key, |
151
|
28 |
|
'value' => $value, |
152
|
|
|
], function ($value) { |
153
|
28 |
|
return ! is_null($value); |
154
|
28 |
|
}); |
155
|
|
|
})->unless(is_null($this->sort), function (Collection $values) { |
156
|
4 |
|
return $values->sortBy('value', SORT_REGULAR, $this->sort === 'desc'); |
157
|
28 |
|
})->values()->all(); |
158
|
|
|
|
159
|
28 |
|
return compact('value'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/* ----------------------------------------------------------------- |
163
|
|
|
| Other Methods |
164
|
|
|
| ----------------------------------------------------------------- |
165
|
|
|
*/ |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Transform the value by the given callback |
169
|
|
|
* |
170
|
|
|
* @param \Closure $callback |
171
|
|
|
* |
172
|
|
|
* @return \Illuminate\Support\Collection |
173
|
|
|
*/ |
174
|
28 |
|
private function mapValue(Closure $callback) |
175
|
|
|
{ |
176
|
|
|
return $this->value->map(function ($value, $key) use ($callback) { |
177
|
28 |
|
return $callback($key, $value); |
178
|
28 |
|
}); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|