This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataset.php
227 lines (196 loc) · 5.45 KB
/
Dataset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* League.Period Visualizer (https://github.com/bakame-php/period-visualizer)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Bakame\Period\Visualizer;
use League\Period\Period;
use League\Period\Sequence;
use function array_column;
use function count;
use function gettype;
use function is_scalar;
use function method_exists;
use function strlen;
final class Dataset implements \Countable, \IteratorAggregate, \JsonSerializable
{
/**
* @var array<int, array{0:string, 1:Sequence}>.
*/
private $pairs = [];
/**
* @var int
*/
private $labelMaxLength = 0;
/**
* @var Period|null
*/
private $boundaries;
/**
* constructor.
*/
public function __construct(iterable $pairs = [])
{
$this->appendAll($pairs);
}
/**
* Add a collection of pairs to the collection.
*/
public function appendAll(iterable $pairs): void
{
foreach ($pairs as [$label, $item]) {
$this->append($label, $item);
}
}
/**
* Add a new pair to the collection.
*
* @param mixed $label a scalar or a stringable object (implementing __toString method).
* @param Period|Sequence $item
*
* @throws \TypeError If the label or the item type are not supported.
*/
public function append($label, $item): void
{
if (!is_scalar($label) && !method_exists($label, '__toString')) {
throw new \TypeError('The label passed to '.__METHOD__.' must be a scalar or an stringable object, '.gettype($label).' given.');
}
if ($item instanceof Period) {
$item = new Sequence($item);
}
if (!$item instanceof Sequence) {
throw new \TypeError('The item passed to '.__METHOD__.' must be a '.Period::class.' or a '.Sequence::class.' instance, '.gettype($item).' given.');
}
$label = (string) $label;
$this->setLabelMaxLength($label);
$this->setBoundaries($item);
$this->pairs[] = [$label, $item];
}
/**
* Computes the label maximum length for the dataset.
*/
private function setLabelMaxLength(string $label): void
{
$labelLength = strlen($label);
if ($this->labelMaxLength < $labelLength) {
$this->labelMaxLength = $labelLength;
}
}
/**
* Computes the Period boundary for the dataset.
*/
private function setBoundaries(Sequence $sequence): void
{
if (null === $this->boundaries) {
$this->boundaries = $sequence->boundaries();
return;
}
$this->boundaries = $this->boundaries->merge(...$sequence);
}
/**
* Creates a new collection from a countable iterable structure.
*
* @param array|(\Countable&iterable) $sequence
* @param ?LabelGenerator $labelGenerator
*/
public static function fromSequence($sequence, ?LabelGenerator $labelGenerator = null): self
{
$labelGenerator = $labelGenerator ?? new LatinLetter();
$labels = $labelGenerator->generate(count($sequence));
$index = 0;
$dataset = new self();
foreach ($sequence as $item) {
$dataset->append($labels[$index], $item);
++$index;
}
return $dataset;
}
/**
* Creates a new collection from a generic iterable structure.
*/
public static function fromCollection(iterable $iterable): self
{
$dataset = new self();
foreach ($iterable as $label => $item) {
$dataset->append($label, $item);
}
return $dataset;
}
/**
* Returns the number of pairs.
*/
public function count(): int
{
return count($this->pairs);
}
/**
* Returns the pairs.
*
* @return \Iterator<int, array{0: string, 1: Sequence}>
*/
public function getIterator(): \Iterator
{
foreach ($this->pairs as $pair) {
yield $pair;
}
}
/**
* @var array<int, array{label:string, item:Sequence}>.
*/
public function jsonSerialize(): array
{
return array_map(function (array $pair): array {
return ['label' => $pair[0], 'item' => $pair[1]];
}, $this->pairs);
}
/**
* Tells whether the collection is empty.
*/
public function isEmpty(): bool
{
return [] === $this->pairs;
}
/**
* @return string[]
*/
public function labels(): array
{
return array_column($this->pairs, 0);
}
/**
* @return Sequence[]
*/
public function items(): array
{
return array_column($this->pairs, 1);
}
/**
* Returns the dataset boundaries.
*/
public function boundaries(): ?Period
{
return $this->boundaries;
}
/**
* Returns the label maximum length.
*/
public function labelMaxLength(): int
{
return $this->labelMaxLength;
}
/**
* Update the labels used for the dataset.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the newly specified labels.
*/
public function withLabels(LabelGenerator $labelGenerator): self
{
return self::fromSequence($this->items(), $labelGenerator);
}
}