1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpartition; |
6
|
|
|
|
7
|
|
|
use drupol\phpartition\Contract\PartitionerInterface; |
8
|
|
|
use drupol\phpartition\Contract\Weightable; |
9
|
|
|
use drupol\phpartition\Partition\Partition; |
10
|
|
|
use drupol\phpartition\Partition\PartitionFactory; |
11
|
|
|
use drupol\phpartition\Partition\PartitionItemFactory; |
12
|
|
|
use drupol\phpartition\Partitions\Partitions; |
13
|
|
|
use drupol\phpartition\Partitions\PartitionsFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Partitioner. |
17
|
|
|
*/ |
18
|
|
|
abstract class Partitioner implements PartitionerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var iterable |
22
|
|
|
*/ |
23
|
|
|
private $dataset; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \drupol\phpartition\Partitions\Partitions |
27
|
|
|
*/ |
28
|
|
|
private $lastRun; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var PartitionFactory |
32
|
|
|
*/ |
33
|
|
|
private $partitionFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PartitionItemFactory |
37
|
|
|
*/ |
38
|
|
|
private $partitionItemFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var PartitionsFactory |
42
|
|
|
*/ |
43
|
|
|
private $partitionsFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
final public function export(int $chunks = 1) |
49
|
|
|
{ |
50
|
|
|
return \array_map( |
51
|
|
|
static function (Partition $partition) { |
52
|
|
|
return \array_values($partition->exportArrayCopy()); |
53
|
|
|
}, |
54
|
|
|
$this |
55
|
|
|
->run($chunks) |
56
|
|
|
->getLastRun() |
57
|
|
|
->partitions() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getDataset() |
65
|
|
|
{ |
66
|
|
|
return $this->dataset; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \drupol\phpartition\Partition\Partition |
71
|
|
|
*/ |
72
|
|
|
final public function getDatasetPartition() |
73
|
|
|
{ |
74
|
|
|
$partition = $this->getPartitionFactory()::create(); |
75
|
|
|
|
76
|
|
|
foreach ($this->getDataset() as $data) { |
77
|
|
|
$partition->append($this->toPartitionItem($data)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $partition; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \drupol\phpartition\Partitions\Partitions |
85
|
|
|
*/ |
86
|
|
|
final public function getLastRun() |
87
|
|
|
{ |
88
|
|
|
return $this->lastRun; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \drupol\phpartition\Partition\PartitionFactory |
93
|
|
|
*/ |
94
|
|
|
public function getPartitionFactory() |
95
|
|
|
{ |
96
|
|
|
return $this->partitionFactory ?? new PartitionFactory(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return \drupol\phpartition\Partition\PartitionItemFactory |
101
|
|
|
*/ |
102
|
|
|
public function getPartitionItemFactory() |
103
|
|
|
{ |
104
|
|
|
return $this->partitionItemFactory ?? new PartitionItemFactory(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return \drupol\phpartition\Partitions\PartitionsFactory |
109
|
|
|
*/ |
110
|
|
|
public function getPartitionsFactory() |
111
|
|
|
{ |
112
|
|
|
return $this->partitionsFactory ?? new PartitionsFactory(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
final public function run(int $chunks = 1) |
119
|
|
|
{ |
120
|
|
|
$partitions = $this->getPartitionsFactory()::create($chunks); |
121
|
|
|
|
122
|
|
|
$dataPartition = $this->getDatasetPartition(); |
123
|
|
|
|
124
|
|
|
$this->fillPartitions($partitions, $dataPartition, $chunks); |
125
|
|
|
|
126
|
|
|
$this->lastRun = $partitions; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
final public function setDataset(iterable $dataset) |
135
|
|
|
{ |
136
|
|
|
$this->dataset = $dataset; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param mixed $originalItem |
143
|
|
|
* |
144
|
|
|
* @return \drupol\phpartition\Contract\Weightable |
145
|
|
|
*/ |
146
|
|
|
public function toPartitionItem($originalItem): Weightable |
147
|
|
|
{ |
148
|
|
|
return $this->getPartitionItemFactory()::create($originalItem); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param \drupol\phpartition\Partitions\Partitions $partitions |
153
|
|
|
* @param Partition $dataset |
154
|
|
|
* @param int $chunks |
155
|
|
|
*/ |
156
|
|
|
abstract protected function fillPartitions(Partitions $partitions, Partition $dataset, int $chunks): void; |
157
|
|
|
} |
158
|
|
|
|