Skip to content

Commit 5b427b8

Browse files
committed
Improve Schema API and DateField API
1 parent a2ceb34 commit 5b427b8

16 files changed

Lines changed: 94 additions & 63 deletions

docs/9.0/connections/instantiation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Alternatively, you can use the <code>fromStream</code> method.</p>
6161
```php
6262
public static AbstractCsv::fromStream(SplFileObject|resource $stream): self
6363
```
64+
6465
Creates a new object from a stream resource or a streaming object.
6566

6667
```php

docs/9.0/reader/record-mapping.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: Converts your CSV records into PHP objects using PHP's powerful Ref
88

99
<p class="message-notice">New in version <code>9.12.0</code></p>
1010

11-
If you are working with a class which implements the `TabularDataReader` interface you can now deserialize
11+
If you are working with a class which implements the `TabularData` interface you can now deserialize
1212
your data using the `TabularDataReader::getRecordsAsObject` method. The method will convert your document records
1313
into objects using PHP's powerful Reflection API.
1414

src/Buffer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ public function reduce(callable $callback, mixed $initial = null): mixed
224224
return $initial;
225225
}
226226

227-
public function schema(?Inspector $inspector = null, array $header = []): Schema
227+
public function inferSchema(?Inspector $inspector = null, array $header = []): Schema
228228
{
229229
return ($inspector ?? Inspector::default())->schema($this, $header);
230230
}
231231

232-
public function getTypedRecords(?Inspector $inspector = null, array $header = []): Iterator
232+
public function inferRecords(?Inspector $inspector = null, array $header = []): Iterator
233233
{
234-
return $this->schema($inspector)->parse($this, $header);
234+
return $this->inferSchema($inspector, $header)->parse($this, $header);
235235
}
236236

237237
/**

src/Reader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,14 @@ public function map(callable $callback): Iterator
418418
return MapIterator::fromIterable($this, $callback);
419419
}
420420

421-
public function schema(?Inspector $inspector = null, array $header = []): Schema
421+
public function inferSchema(?Inspector $inspector = null, array $header = []): Schema
422422
{
423423
return ($inspector ?? Inspector::default())->schema($this, $header);
424424
}
425425

426-
public function getTypedRecords(?Inspector $inspector = null, array $header = []): Iterator
426+
public function inferRecords(?Inspector $inspector = null, array $header = []): Iterator
427427
{
428-
return $this->schema($inspector)->parse($this, $header);
428+
return $this->inferSchema($inspector, $header)->parse($this, $header);
429429
}
430430

431431
/**

src/ResultSet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ public function map(callable $callback): Iterator
208208
return MapIterator::fromIterable($this, $callback);
209209
}
210210

211-
public function schema(?Inspector $inspector = null, array $header = []): Schema
211+
public function inferSchema(?Inspector $inspector = null, array $header = []): Schema
212212
{
213213
return ($inspector ?? Inspector::default())->schema($this, $header);
214214
}
215215

216-
public function getTypedRecords(?Inspector $inspector = null, array $header = []): Iterator
216+
public function inferRecords(?Inspector $inspector = null, array $header = []): Iterator
217217
{
218-
return $this->schema($inspector)->parse($this, $header);
218+
return $this->inferSchema($inspector, $header)->parse($this, $header);
219219
}
220220

221221
/**

src/Schema/AbstractField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function evaluate(mixed $value): int
6969
return null !== $this->parse($value) ? 1 : -1;
7070
}
7171

72-
public function metadata(): Metadata
72+
public function metadata(): FieldMetadata
7373
{
74-
return new Metadata();
74+
return new FieldMetadata();
7575
}
7676
}

src/Schema/DateField.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use DateTimeImmutable;
1717
use DateTimeInterface;
18+
use DateTimeZone;
1819
use Exception;
1920
use ValueError;
2021

@@ -24,14 +25,34 @@
2425
final class DateField extends AbstractField
2526
{
2627
public readonly string $format;
28+
public readonly ?DateTimeZone $timeZone;
2729

28-
public function __construct(string $format, float $confidenceThreshold = 0.8)
30+
public function __construct(string $format, DateTimeZone|string|null $timeZone = null, float $confidenceThreshold = 0.8)
2931
{
3032
$format = trim($format);
31-
'' !== $format || throw new ValueError('The date field strategy format can not be empty.');
33+
'' !== $format || throw new ValueError('The date field format can not be empty.');
34+
$timeZone = self::filterTimezone($timeZone);
3235

3336
parent::__construct($confidenceThreshold);
3437
$this->format = $format;
38+
$this->timeZone = $timeZone;
39+
}
40+
41+
private static function filterTimezone(DateTimeZone|string|null $timeZone): ?DateTimeZone
42+
{
43+
if (null === $timeZone) {
44+
return null;
45+
}
46+
47+
if ($timeZone instanceof DateTimeZone) {
48+
return $timeZone;
49+
}
50+
51+
try {
52+
return new DateTimeZone($timeZone);
53+
} catch (Exception $exception) {
54+
throw new ValueError('The date field timezone value `'.$timeZone.'` is invalid.', previous: $exception);
55+
}
3556
}
3657

3758
public function type(): FieldType
@@ -44,11 +65,6 @@ public function name(): string
4465
return FieldType::Date->value;
4566
}
4667

47-
public function format(): string
48-
{
49-
return $this->format;
50-
}
51-
5268
public function parse(mixed $value): ?DateTimeImmutable
5369
{
5470
if ($value instanceof DateTimeInterface) {
@@ -65,18 +81,30 @@ public function parse(mixed $value): ?DateTimeImmutable
6581
}
6682

6783
try {
68-
$value = DateTimeImmutable::createFromFormat($this->format, $value);
69-
70-
return false === $value ? null : $value;
71-
} catch (Exception) {
84+
$value = DateTimeImmutable::createFromFormat($this->format, $value, $this->timeZone);
85+
if (false === $value) {
86+
return null;
87+
}
88+
89+
$errors = DateTimeImmutable::getLastErrors();
90+
if (
91+
(isset($errors['warning_count']) && 0 < $errors['warning_count']) ||
92+
(isset($errors['error_count']) && 0 < $errors['error_count'])
93+
) {
94+
return null;
95+
}
96+
97+
return $value;
98+
} catch (ValueError) {
7299
return null;
73100
}
74101
}
75102

76-
public function metadata(): Metadata
103+
public function metadata(): FieldMetadata
77104
{
78-
return new Metadata([
105+
return new FieldMetadata([
79106
'format' => $this->format,
107+
'timezone' => $this->timeZone?->getName(),
80108
]);
81109
}
82110
}

src/Schema/EnumField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public function parse(mixed $value): ?UnitEnum
107107
return $enumClass::tryFrom($value);
108108
}
109109

110-
public function metadata(): Metadata
110+
public function metadata(): FieldMetadata
111111
{
112-
return new Metadata([
112+
return new FieldMetadata([
113113
'class' => $this->enumClass,
114114
]);
115115
}

src/Schema/Field.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function name(): string;
3434
*/
3535
public function parse(mixed $value): mixed;
3636

37+
/**
38+
* Returns the confidence on the field value
39+
*
40+
* The range of valide value is from 0.0 up to including 1.0
41+
*/
3742
public function confidenceThreshold(): float;
3843

3944
/**
@@ -58,5 +63,5 @@ public function score(iterable $values): float;
5863
*/
5964
public function evaluate(mixed $value): int;
6065

61-
public function metadata(): Metadata;
66+
public function metadata(): FieldMetadata;
6267
}

src/Schema/FieldListTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function name(): string
4848
return $this->type->name;
4949
}
5050

51-
public function metadata(): Metadata
51+
public function metadata(): FieldMetadata
5252
{
53-
return new Metadata();
53+
return new FieldMetadata();
5454
}
5555

5656
public function confidenceThreshold(): float

0 commit comments

Comments
 (0)