Skip to content

Commit 4dfb68c

Browse files
committed
Improve Schema API and DateField API
1 parent 54b1bd3 commit 4dfb68c

8 files changed

Lines changed: 63 additions & 30 deletions

src/Buffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function inferSchema(?Inspector $inspector = null, array $header = []): S
231231

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

237237
/**

src/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public function inferSchema(?Inspector $inspector = null, array $header = []): S
425425

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

431431
/**

src/ResultSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function inferSchema(?Inspector $inspector = null, array $header = []): S
215215

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

221221
/**

src/Schema/FieldList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function default(): self
4545
new DateField(format: 'd/m/Y'),
4646
new DateField(format: 'Y-m-d H:i:s'),
4747
new JsonField(),
48-
RegexpField::uuid(),
48+
StructuredStringField::uuid(),
4949
);
5050
}
5151

src/Schema/Schema.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use League\Csv\TabularData;
2121
use ValueError;
2222

23+
use function array_diff_key;
24+
use function array_flip;
2325
use function array_key_exists;
2426
use function array_keys;
2527
use function array_map;
@@ -44,6 +46,30 @@ public function __construct(iterable $fields = [])
4446
$this->fields = $newFields;
4547
}
4648

49+
public function append(int|string $name, Field $field): self
50+
{
51+
self::assertNoDuplicate($this->fields, $name);
52+
53+
return new self([...$this->fields, ...[$name => $field]]);
54+
}
55+
56+
public function replace(int|string $name, Field $field): self
57+
{
58+
$this->has($name) || throw new ValueError('Field "'.$name.'" does not exist.');
59+
60+
$fields = $this->fields;
61+
$fields[$name] = $field;
62+
63+
return new self($fields);
64+
}
65+
66+
public function remove(int|string ...$names): self
67+
{
68+
return [] === $names
69+
? $this
70+
: new self(array_diff_key($this->fields, array_flip($names)));
71+
}
72+
4773
private static function assertNoDuplicate(array $data, string|int $key): void
4874
{
4975
! array_key_exists($key, $data) || throw new ValueError('The key already exists: '.$key);
@@ -118,9 +144,9 @@ public function map(callable $callback): Iterator
118144
/**
119145
* @return Iterator<int, array<mixed>>
120146
*/
121-
public function parse(TabularData $tabularData, array $header = []): Iterator
147+
public function parse(TabularData $tabularData): Iterator
122148
{
123-
return MapIterator::fromIterable($tabularData->getRecords($header), $this->format(...));
149+
return MapIterator::fromIterable($tabularData->getRecords($this->names()), $this->format(...));
124150
}
125151

126152
public function format(array $row): array
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
use function preg_match;
1818
use function trim;
1919

20-
final class RegexpField extends AbstractField
20+
final class StructuredStringField extends AbstractField
2121
{
2222
/** @var non-empty-string */
2323
public readonly string $name;
2424
public readonly string $pattern;
2525

26-
public function __construct(RegexpFieldDefinition $definition)
26+
public function __construct(StructuredStringFieldDefinition $definition)
2727
{
2828
parent::__construct($definition->confidenceThreshold);
2929

@@ -33,32 +33,32 @@ public function __construct(RegexpFieldDefinition $definition)
3333

3434
public static function uuid(float $confidenceThreshold = 0.8): self
3535
{
36-
return new self(RegexpFieldDefinition::uuid($confidenceThreshold));
36+
return new self(StructuredStringFieldDefinition::uuid($confidenceThreshold));
3737
}
3838

3939
public static function ulid(float $confidenceThreshold = 0.8): self
4040
{
41-
return new self(RegexpFieldDefinition::ulid($confidenceThreshold));
41+
return new self(StructuredStringFieldDefinition::ulid($confidenceThreshold));
4242
}
4343

4444
public static function hexColor(float $confidenceThreshold = 0.8): self
4545
{
46-
return new self(RegexpFieldDefinition::hexColor($confidenceThreshold));
46+
return new self(StructuredStringFieldDefinition::hexColor($confidenceThreshold));
4747
}
4848

4949
public static function jwtToken(float $confidenceThreshold = 0.8): self
5050
{
51-
return new self(RegexpFieldDefinition::jwtToken($confidenceThreshold));
51+
return new self(StructuredStringFieldDefinition::jwtToken($confidenceThreshold));
5252
}
5353

5454
public static function md5(float $confidenceThreshold = 0.8): self
5555
{
56-
return new self(RegexpFieldDefinition::md5($confidenceThreshold));
56+
return new self(StructuredStringFieldDefinition::md5($confidenceThreshold));
5757
}
5858

5959
public static function sha1(float $confidenceThreshold = 0.8): self
6060
{
61-
return new self(RegexpFieldDefinition::sha1($confidenceThreshold));
61+
return new self(StructuredStringFieldDefinition::sha1($confidenceThreshold));
6262
}
6363

6464
public function type(): FieldType

src/Schema/RegexpFieldDefinition.php renamed to src/Schema/StructuredStringFieldDefinition.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
use function preg_match;
1919

20-
final readonly class RegexpFieldDefinition
20+
final readonly class StructuredStringFieldDefinition
2121
{
22-
public string $pattern;
23-
public float $confidenceThreshold;
2422
/** @var non-empty-string */
2523
public string $name;
24+
public string $pattern;
25+
public float $confidenceThreshold;
2626

2727
public function __construct(
2828
string $name,
@@ -91,4 +91,11 @@ public static function sha1(float $confidenceThreshold = 0.8): self
9191
confidenceThreshold: $confidenceThreshold,
9292
);
9393
}
94+
95+
public function withConfidenceThreshold(float $confidenceThreshold): self
96+
{
97+
return $this->confidenceThreshold === $confidenceThreshold
98+
? $this
99+
: new self(name: $this->name, pattern: $this->pattern, confidenceThreshold: $confidenceThreshold);
100+
}
94101
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
use PHPUnit\Framework\Attributes\DataProvider;
1818
use PHPUnit\Framework\TestCase;
1919

20-
#[CoversClass(RegexpField::class)]
21-
final class RegexpFieldTest extends TestCase
20+
#[CoversClass(StructuredStringField::class)]
21+
final class StructuredStringFieldTest extends TestCase
2222
{
2323
// --------------------------------------------------------
2424
// Factory constructors
2525
// --------------------------------------------------------
2626

2727
public function testUuidFactoryCreatesValidStrategy(): void
2828
{
29-
$field = RegexpField::uuid();
29+
$field = StructuredStringField::uuid();
3030

3131
self::assertSame(FieldType::StructuredString, $field->type());
3232
self::assertSame('uuid', $field->name());
@@ -35,21 +35,21 @@ public function testUuidFactoryCreatesValidStrategy(): void
3535

3636
public function testUlidFactoryCreatesValidStrategy(): void
3737
{
38-
$field = RegexpField::ulid();
38+
$field = StructuredStringField::ulid();
3939

4040
self::assertSame('ulid', $field->name());
4141
}
4242

4343
public function testHexColorFactoryCreatesValidStrategy(): void
4444
{
45-
$field = RegexpField::hexColor();
45+
$field = StructuredStringField::hexColor();
4646

4747
self::assertSame('hex_color', $field->name());
4848
}
4949

5050
public function testJwtTokenFactoryCreatesValidStrategy(): void
5151
{
52-
$field = RegexpField::jwtToken();
52+
$field = StructuredStringField::jwtToken();
5353

5454
self::assertSame('jwt_token', $field->name());
5555
}
@@ -81,10 +81,10 @@ public function testParseValidValues(string $input, string $expected): void
8181
{
8282
// NOTE: we test multiple strategies explicitly because pattern differs
8383
$fields = [
84-
RegexpField::uuid(),
85-
RegexpField::ulid(),
86-
RegexpField::hexColor(),
87-
RegexpField::jwtToken(),
84+
StructuredStringField::uuid(),
85+
StructuredStringField::ulid(),
86+
StructuredStringField::hexColor(),
87+
StructuredStringField::jwtToken(),
8888
];
8989

9090
$matched = false;
@@ -118,7 +118,7 @@ public static function provideInvalidValues(): array
118118
#[DataProvider('provideInvalidValues')]
119119
public function testParseInvalidValues(mixed $input): void
120120
{
121-
$field = RegexpField::uuid();
121+
$field = StructuredStringField::uuid();
122122

123123
self::assertNull($field->parse($input));
124124
}
@@ -129,7 +129,7 @@ public function testParseInvalidValues(mixed $input): void
129129

130130
public function testParseTrimsInput(): void
131131
{
132-
$field = RegexpField::uuid();
132+
$field = StructuredStringField::uuid();
133133

134134
$value = ' 550e8400-e29b-41d4-a716-446655440000 ';
135135

@@ -145,7 +145,7 @@ public function testParseTrimsInput(): void
145145

146146
public function testTypeMatchesDefinition(): void
147147
{
148-
$field = RegexpField::hexColor();
148+
$field = StructuredStringField::hexColor();
149149

150150
self::assertSame(FieldType::StructuredString, $field->type());
151151
}

0 commit comments

Comments
 (0)