Completed
Push — master ( 137b9b...8d97a2 )
by Pol
03:29
created

src/SubsetContainer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace drupol\phpartition;
4
5
class SubsetContainer extends \SplHeap {
6
7
  /**
8
   * @var BasePartitionAlgorithm
9
   */
10
  protected $algo;
11
12
  /**
13
   * Override compare method.
14
   *
15
   * @param Subset $a
16
   * @param Subset $b
17
   *
18
   * @return int
19
   */
20 6
  public function compare($a, $b) {
21 6
    $al = $a->getAlgo()->getSubsetWeight($a);
22 6
    $bl = $b->getAlgo()->getSubsetWeight($b);
23
24 6
    if ($al == $bl) {
25 6
      return 0;
26
    }
27 6
    return ($al > $bl) ? -1 : +1;
28
  }
29
30
  /**
31
   * @param $partition
32
   */
33 6
  public function setPartition($partition) {
34 6
    for($i=0; $i<$partition; $i++) {
35 6
      $subset = new Subset();
36 6
      $subset->setAlgo($this->getAlgo());
37 6
      $this->insert($subset);
38 6
    }
39 6
  }
40
41
  /**
42
   * @param SubsetItem[] $items
43
   */
44
  public function addItemsToSubset(array $items = array()) {
45
    foreach($items as $item) {
0 ignored issues
show
Expected 1 space after FOREACH keyword; 0 found
Loading history...
46
      $this->addItemToSubset($item);
47
    }
48
  }
49
50
  /**
51
   * @param \drupol\phpartition\SubsetItem $item
52
   */
53 4
  public function addItemToSubset(SubsetItem $item) {
54 4
    $this->top();
55 4
    $subset = $this->extract();
56 4
    $subset->addItem($item);
57 4
    $this->insert($subset);
58 4
  }
59
60
  /**
61
   * @return Subset[]
62
   */
63 6
  public function getSubsets() {
64 6
    $data = array();
65 6
    $clone = clone $this;
66
67 6
    for($clone->top(); $clone->valid(); $clone->next()) {
68 6
      $data[] = $clone->current();
69 6
    }
70
71 6
    return $data;
72
  }
73
74
  /**
75
   * @return array
76
   */
77 6
  public function getSubsetsAndItemsAsArray() {
78 6
    $data = array();
79
80 6
    foreach($this->getSubsets() as $subset) {
81 6
      $data[] = $subset->getRawItems();
82 6
      }
83
84 6
    return array_values(array_filter($data));
85
  }
86
87
  /**
88
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
89
   */
90 6
  public function setAlgo(BasePartitionAlgorithm $algo) {
91 6
    $this->algo = $algo;
92 6
  }
93
94
  /**
95
   * @return BasePartitionAlgorithm
96
   */
97 6
  public function getAlgo() {
98 6
    return $this->algo;
99
  }
100
101
}
102