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\PartitionItemFactory; |
11
|
|
|
use drupol\phpartition\Partitions\Partitions; |
12
|
|
|
use drupol\phpartition\Partitions\PartitionsFactory; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Partitioner. |
16
|
|
|
*/ |
17
|
|
|
abstract class Partitioner implements PartitionerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Traversable |
21
|
|
|
*/ |
22
|
|
|
protected $dataset; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var PartitionItemFactory |
26
|
|
|
*/ |
27
|
|
|
private $partitionItemFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var PartitionsFactory |
31
|
|
|
*/ |
32
|
|
|
private $partitionsFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
abstract public function add(...$values); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
final public function export(int $chunks = 1) |
43
|
|
|
{ |
44
|
|
|
$partitions = $this->getPartitionsFactory()::create($chunks); |
45
|
|
|
|
46
|
|
|
$datas = \array_map( |
47
|
|
|
function ($data) { |
48
|
|
|
return $this->toPartitionItem($data); |
49
|
|
|
}, |
50
|
|
|
\iterator_to_array($this->getDataset()) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->fillPartitions($partitions, $datas, $chunks); |
54
|
|
|
|
55
|
|
|
return \array_map( |
56
|
|
|
static function (Partition $partition) { |
57
|
|
|
return \array_values($partition->exportArrayCopy()); |
58
|
|
|
}, |
59
|
|
|
$partitions->partitions() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return \Traversable |
65
|
|
|
*/ |
66
|
|
|
public function getDataset() |
67
|
|
|
{ |
68
|
|
|
return $this->dataset; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \drupol\phpartition\Partition\PartitionItemFactory |
73
|
|
|
*/ |
74
|
|
|
public function getPartitionItemFactory() |
75
|
|
|
{ |
76
|
|
|
return $this->partitionItemFactory ?? new PartitionItemFactory(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \drupol\phpartition\Partitions\PartitionsFactory |
81
|
|
|
*/ |
82
|
|
|
public function getPartitionsFactory() |
83
|
|
|
{ |
84
|
|
|
return $this->partitionsFactory ?? new PartitionsFactory(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $dataset |
89
|
|
|
* |
90
|
|
|
* @return PartitionerInterface |
91
|
|
|
*/ |
92
|
|
|
final public function setDataset(array $dataset) |
93
|
|
|
{ |
94
|
|
|
foreach ($dataset as $value) { |
95
|
|
|
$this->add($value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $originalItem |
103
|
|
|
* |
104
|
|
|
* @return \drupol\phpartition\Contract\Weightable |
105
|
|
|
*/ |
106
|
|
|
public function toPartitionItem($originalItem): Weightable |
107
|
|
|
{ |
108
|
|
|
if ($originalItem instanceof Weightable) { |
109
|
|
|
return $originalItem; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->getPartitionItemFactory()::create($originalItem); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \drupol\phpartition\Partitions\Partitions $partitions |
117
|
|
|
* @param array $datas |
118
|
|
|
* @param int $chunks |
119
|
|
|
*/ |
120
|
|
|
abstract protected function fillPartitions(Partitions $partitions, array $datas, int $chunks): void; |
121
|
|
|
} |
122
|
|
|
|