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