1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Phi; |
4
|
|
|
|
5
|
|
|
class Fraction |
6
|
|
|
{ |
7
|
|
|
/** @var int $whole */ |
8
|
|
|
private $whole; |
9
|
|
|
|
10
|
|
|
/** @var int $numerator */ |
11
|
|
|
private $numerator; |
12
|
|
|
|
13
|
|
|
/** @var int $denominator */ |
14
|
|
|
private $denominator; |
15
|
|
|
|
16
|
|
|
/** @var bool $negative */ |
17
|
|
|
private $negative; |
18
|
|
|
|
19
|
4 |
|
public function __construct($whole = 0, $numerator = 0, $denominator = 1) |
20
|
|
|
{ |
21
|
4 |
|
$this->whole = $whole; |
22
|
4 |
|
$this->numerator = $numerator; |
23
|
4 |
|
$this->denominator = $denominator; |
24
|
4 |
|
$this->negative = false; |
25
|
4 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
1 |
|
public function getWhole() |
31
|
|
|
{ |
32
|
1 |
|
return $this->whole; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $whole |
37
|
|
|
* @return Fraction |
38
|
|
|
*/ |
39
|
4 |
|
public function setWhole($whole) |
40
|
|
|
{ |
41
|
4 |
|
$this->whole = $whole; |
42
|
4 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
1 |
|
public function getNumerator() |
49
|
|
|
{ |
50
|
1 |
|
return $this->numerator; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $numerator |
55
|
|
|
* @return Fraction |
56
|
|
|
*/ |
57
|
4 |
|
public function setNumerator($numerator) |
58
|
|
|
{ |
59
|
4 |
|
$this->numerator = $numerator; |
60
|
4 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
1 |
|
public function getDenominator() |
67
|
|
|
{ |
68
|
1 |
|
return $this->denominator; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $denominator |
73
|
|
|
* @return Fraction |
74
|
|
|
*/ |
75
|
4 |
|
public function setDenominator($denominator) |
76
|
|
|
{ |
77
|
4 |
|
$this->denominator = $denominator; |
78
|
4 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function refactor() |
82
|
|
|
{ |
83
|
1 |
|
if ($this->numerator >= $this->denominator) { |
84
|
1 |
|
for ($x = $this->numerator; $x >= $this->denominator; $x = $x - $this->denominator) { |
85
|
1 |
|
$this->whole ++; |
86
|
|
|
} |
87
|
1 |
|
$this->numerator = 0; |
88
|
|
|
} |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function isNegative() |
95
|
|
|
{ |
96
|
|
|
return $this->negative; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param bool $negative |
101
|
|
|
* @return Fraction |
102
|
|
|
*/ |
103
|
|
|
public function setNegative($negative) |
104
|
|
|
{ |
105
|
|
|
$this->negative = $negative; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
1 |
|
public function isInteger() |
113
|
|
|
{ |
114
|
1 |
|
return $this->numerator % $this->denominator == 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
1 |
|
public function __toString() |
121
|
|
|
{ |
122
|
1 |
|
$this->refactor(); |
123
|
|
|
|
124
|
|
|
// if the whole is 0, don't display it |
125
|
1 |
|
$whole = $this->whole == 0 ? '' : $this->whole; |
126
|
1 |
|
$fraction = $this->numerator > 0 ? $this->numerator.'/'.$this->denominator : ''; |
127
|
1 |
|
$space = ($whole && $fraction) ? ' ' : ''; |
128
|
|
|
|
129
|
1 |
|
return $whole.$space.$fraction; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return float |
134
|
|
|
*/ |
135
|
1 |
|
public function toDecimal() |
136
|
|
|
{ |
137
|
|
|
/* |
138
|
|
|
* a divide symbol. so this is broken and will need refactoring to be accurate. ;-) |
139
|
|
|
*/ |
140
|
1 |
|
return $this->whole + ($this->numerator / $this->denominator); |
141
|
|
|
} |
142
|
|
|
} |