Completed
Push — master ( a6e84f...ca7d58 )
by Pol
10:47
created

src/BasePartitionAlgorithm.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
/**
6
 * Class BasePartitionAlgorithm.
7
 *
8
 * @package drupol\phpartition
9
 */
10
abstract class BasePartitionAlgorithm implements PartitionAlgorithmInterface {
11
12
  /**
13
   * The original data.
14
   *
15
   * @var array
16
   */
17
  protected $data;
18
19
  /**
20
   * A single partition containing the original data in the right format.
21
   *
22
   * @var Partition
23
   */
24
  protected $dataPartition;
25
26
  /**
27
   * The partition container.
28
   *
29
   * @var PartitionContainer
30
   */
31
  protected $partitionContainer;
32
33
  /**
34
   * The function used to compute the value of an item.
35
   *
36
   * @var callable
37
   */
38
  protected $itemAccessCallback;
39
40
  /**
41
   * BasePartitionAlgorithm constructor.
42
   */
43 14
  public function __construct() {
44 14
    $this->partitionContainer = new PartitionContainer();
45 14
    $this->partitionContainer->setAlgo($this);
46 14
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51 14
  public function setData(array $data = array()) {
52 14
    $this->data = $data;
53 14
    $this->setDataPartition($this->generateDataPartition($this->getData()));
54 14
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59 14
  public function getData() {
60 14
    return $this->data;
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66 14
  public function setDataPartition(Partition $partition) {
67 14
    $this->dataPartition = $partition;
68 14
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73 14
  public function getDataPartition() {
74 14
    return $this->dataPartition;
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80 14
  public function generateDataPartition(array $items = array()) {
81 14
    return $this->getPartition()->addItems(
82 14
      array_map(function ($item) {
83 14
        if ($item instanceof PartitionItemInterface) {
84 4
          return $item;
85
        }
86 14
        return new PartitionItem(
87 14
          $item,
88 14
          $this->getItemAccessCallback()
89 14
        );
90 14
      }, $items)
91 14
    );
92
  }
93
94
  /**
95
   * {@inheritdoc}
96
   */
97 14
  public function setSize($size) {
98 14
    $this->getPartitionContainer()->setSize($size);
99 14
  }
100
101
  /**
102
   * {@inheritdoc}
103
   */
104 14
  public function getSize() {
105 14
    return $this->getPartitionContainer()->getSize();
106
  }
107
108
  /**
109
   * {@inheritdoc}
110
   */
111 4
  public function setItemAccessCallback(callable $callable = NULL) {
0 ignored issues
show
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
112 4
    $this->itemAccessCallback = $callable;
113 4
    $this->setData($this->getData());
114 4
  }
115
116
  /**
117
   * {@inheritdoc}
118
   */
119 14
  public function getItemAccessCallback() {
120 14
    return $this->itemAccessCallback;
121
  }
122
123
  /**
124
   * {@inheritdoc}
125
   */
126 14
  public function getPartitionContainer() {
127 14
    return $this->partitionContainer;
128
  }
129
130
  /**
131
   * {@inheritdoc}
132
   */
133 6
  public function getResult() {
134 6
    $this->getPartitionContainer()->addItemsToPartition($this->getDataPartition()->toArray());
135
136 6
    return $this->getPartitionContainer()->getPartitionsItemsArray();
137
  }
138
139
  /**
140
   * {@inheritdoc}
141
   */
142 11
  public function getPartitionWeight(Partition $partition) {
143 11
    return $partition->size();
144
  }
145
146
  /**
147
   * {@inheritdoc}
148
   */
149 14
  public function getPartition() {
150 14
    $partition = new Partition();
151 14
    $partition->setAlgo($this);
152
153 14
    return $partition;
154
  }
155
156
}
157