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

BasePartitionAlgorithm::setData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
crap 3
1
<?php
2
3
namespace drupol\phpartition;
4
5
/**
6
 * Class BasePartitionAlgorithm.
7
 *
8
 * @package drupol\phpartition
9
 */
10
abstract class BasePartitionAlgorithm implements PartitionAlgorithmInterface {
11
12
  /**
13
   * @var array
14
   */
15
  protected $data;
16
17
  /**
18
   * @var SubsetContainer
19
   */
20
  protected $subsets;
21
22
  /**
23
   * @var int
24
   */
25
  protected $partition;
26
27
  /**
28
   * @var callable
29
   */
30
  protected $itemAccessCallback;
31
32
  /**
33
   * @var \drupol\phpartition\Dataset
34
   */
35
  protected $dataset;
36
37
  /**
38
   * BasePartitionAlgorithm constructor.
39
   */
40 6
  public function __construct() {
41 6
    $this->subsets = new SubsetContainer();
42 6
    $this->subsets->setAlgo($this);
43 6
    $this->dataset = new Dataset();
44 6
  }
45
46
  /**
47
   * @param array $data
48
   */
49 6
  public function setData(array $data = array()) {
50 6
    $this->data = $data;
51 6
    $this->dataset->clear();
52 6
    $itemAccessCallback = $this->getItemAccessCallback();
53
54 6
    foreach ($data as $key => $item) {
55 6
      $this->dataset->add(
56 6
        new SubsetItem(
57 6
          $key,
58 6
          $itemAccessCallback ? call_user_func($itemAccessCallback, $item) : $item,
59
          $item
60 6
        )
61 6
      );
62 6
    }
63 6
  }
64
65
  /**
66
   * @return array
67
   */
68 6
  public function getData() {
69 6
    return $this->data;
70
  }
71
72
  /**
73
   * @param int $partition
74
   */
75 6
  public function setPartition($partition) {
76 6
    $this->partition = $partition;
77 6
    $this->getSubsetContainer()->setPartition($this->getPartition());
78 6
  }
79
80
  /**
81
   * @return int
82
   */
83 6
  public function getPartition() {
84 6
    return (int) $this->partition;
85
  }
86
87
  /**
88
   * @param callable $callable
89
   */
90 3
  public function setItemAccessCallback($callable) {
91 3
    $this->itemAccessCallback = $callable;
92 3
    $this->setData($this->getData());
93 3
  }
94
95
  /**
96
   * @return callable
97
   */
98 6
  public function getItemAccessCallback() {
99 6
    return $this->itemAccessCallback;
100
  }
101
102
  /**
103
   * @return SubsetContainer
104
   */
105 6
  public function getSubsetContainer() {
106 6
    return $this->subsets;
107
  }
108
109
  /**
110
   * @return array
111
   */
112 4
  public function getResult() {
113 4
    foreach($this->dataset->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
114 4
      $this->getSubsetContainer()->addItemToSubset($item);
115 4
    }
116
117 4
    return $this->getSubsetContainer()->getSubsetsAndItemsAsArray();
118
  }
119
120
}
121