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 |
|
$partition = new Partition(); |
82
|
|
|
|
83
|
14 |
|
$partition->addItems( |
84
|
14 |
|
array_map(function ($item) { |
85
|
14 |
|
if ($item instanceof PartitionItemInterface) { |
86
|
4 |
|
return $item; |
87
|
|
|
} |
88
|
14 |
|
return new PartitionItem( |
89
|
|
|
$item, |
90
|
14 |
|
$this->getItemAccessCallback() |
91
|
|
|
); |
92
|
14 |
|
}, $items) |
93
|
|
|
); |
94
|
|
|
|
95
|
14 |
|
return $partition; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
14 |
|
public function setSize($size) { |
102
|
14 |
|
$this->getPartitionContainer()->setSize($size); |
103
|
14 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
14 |
|
public function getSize() { |
109
|
14 |
|
return $this->getPartitionContainer()->getSize(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
4 |
|
public function setItemAccessCallback(callable $callable = NULL) { |
|
|
|
|
116
|
4 |
|
$this->itemAccessCallback = $callable; |
117
|
4 |
|
$this->setData($this->getData()); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
14 |
|
public function getItemAccessCallback() { |
124
|
14 |
|
return $this->itemAccessCallback; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
14 |
|
public function getPartitionContainer() { |
131
|
14 |
|
return $this->partitionContainer; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
6 |
|
public function getResult() { |
138
|
6 |
|
$this->getPartitionContainer()->addItemsToPartition($this->getDataPartition()->all()); |
139
|
|
|
|
140
|
6 |
|
return $this->getPartitionContainer()->getPartitionsItemsArray(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
11 |
|
public function getPartitionWeight(Partition $partition) { |
147
|
11 |
|
return $partition->count(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
8 |
|
public function getPartition() { |
154
|
8 |
|
$partition = new Partition(); |
155
|
8 |
|
$partition->setAlgo($this); |
156
|
|
|
|
157
|
8 |
|
return $partition; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|