|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\phpartition; |
|
4
|
|
|
|
|
5
|
|
|
class Dataset implements \Countable { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @var SubsetItem[] |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $items; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return int |
|
14
|
|
|
*/ |
|
15
|
2 |
|
public function count() { |
|
16
|
2 |
|
return count($this->getItems()); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return SubsetItem[] |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function getItems() { |
|
23
|
6 |
|
return $this->items; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param SubsetItem[] $items |
|
28
|
|
|
* |
|
29
|
|
|
* @return \drupol\phpartition\SubsetItem[] |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function setItems(array $items = array()) { |
|
32
|
6 |
|
$this->items = $items; |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \drupol\phpartition\SubsetItem $item |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function add(SubsetItem $item) { |
|
39
|
6 |
|
$this->items[$item->getKey()] = $item; |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function getSum() { |
|
46
|
2 |
|
$sum = 0; |
|
47
|
|
|
|
|
48
|
2 |
|
foreach($this->getItems() as $item) { |
|
|
|
|
|
|
49
|
2 |
|
$sum += $item->getValue(); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
return $sum; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getItemsValues() { |
|
59
|
|
|
$data = array(); |
|
60
|
|
|
|
|
61
|
|
|
foreach($this->getItems() as $item) { |
|
|
|
|
|
|
62
|
|
|
$data[$item->getKey()] = $item->getValue(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param SubsetItem[] $items |
|
70
|
|
|
*/ |
|
71
|
|
|
public function substract(array $items = array()) { |
|
72
|
|
|
$this->setItems(array_diff($this->getItems(), $items)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function clear() { |
|
79
|
6 |
|
$this->setItems(); |
|
80
|
6 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $key |
|
84
|
|
|
* |
|
85
|
|
|
* @return \drupol\phpartition\SubsetItem |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getItem($key) { |
|
88
|
|
|
return $this->items[$key]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param SubsetItem[] $items |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function deleteItems(array $items = array()) { |
|
95
|
2 |
|
foreach($items as $item) { |
|
|
|
|
|
|
96
|
2 |
|
$this->delete($item); |
|
97
|
2 |
|
} |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param \drupol\phpartition\SubsetItem $item |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function delete(SubsetItem $item) { |
|
104
|
2 |
|
foreach($this->items as $key => $value) { |
|
|
|
|
|
|
105
|
2 |
|
if ($value->getKey() == $item->getKey()) { |
|
106
|
2 |
|
unset($this->items[$value->getKey()]); |
|
107
|
2 |
|
} |
|
108
|
2 |
|
} |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $order |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
4 |
|
public function sortByValue($order = 'ASC') { |
|
117
|
4 |
|
$data = $this->getItems(); |
|
118
|
|
|
|
|
119
|
4 |
|
if ('ASC' == $order) { |
|
120
|
|
|
usort($data, function($a, $b) { |
|
|
|
|
|
|
121
|
2 |
|
return $a->getValue() > $b->getValue(); |
|
122
|
2 |
|
}); |
|
123
|
2 |
|
} |
|
124
|
|
|
|
|
125
|
4 |
|
if ('DESC' == $order) { |
|
126
|
2 |
|
usort($data, function($a, $b) { |
|
|
|
|
|
|
127
|
2 |
|
return $a->getValue() < $b->getValue(); |
|
128
|
2 |
|
}); |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
4 |
|
$this->setItems($data); |
|
132
|
|
|
|
|
133
|
4 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.