Completed
Push — master ( d4c84e...b0612a )
by Pol
04:30
created

Subset::addItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace drupol\phpartition;
4
5
class Subset implements \Countable {
6
7
  /**
8
   * @var BasePartitionAlgorithm
9
   */
10
  protected $algo;
11
12
  /**
13
   * @var SubsetItem[]
14
   */
15
  protected $items;
16
17
  /**
18
   * @return int
19
   */
20 6
  public function count() {
21 6
    return count($this->getItems());
22
  }
23
24
  /**
25
   * @param \drupol\phpartition\SubsetItem $item
26
   */
27 10
  public function addItem(SubsetItem $item) {
28 10
    $this->items[$item->getKey()] = $item;
29 10
  }
30
31
  /**
32
   * @param SubsetItem[] $items
33
   */
34 2
  public function addItems(array $items = array()) {
35 2
    foreach ($items as $item) {
36 2
      $this->addItem($item);
37 2
    }
38 2
  }
39
40
  /**
41
   * @return SubsetItem[]
42
   */
43 10
  public function getItems() {
44 10
    return (array) $this->items;
45
  }
46
47
  /**
48
   * @param SubsetItem[] $items
49
   */
50 10
  public function setItems(array $items = array()) {
51 10
    $this->items = $items;
52 10
  }
53
54
  /**
55
   * @return array
56
   */
57
  public function getItemsValues() {
58
    $data = array();
59
60
    foreach ($this->getItems() as $item) {
61
      $data[$item->getKey()] = $item->getValue();
62
    }
63
64
    return $data;
65
  }
66
67
  /**
68
   * @return array
69
   */
70 10
  public function getRawItems() {
71 10
    $data = array();
72
73 10
    foreach ($this->getItems() as $item) {
74 10
      $data[] = $item->getItem();
75 10
    }
76
77 10
    return $data;
78
  }
79
80
  /**
81
   * @return int
82
   */
83 6
  public function getWeight() {
84 6
    $sum = 0;
85
86 6
    foreach ((array) $this->items as $item) {
87 6
      $sum += $item->getValue();
88 6
    }
89
90 6
    return $sum;
91
  }
92
93
  /**
94
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
95
   */
96 10
  public function setAlgo(BasePartitionAlgorithm $algo) {
97 10
    $this->algo = $algo;
98 10
  }
99
100
  /**
101
   * @return BasePartitionAlgorithm
102
   */
103 10
  public function getAlgo() {
104 10
    return $this->algo;
105
  }
106
107
  /**
108
   * Clear the subset.
109
   */
110 10
  public function clear() {
111 10
    $this->setItems();
112 10
  }
113
114
  /**
115
   * @param $key
116
   *
117
   * @return \drupol\phpartition\SubsetItem
118
   */
119
  public function getItem($key) {
120
    return $this->items[$key];
121
  }
122
123
  /**
124
   * @param SubsetItem[] $items
125
   */
126 2
  public function deleteItems(array $items = array()) {
127 2
    foreach ($items as $item) {
128 2
      $this->delete($item);
129 2
    }
130 2
  }
131
132
  /**
133
   * @param \drupol\phpartition\SubsetItem $item
134
   */
135 2
  public function delete(SubsetItem $item) {
136 2
    foreach ($this->items as $key => $value) {
137 2
      if ($value->getKey() == $item->getKey()) {
138 2
        unset($this->items[$key]);
139 2
      }
140 2
    }
141 2
  }
142
143
  /**
144
   * @param string $order
145
   *
146
   * @return $this
147
   */
148 8
  public function sortByValue($order = 'ASC') {
149 8
    $data = $this->getItems();
150
151 8
    if ('ASC' == $order) {
152
      usort($data, function ($itemA, $itemB) {
153 6
        return $itemA->getValue() > $itemB->getValue();
154 6
      });
155 6
    }
156
157 8
    if ('DESC' == $order) {
158 2
      usort($data, function ($itemA, $itemB) {
159 2
        return $itemA->getValue() < $itemB->getValue();
160 2
      });
161 2
    }
162
163 8
    $this->setItems($data);
164
165 8
    return $this;
166
  }
167
168
}