Subset::deleteItems()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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