1
|
|
|
<?php namespace Arcanedev\LaravelMetrics\Results; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class RangedValueResult |
5
|
|
|
* |
6
|
|
|
* @package Arcanedev\LaravelMetrics\Results |
7
|
|
|
* @author ARCANEDEV <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class RangedValueResult extends ValueResult |
10
|
|
|
{ |
11
|
|
|
/* ----------------------------------------------------------------- |
12
|
|
|
| Properties |
13
|
|
|
| ----------------------------------------------------------------- |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The previous property. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public $previous = [ |
22
|
|
|
'value' => null, |
23
|
|
|
'label' => null, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The change property. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $change = [ |
32
|
|
|
'value' => null, |
33
|
|
|
'label' => null, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
37
|
|
|
| Getters & Setters |
38
|
|
|
| ----------------------------------------------------------------- |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the value. |
43
|
|
|
* |
44
|
|
|
* @param mixed $value |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
52 |
|
public function value($value) |
49
|
|
|
{ |
50
|
52 |
|
parent::value($value); |
51
|
|
|
|
52
|
52 |
|
return $this->updateChange(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the previous property. |
57
|
|
|
* |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @param string|null $label |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
32 |
|
public function previous($value, $label = null) |
64
|
|
|
{ |
65
|
32 |
|
$this->previous = compact('value', 'label'); |
66
|
|
|
|
67
|
32 |
|
return $this->updateChange(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the change property. |
72
|
|
|
* |
73
|
|
|
* @param mixed $value |
74
|
|
|
* @param string|null $label |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
52 |
|
public function change($value, $label = null) |
79
|
|
|
{ |
80
|
52 |
|
$this->change = compact('value', 'label'); |
81
|
|
|
|
82
|
52 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* ----------------------------------------------------------------- |
86
|
|
|
| Other Methods |
87
|
|
|
| ----------------------------------------------------------------- |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Update the change property. |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
52 |
|
protected function updateChange() |
96
|
|
|
{ |
97
|
52 |
|
$current = $this->value; |
98
|
52 |
|
$previous = $this->previous['value']; |
99
|
52 |
|
$change = null; |
100
|
52 |
|
$label = __('No Prior Data'); |
101
|
|
|
|
102
|
52 |
|
if ($previous && $previous > 0) { |
103
|
32 |
|
$change = static::calculateChange($current, $previous); |
104
|
32 |
|
$label = $change === 0 |
105
|
4 |
|
? 'Constant' |
106
|
32 |
|
: (abs($change) . '% ' . __($change > 0 ? 'Increase' : 'Decrease')); |
107
|
|
|
} |
108
|
|
|
|
109
|
52 |
|
return $this->change($change, $label); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the instance as an array. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
16 |
|
public function toArray(): array |
118
|
|
|
{ |
119
|
16 |
|
return array_merge(parent::toArray(), [ |
120
|
16 |
|
'previous' => $this->previous, |
121
|
16 |
|
'change' => $this->change, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Calculate the change. |
127
|
|
|
* |
128
|
|
|
* @param float|int $current |
129
|
|
|
* @param float|int $previous |
130
|
|
|
* |
131
|
|
|
* @return float|int |
132
|
|
|
*/ |
133
|
32 |
|
protected static function calculateChange($current, $previous) |
134
|
|
|
{ |
135
|
32 |
|
$diff = $current - $previous; |
136
|
|
|
|
137
|
32 |
|
if (is_null($current) || $current === 0) |
138
|
4 |
|
$current = $previous; |
139
|
|
|
|
140
|
32 |
|
return $diff !== 0 |
141
|
32 |
|
? round(($diff / $current) * 100, 2) |
142
|
32 |
|
: 0; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|