Completed
Push — master ( b0612a...e62e65 )
by Pol
06:37
created

PartitionContainer::addItemsToPartition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
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
/**
6
 * Class PartitionContainer.
7
 *
8
 * @package drupol\phpartition
9
 */
10
class PartitionContainer extends \SplHeap {
11
12
  /**
13
   * @var BasePartitionAlgorithm
14
   */
15
  protected $algo;
16
17
  /**
18
   * @var int
19
   */
20
  protected $size;
21
22
  /**
23
   * Override compare method.
24
   *
25
   * @param Partition $a
26
   * @param Partition $b
27
   *
28
   * @return int
29
   */
30 10
  public function compare($a, $b) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31 10
    $al = $a->getAlgo()->getPartitionWeight($a);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $al. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
32 10
    $bl = $b->getAlgo()->getPartitionWeight($b);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bl. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
33
34 10
    if ($al == $bl) {
35 10
      return 0;
36
    }
37 10
    return ($al > $bl) ? -1 : +1;
38
  }
39
40
  /**
41
   * @param $size
42
   */
43 10
  public function setSize($size) {
44 10
    $this->size = $size;
45
46 10
    for ($i = 0; $i < $size; $i++) {
47 10
      $subset = new Partition();
48 10
      $subset->setAlgo($this->getAlgo());
49 10
      $this->insert($subset);
50
    }
51 10
  }
52
53
  /**
54
   * @return int
55
   */
56 10
  public function getSize() {
57 10
    return $this->size;
58
  }
59
60
  /**
61
   * @param PartitionItem[] $items
62
   */
63 4
  public function addItemsToPartition(array $items = array()) {
64 4
    foreach ($items as $item) {
65 4
      $this->addItemToPartition($item);
66
    }
67 4
  }
68
69
  /**
70
   * @param \drupol\phpartition\PartitionItem $item
71
   */
72 4
  public function addItemToPartition(PartitionItem $item) {
73 4
    $this->top();
74 4
    $subset = $this->extract();
75 4
    $subset->addItem($item);
76 4
    $this->insert($subset);
77 4
  }
78
79
  /**
80
   * @return Partition[]
81
   */
82 10
  public function getPartitions() {
83 10
    $data = array();
84 10
    $clone = clone $this;
85
86 10
    for ($clone->top(); $clone->valid(); $clone->next()) {
87 10
      $data[] = $clone->current();
88
    }
89
90 10
    return $data;
91
  }
92
93
  /**
94
   * @return array
95
   */
96 10
  public function getPartitionsItemsArray() {
97 10
    $data = array();
98
99 10
    foreach ($this->getPartitions() as $subset) {
100 10
      $data[] = $subset->getRawItems();
101
    }
102
103 10
    return array_values(array_filter($data));
104
  }
105
106
  /**
107
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
108
   */
109 10
  public function setAlgo(BasePartitionAlgorithm $algo) {
110 10
    $this->algo = $algo;
111 10
  }
112
113
  /**
114
   * @return BasePartitionAlgorithm
115
   */
116 10
  public function getAlgo() {
117 10
    return $this->algo;
118
  }
119
120
}
121