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\Subset |
34
|
|
|
*/ |
35
|
|
|
protected $dataset; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* BasePartitionAlgorithm constructor. |
39
|
|
|
*/ |
40
|
10 |
|
public function __construct() { |
41
|
10 |
|
$this->subsets = new SubsetContainer(); |
42
|
10 |
|
$this->subsets->setAlgo($this); |
43
|
10 |
|
$this->dataset = new Subset(); |
44
|
10 |
|
$this->dataset->setAlgo($this); |
45
|
10 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $data |
49
|
|
|
*/ |
50
|
10 |
|
public function setData(array $data = array()) { |
51
|
10 |
|
$this->data = $data; |
52
|
10 |
|
$this->dataset->clear(); |
53
|
10 |
|
$itemAccessCallback = $this->getItemAccessCallback(); |
54
|
|
|
|
55
|
10 |
|
foreach ($data as $key => $item) { |
56
|
10 |
|
$this->dataset->addItem( |
57
|
10 |
|
new SubsetItem( |
58
|
10 |
|
$key, |
59
|
10 |
|
$itemAccessCallback ? call_user_func($itemAccessCallback, $item) : $item, |
60
|
|
|
$item |
61
|
10 |
|
) |
62
|
10 |
|
); |
63
|
10 |
|
} |
64
|
10 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
10 |
|
public function getData() { |
70
|
10 |
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $partition |
75
|
|
|
*/ |
76
|
10 |
|
public function setPartition($partition) { |
77
|
10 |
|
$this->partition = $partition; |
78
|
10 |
|
$this->getSubsetContainer()->setPartition($this->getPartition()); |
79
|
10 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
10 |
|
public function getPartition() { |
85
|
10 |
|
return (int) $this->partition; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param callable $callable |
90
|
|
|
*/ |
91
|
4 |
|
public function setItemAccessCallback($callable) { |
92
|
4 |
|
$this->itemAccessCallback = $callable; |
93
|
4 |
|
$this->setData($this->getData()); |
94
|
4 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return callable |
98
|
|
|
*/ |
99
|
10 |
|
public function getItemAccessCallback() { |
100
|
10 |
|
return $this->itemAccessCallback; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return SubsetContainer |
105
|
|
|
*/ |
106
|
10 |
|
public function getSubsetContainer() { |
107
|
10 |
|
return $this->subsets; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
4 |
|
public function getResult() { |
114
|
4 |
|
foreach ($this->dataset->getItems() as $item) { |
115
|
4 |
|
$this->getSubsetContainer()->addItemToSubset($item); |
116
|
4 |
|
} |
117
|
|
|
|
118
|
4 |
|
return $this->getSubsetContainer()->getSubsetsAndItemsAsArray(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
4 |
|
public function getSubsetWeight(Subset $subset) { |
125
|
4 |
|
return 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|