1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the Thelia package. */ |
4
|
|
|
/* */ |
5
|
|
|
/* Copyright (c) OpenStudio */ |
6
|
|
|
/* email : [email protected] */ |
7
|
|
|
/* web : http://www.thelia.net */ |
|
|
|
|
8
|
|
|
/* */ |
9
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
10
|
|
|
/* file that was distributed with this source code. */ |
11
|
|
|
/*************************************************************************************/ |
12
|
|
|
|
13
|
|
|
namespace Thelia\Math; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Number |
17
|
|
|
* @package Lovenunu\Math |
18
|
|
|
* @author Benjamin Perche <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Number implements \Serializable, \JsonSerializable |
21
|
|
|
{ |
22
|
|
|
// Regex to find correct numbers |
23
|
|
|
const NUMBER_PATTERN = "#^\d+(\.\d+)?$#"; |
24
|
|
|
|
25
|
|
|
// Internal variables |
26
|
|
|
protected $dividend; |
27
|
|
|
|
28
|
|
|
protected $divisor; |
29
|
|
|
|
30
|
|
|
// --- Object construction ------------ |
31
|
|
|
public function __construct($number = null) |
32
|
|
|
{ |
33
|
|
|
list($this->dividend, $this->divisor) = $this->retrieve($number); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function initialize($dividend, $divisor) |
37
|
|
|
{ |
38
|
|
|
$this->dividend = $dividend; |
39
|
|
|
|
40
|
|
|
$this->divisor = $divisor; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// --- Number processor ------------ |
44
|
|
|
|
45
|
|
|
protected function retrieve($number) |
46
|
|
|
{ |
47
|
|
|
$dividend = 0; |
48
|
|
|
$divisor = 1; |
49
|
|
|
|
50
|
|
|
if ($number !== null) { |
51
|
|
|
if (is_scalar($number) && preg_match(static::NUMBER_PATTERN, $number)) { |
52
|
|
|
$dividend = (int) str_replace(".", "", $number); |
53
|
|
|
|
54
|
|
|
if (false !== $pos = strpos($number, ".")) { |
55
|
|
|
$divisor = pow(10, strlen($number) - $pos - 1); |
56
|
|
|
} |
57
|
|
|
} elseif ($number instanceof $this) { |
58
|
|
|
$dividend = $number->getDividend(); |
59
|
|
|
$divisor = $number->getDivisor(); |
60
|
|
|
} else { |
61
|
|
|
throw new \InvalidArgumentException( |
62
|
|
|
"The given price is not valid" |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return array($dividend, $divisor); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function computeDividend($askedDividend, $askedDivisor, Number $number) |
71
|
|
|
{ |
72
|
|
|
if ($number->divisor !== $askedDivisor) { |
73
|
|
|
$number->dividend *= $askedDivisor; |
74
|
|
|
$askedDividend *= $number->divisor; |
75
|
|
|
|
76
|
|
|
$number->divisor *= $askedDivisor; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $askedDividend; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function simplify() |
83
|
|
|
{ |
84
|
|
|
if (1 < $gcd = GCD::getGCD($this->dividend, $this->divisor)) { |
85
|
|
|
$this->dividend /= $gcd; |
86
|
|
|
$this->divisor /= $gcd; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// --- Operations ------------ |
91
|
|
|
|
92
|
|
View Code Duplication |
public function add($number) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
list($askedDividend, $askedDivisor) = $this->retrieve($number); |
95
|
|
|
$returnNumber = clone $this; |
96
|
|
|
|
97
|
|
|
$askedDividend = $this->computeDividend($askedDividend, $askedDivisor, $returnNumber); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* If divisors are different, just multiply them |
101
|
|
|
*/ |
102
|
|
|
|
103
|
|
|
$returnNumber->dividend += $askedDividend; |
104
|
|
|
$returnNumber->simplify(); |
105
|
|
|
|
106
|
|
|
return $returnNumber; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function sub($number) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
list($askedDividend, $askedDivisor) = $this->retrieve($number); |
112
|
|
|
$returnNumber = clone $this; |
113
|
|
|
|
114
|
|
|
$askedDividend = $this->computeDividend($askedDividend, $askedDivisor, $returnNumber); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* If divisors are different, just multiply them |
118
|
|
|
*/ |
119
|
|
|
|
120
|
|
|
$returnNumber->dividend -= $askedDividend; |
121
|
|
|
$returnNumber->simplify(); |
122
|
|
|
|
123
|
|
|
return $returnNumber; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function multiply($number) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
list($askedDividend, $askedDivisor) = $this->retrieve($number); |
129
|
|
|
$returnNumber = clone $this; |
130
|
|
|
|
131
|
|
|
$returnNumber->dividend *= $askedDividend; |
132
|
|
|
$returnNumber->divisor *= $askedDivisor; |
133
|
|
|
$returnNumber->simplify(); |
134
|
|
|
|
135
|
|
|
return $returnNumber; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function divide($number) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
list($askedDividend, $askedDivisor) = $this->retrieve($number); |
141
|
|
|
$returnNumber = clone $this; |
142
|
|
|
|
143
|
|
|
$returnNumber->dividend *= $askedDivisor; |
144
|
|
|
$returnNumber->divisor *= $askedDividend; |
145
|
|
|
$returnNumber->simplify(); |
146
|
|
|
|
147
|
|
|
return $returnNumber; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// --- Results ------------ |
151
|
|
|
|
152
|
|
|
public function getNumber($roundPrecision = 2, $roundMode = PHP_ROUND_HALF_UP) |
153
|
|
|
{ |
154
|
|
|
$value = $this->dividend / $this->divisor; |
155
|
|
|
|
156
|
|
|
if ($roundPrecision >= 0) { |
157
|
|
|
$value = round($value, $roundPrecision, $roundMode); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// --- Accessor ------------ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function getDividend() |
169
|
|
|
{ |
170
|
|
|
return $this->dividend; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function getDivisor() |
177
|
|
|
{ |
178
|
|
|
return $this->divisor; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// --- Interfaces implementation ------------ |
182
|
|
|
/** |
183
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
184
|
|
|
* String representation of object |
185
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
186
|
|
|
* @return string the string representation of the object or null |
187
|
|
|
*/ |
188
|
|
|
public function serialize() |
189
|
|
|
{ |
190
|
|
|
return json_encode(array($this->dividend, $this->divisor)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
195
|
|
|
* Constructs the object |
196
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
197
|
|
|
* @param string $serialized <p> |
198
|
|
|
* The string representation of the object. |
199
|
|
|
* </p> |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function unserialize($serialized) |
203
|
|
|
{ |
204
|
|
|
list($this->dividend, $this->divisor) = json_decode($serialized, true); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
209
|
|
|
* Specify data which should be serialized to JSON |
210
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
211
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
212
|
|
|
* which is a value of any type other than a resource. |
213
|
|
|
*/ |
214
|
|
|
function jsonSerialize() |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
return $this->serialize(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.