Skip to content

Commit ce8aeda

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

5 files changed

Lines changed: 39 additions & 19 deletions

File tree

src/Buffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function schema(?Inspector $inspector = null, array $header = []): Schema
229229
return ($inspector ?? Inspector::default())->schema($this, $header);
230230
}
231231

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

src/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public function schema(?Inspector $inspector = null, array $header = []): Schema
423423
return ($inspector ?? Inspector::default())->schema($this, $header);
424424
}
425425

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

src/ResultSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function schema(?Inspector $inspector = null, array $header = []): Schema
213213
return ($inspector ?? Inspector::default())->schema($this, $header);
214214
}
215215

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

src/Schema/DateField.php

Lines changed: 36 additions & 11 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,10 +81,18 @@ 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 (0 < $errors['warning_count'] || 0 < $errors['error_count']) {
91+
return null;
92+
}
93+
94+
return $value;
95+
} catch (ValueError) {
7296
return null;
7397
}
7498
}
@@ -77,6 +101,7 @@ public function metadata(): Metadata
77101
{
78102
return new Metadata([
79103
'format' => $this->format,
104+
'timezone' => $this->timeZone?->getName(),
80105
]);
81106
}
82107
}

src/Schema/RegexpField.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ public function parse(mixed $value): ?string
8282
return ('' === $value || 1 !== preg_match($this->pattern, $value)) ? null : $value;
8383
}
8484

85-
public function pattern(): string
86-
{
87-
return $this->pattern;
88-
}
89-
9085
public function metadata(): Metadata
9186
{
9287
return new Metadata([

0 commit comments

Comments
 (0)