|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Character\Properties\Numericity; |
|
4
|
|
|
|
|
5
|
|
|
use UCD\Exception\InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
class RationalNumber |
|
8
|
|
|
{ |
|
9
|
|
|
const REGEX_FRACTION = '#^(?P<sign>[+-]?)(?P<numerator>[0-9]+)(/(?P<denominator>[0-9]+))?$#'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
private $numerator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
private $denominator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $negative; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $numerator |
|
28
|
|
|
* @param int $denominator |
|
29
|
|
|
* @param bool $negative |
|
30
|
|
|
* @throws InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($numerator, $denominator, $negative) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($denominator === 0) { |
|
35
|
|
|
throw new InvalidArgumentException('Zero denominator'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->numerator = $numerator; |
|
39
|
|
|
$this->denominator = $denominator; |
|
40
|
|
|
$this->negative = $negative; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function isFraction() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->denominator !== 1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getNumerator() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->numerator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getDenominator() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->denominator; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return boolean |
|
69
|
|
|
*/ |
|
70
|
|
|
public function isNegative() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->negative; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $value |
|
77
|
|
|
* @return RationalNumber |
|
78
|
|
|
* @throws InvalidArgumentException |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function fromString($value) |
|
81
|
|
|
{ |
|
82
|
|
|
if (preg_match(self::REGEX_FRACTION, $value, $matches) !== 1) { |
|
83
|
|
|
throw new InvalidArgumentException('Invalid number'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$numerator = $matches['numerator']; |
|
87
|
|
|
$denominator = isset($matches['denominator']) ? $matches['denominator'] : 1; |
|
88
|
|
|
$isNegative = isset($matches['sign']) && $matches['sign'] === '-'; |
|
89
|
|
|
|
|
90
|
|
|
return new self($numerator, $denominator, $isNegative); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param self $number |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function toString(self $number) |
|
98
|
|
|
{ |
|
99
|
|
|
$string = $number->isNegative() ? '-' : ''; |
|
100
|
|
|
$string .= $number->getNumerator(); |
|
101
|
|
|
|
|
102
|
|
|
if ($number->isFraction()) { |
|
103
|
|
|
$string .= sprintf('/%d', $number->getDenominator()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $string; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function __toString() |
|
113
|
|
|
{ |
|
114
|
|
|
return self::toString($this); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: