BasePartitionAlgorithm::setDataPartition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
  /**
14
   * The original data.
15
   *
16
   * @var array
17
   */
18
    protected $data;
19
20
  /**
21
   * A single partition containing the original data in the right format.
22
   *
23
   * @var Partition
24
   */
25
    protected $dataPartition;
26
27
  /**
28
   * The partition container.
29
   *
30
   * @var PartitionContainer
31
   */
32
    protected $partitionContainer;
33
34
  /**
35
   * The function used to compute the value of an item.
36
   *
37
   * @var callable
38
   */
39
    protected $itemAccessCallback;
40
41
  /**
42
   * BasePartitionAlgorithm constructor.
43
   */
44 14
    public function __construct()
45
    {
46 14
        $this->partitionContainer = new PartitionContainer();
47 14
        $this->partitionContainer->setAlgo($this);
48 14
    }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53 14
    public function setData(array $data = array())
54
    {
55 14
        $this->data = $data;
56 14
        $this->setDataPartition($this->generateDataPartition($this->getData()));
57 14
    }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 14
    public function getData()
63
    {
64 14
        return $this->data;
65
    }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70 14
    public function setDataPartition(Partition $partition)
71
    {
72 14
        $this->dataPartition = $partition;
73 14
    }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78 14
    public function getDataPartition()
79
    {
80 14
        return $this->dataPartition;
81
    }
82
83
  /**
84
   * {@inheritdoc}
85
   */
86 14
    public function generateDataPartition(array $items = array())
87
    {
88 14
        return $this->getPartition()->addItems(
89 14
            array_map(function ($item) {
90 14
                if ($item instanceof PartitionItemInterface) {
91 4
                    return $item;
92
                }
93 14
                return new PartitionItem(
94 14
                    $item,
95 14
                    $this->getItemAccessCallback()
96 14
                );
97 14
            }, $items)
98 14
        );
99
    }
100
101
  /**
102
   * {@inheritdoc}
103
   */
104 14
    public function setSize($size)
105
    {
106 14
        $this->getPartitionContainer()->setSize($size);
107 14
    }
108
109
  /**
110
   * {@inheritdoc}
111
   */
112 14
    public function getSize()
113
    {
114 14
        return $this->getPartitionContainer()->getSize();
115
    }
116
117
  /**
118
   * {@inheritdoc}
119
   */
120 4
    public function setItemAccessCallback(callable $callable = null)
121
    {
122 4
        $this->itemAccessCallback = $callable;
123 4
        $this->setData($this->getData());
124 4
    }
125
126
  /**
127
   * {@inheritdoc}
128
   */
129 14
    public function getItemAccessCallback()
130
    {
131 14
        return $this->itemAccessCallback;
132
    }
133
134
  /**
135
   * {@inheritdoc}
136
   */
137 14
    public function getPartitionContainer()
138
    {
139 14
        return $this->partitionContainer;
140
    }
141
142
  /**
143
   * {@inheritdoc}
144
   */
145 6
    public function getResult()
146
    {
147 6
        $this->getPartitionContainer()->addItemsToPartition($this->getDataPartition()->toArray());
148
149 6
        return $this->getPartitionContainer()->getPartitionsItemsArray();
150
    }
151
152
  /**
153
   * {@inheritdoc}
154
   */
155 11
    public function getPartitionWeight(Partition $partition)
156
    {
157 11
        return $partition->size();
158
    }
159
160
  /**
161
   * {@inheritdoc}
162
   */
163 14
    public function getPartition()
164
    {
165 14
        $partition = new Partition();
166 14
        $partition->setAlgo($this);
167
168 14
        return $partition;
169
    }
170
}
171