1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PierceMcGeough\phpquartiles; |
4
|
|
|
|
5
|
|
|
class Quartile |
6
|
|
|
{ |
7
|
|
|
private $scores; |
8
|
|
|
|
9
|
|
|
private $quartiles; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create a new Quartiles Instance |
13
|
|
|
* @param array $scores |
14
|
|
|
*/ |
15
|
|
|
public function __construct(array $scores) |
16
|
|
|
{ |
17
|
|
|
$this->scores = $scores; |
18
|
|
|
|
19
|
|
|
if (!$this->arrayOnlyContainsNumbers()) { |
20
|
|
|
throw new \Exception('Scores can only contain numbers'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->calculateQuartiles(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Calculate the quartiles |
28
|
|
|
*/ |
29
|
|
|
private function calculateQuartiles() |
30
|
|
|
{ |
31
|
|
|
if (count($this->scores)+1 <= 3) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
sort($this->scores, SORT_NUMERIC); |
36
|
|
|
|
37
|
|
|
$this->quartiles = [ |
38
|
|
|
'first' => $this->getQuartile(0.25), |
39
|
|
|
'second' => $this->getQuartile(0.50), |
40
|
|
|
'third' => $this->getQuartile(0.75) |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the quartiles |
46
|
|
|
* |
47
|
|
|
* @return array|void |
48
|
|
|
*/ |
49
|
|
|
public function getAllQuartiles() |
50
|
|
|
{ |
51
|
|
|
return $this->quartiles; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return numeric |
56
|
|
|
*/ |
57
|
|
|
public function getFirstQuartile() |
58
|
|
|
{ |
59
|
|
|
return $this->getQuartile(0.25); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return numeric |
64
|
|
|
*/ |
65
|
|
|
public function getSecondQuartile() |
66
|
|
|
{ |
67
|
|
|
return $this->getQuartile(0.50); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return numeric |
72
|
|
|
*/ |
73
|
|
|
public function getMedianQuartile() |
74
|
|
|
{ |
75
|
|
|
return $this->getQuartile(0.50); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return numeric |
80
|
|
|
*/ |
81
|
|
|
public function getThirdQuartile() |
82
|
|
|
{ |
83
|
|
|
return $this->getQuartile(0.75); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Use the params to work out the quartile specific to this $array |
88
|
|
|
* |
89
|
|
|
* @param double $quartilePlace |
90
|
|
|
* @return float |
91
|
|
|
*/ |
92
|
|
|
public function getQuartile($quartilePlace) |
93
|
|
|
{ |
94
|
|
|
$pos = (count($this->scores) + 1) * $quartilePlace; |
95
|
|
|
|
96
|
|
|
if (fmod($pos, 1) == 0) { |
97
|
|
|
return $this->scores[$pos-1]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$fraction = $pos - floor($pos); |
101
|
|
|
|
102
|
|
|
$lower_num = $this->scores[floor($pos) - 1]; |
103
|
|
|
$upper_num = $this->scores[ceil($pos) - 1]; |
104
|
|
|
|
105
|
|
|
$difference = $upper_num - $lower_num; |
106
|
|
|
|
107
|
|
|
return round($lower_num + ($difference * $fraction), 2); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate that the scores array only contains numbers |
112
|
|
|
* |
113
|
|
|
* @return boolean |
114
|
|
|
*/ |
115
|
|
|
private function arrayOnlyContainsNumbers() |
116
|
|
|
{ |
117
|
|
|
foreach ($this->scores as $score) { |
118
|
|
|
if (!is_numeric($score)) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|