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
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function isNegative() |
85
|
|
|
{ |
86
|
|
|
return $this->negative; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param bool $negative |
91
|
|
|
* @return Fraction |
92
|
|
|
*/ |
93
|
|
|
public function setNegative($negative) |
94
|
|
|
{ |
95
|
|
|
$this->negative = $negative; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
1 |
|
public function isInteger() |
103
|
|
|
{ |
104
|
1 |
|
return $this->numerator % $this->denominator == 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
1 |
|
public function __toString() |
111
|
|
|
{ |
112
|
1 |
|
$whole = $this->whole == 0 ? '' : $this->whole.' '; |
113
|
1 |
|
return $whole.$this->numerator.'/'.$this->denominator; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return float |
118
|
|
|
*/ |
119
|
1 |
|
public function toDecimal() |
120
|
|
|
{ |
121
|
|
|
/* |
122
|
|
|
* a divide symbol. so this is broken and will need refactoring to be accurate. ;-) |
123
|
|
|
*/ |
124
|
1 |
|
return $this->whole + ($this->numerator / $this->denominator); |
125
|
|
|
} |
126
|
|
|
} |