-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperNumber.php
140 lines (127 loc) · 3.06 KB
/
SuperNumber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace DeGecko;
use DeGecko\Traits\Castings;
use DeGecko\Traits\Checks;
use DeGecko\Traits\Formulas;
use DeGecko\Traits\Mutators;
use DeGecko\Traits\Output;
use DeGecko\Traits\SimpleMath;
/**
* SuperNumber is a number objectifier.
* Its purpose is to simply working with numbers.
*
* @method abs
* @method acos
* @method acosh
* @method asin
* @method asinh
* @method atan
* @method atanh
* @method ceil
* @method cos
* @method cosh
* @method deg2rad
* @method exp
* @method expm1
* @method floor
* @method fmod
* @method log10
* @method log1p
* @method log
* @method min
* @method max
* @method rad2deg
* @method round
* @method sin
* @method sinh
* @method sqrt
* @method tan
* @method tanh
* @method base_convert
* @method bindec
* @method decbin
* @method dechex
* @method decoct
* @method hexdec
* @method octdec
* @method pow
* @package SuperNumber
*/
class SuperNumber
{
use SimpleMath, Formulas, Mutators, Output, Castings, Checks;
/**
* The current value of the instance.
*
* @var $number
*/
protected $number;
/**
* Initiates the class if the given $number is a valid numeric value.
* Otherwise, it throws and explicit error.
*
* @param $number
*/
public function __construct($number)
{
$this->number = $number;
$this->validate();
}
/**
* Handles calls to methods matching PHP basic math functions.
*
* @param $name
* @param array $arguments
* @return $this
*/
public function __call($name, $arguments = [])
{
$phpMathFunctions = [
'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'ceil', 'cos', 'cosh', 'deg2rad', 'exp', 'expm1',
'floor', 'fmod', 'log10', 'log1p', 'log', 'min', 'max', 'rad2deg', 'round', 'sin', 'sinh', 'sqrt', 'tan',
'tanh', 'base_convert', 'bindec', 'decbin', 'dechex', 'decoct', 'hexdec', 'octdec', 'pow',
];
// Handle the math functions.
if (in_array($name, $phpMathFunctions)) {
array_unshift($arguments, $this->number);
$this->number = call_user_func($name, ...$arguments);
}
return $this;
}
/**
* Checks the given $number to be numeric.
* Stores the number so it can be mutated.
*/
private function validate()
{
if (! is_numeric($this->number)) {
throw new \InvalidArgumentException("Invalid numeric value provided ($this->number).");
}
}
/**
* Transforms the value.
* If $percent is true, it will return that percentage of the value.
*
* @param $value
* @param bool $percentage
* @return float|int
*/
private function handleValue($value, $percentage = false)
{
if ($percentage) {
return $value / 100 * $this->number;
}
return $value;
}
/**
* Set a new value.
*
* @param $number
* @return mixed
*/
public function set($number)
{
$this->number = $number;
return $this;
}
}