|
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) { |
|
|
|
|
|
|
114
|
4 |
|
$this->getSubsetContainer()->addItemToSubset($item); |
|
115
|
4 |
|
} |
|
116
|
|
|
|
|
117
|
4 |
|
return $this->getSubsetContainer()->getSubsetsAndItemsAsArray(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|