|
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
|
|
|
if (!$this->arrayOnlyContainsNumbers()) { |
|
18
|
|
|
throw new \Exceptioon('Scores can only contain numbers'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$this->scores = $scores; |
|
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
|
|
|
'lowest' => $this->getQuartile(0.25), |
|
39
|
|
|
'third' => $this->getQuartile(0.50), |
|
40
|
|
|
'second' => $this->getQuartile(0.75) |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the quartiles |
|
46
|
|
|
* |
|
47
|
|
|
* @return array|void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getQuartiles() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->quartiles; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Use the params to work out the quartile specific to this $array |
|
56
|
|
|
* |
|
57
|
|
|
* @param double $quartilePlace |
|
58
|
|
|
* @return float |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getQuartile($quartilePlace) |
|
61
|
|
|
{ |
|
62
|
|
|
$pos = (count($this->scores) + 1) * $quartilePlace; |
|
63
|
|
|
|
|
64
|
|
|
if (fmod($pos, 1) == 0) { |
|
65
|
|
|
return $this->scores[$pos-1]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$fraction = $pos - floor($pos); |
|
69
|
|
|
|
|
70
|
|
|
$lower_num = $this->scores[floor($pos) - 1]; |
|
71
|
|
|
$upper_num = $this->scores[ceil($pos) - 1]; |
|
72
|
|
|
|
|
73
|
|
|
$difference = $upper_num - $lower_num; |
|
74
|
|
|
|
|
75
|
|
|
return round($lower_num + ($difference * $fraction), 2); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Validate that the scores array only contains numbers |
|
80
|
|
|
* |
|
81
|
|
|
* @return boolean |
|
82
|
|
|
*/ |
|
83
|
|
|
private function arrayOnlyContainsNumbers() |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($this->scores as $score) { |
|
86
|
|
|
if (!is_numeric($score)) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|