Skip to content

Commit f81333a

Browse files
committed
Improve Schema API
1 parent 76522f5 commit f81333a

4 files changed

Lines changed: 27 additions & 35 deletions

File tree

src/Schema/JsonFieldTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ public function testInvalidConstructorArgumentsThrow(): void
130130

131131
public function test_metadata_contains_expected_structure(): void
132132
{
133-
$field = new JsonField(depth: 0, flags: JSON_BIGINT_AS_STRING);
133+
$field = new JsonField(depth: 2, flags: JSON_BIGINT_AS_STRING);
134134

135135
self::assertSame([
136136
'flags' => JSON_BIGINT_AS_STRING,
137-
'depth' => 0,
137+
'depth' => 2,
138138
], $field->metadata()->all());
139139
}
140140
}

src/Schema/StructuredStringField.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@
1919

2020
final class StructuredStringField extends FieldEvaluator implements Field
2121
{
22-
/** @var non-empty-string */
23-
public readonly string $name;
24-
public readonly string $pattern;
25-
26-
public function __construct(StructuredStringFieldDefinition $definition)
22+
public function __construct(public readonly StructuredStringFieldDefinition $definition)
2723
{
2824
parent::__construct($definition->confidenceThreshold);
29-
30-
$this->name = $definition->name;
31-
$this->pattern = $definition->pattern;
3225
}
3326

3427
public static function uuid(float $confidenceThreshold = 0.8): self
@@ -80,7 +73,7 @@ public function type(): FieldType
8073

8174
public function name(): string
8275
{
83-
return $this->name;
76+
return $this->definition->fieldTypeName;
8477
}
8578

8679
public function parse(mixed $value): ?string
@@ -91,13 +84,13 @@ public function parse(mixed $value): ?string
9184

9285
$value = trim($value);
9386

94-
return ('' === $value || 1 !== preg_match($this->pattern, $value)) ? null : $value;
87+
return ('' === $value || 1 !== preg_match($this->definition->pattern, $value)) ? null : $value;
9588
}
9689

9790
public function metadata(): FieldMetadata
9891
{
9992
return new FieldMetadata([
100-
'pattern' => $this->pattern,
93+
'pattern' => $this->definition->pattern,
10194
]);
10295
}
10396
}

src/Schema/StructuredStringFieldDefinition.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,24 @@
1919

2020
final readonly class StructuredStringFieldDefinition
2121
{
22-
/** @var non-empty-string */
23-
public string $name;
24-
public string $pattern;
25-
public float $confidenceThreshold;
26-
22+
/**
23+
* @param non-empty-string $fieldTypeName
24+
* @param non-empty-string $pattern
25+
*/
2726
public function __construct(
28-
string $name,
29-
string $pattern,
30-
float $confidenceThreshold,
27+
public string $fieldTypeName,
28+
public string $pattern,
29+
public float $confidenceThreshold,
3130
) {
3231
($confidenceThreshold >= 0 && $confidenceThreshold <= 1) || throw new ValueError('the confidence threshold must be between 0 and 1.');
33-
false !== @preg_match($pattern, '') || throw new ValueError('the regular expression pattern "'.$pattern.'" is not valid. Did you forget the delimiter?');
34-
('' !== $name && 1 === preg_match('/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/', $name)) || throw new ValueError('The name "'.$name.'" is not a valid snake case variable name.');
35-
36-
$this->name = $name;
37-
$this->pattern = $pattern;
38-
$this->confidenceThreshold = $confidenceThreshold;
32+
('' !== $pattern && false !== @preg_match($pattern, '')) || throw new ValueError('the regular expression pattern "'.$pattern.'" is not valid. Did you forget the delimiter?');
33+
('' !== $fieldTypeName && 1 === preg_match('/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/', $fieldTypeName)) || throw new ValueError('The name "'.$fieldTypeName.'" is not a valid snake case variable name.');
3934
}
4035

4136
public static function uuid(float $confidenceThreshold = 0.8): self
4237
{
4338
return new self(
44-
name: 'uuid',
39+
fieldTypeName: 'uuid',
4540
pattern: '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i',
4641
confidenceThreshold: $confidenceThreshold,
4742
);
@@ -50,7 +45,7 @@ public static function uuid(float $confidenceThreshold = 0.8): self
5045
public static function ulid(float $confidenceThreshold = 0.8): self
5146
{
5247
return new self(
53-
name: 'ulid',
48+
fieldTypeName: 'ulid',
5449
pattern: '/^[0-9A-HJKMNP-TV-Z]{26}$/i',
5550
confidenceThreshold: $confidenceThreshold,
5651
);
@@ -59,7 +54,7 @@ public static function ulid(float $confidenceThreshold = 0.8): self
5954
public static function jwtToken(float $confidenceThreshold = 0.8): self
6055
{
6156
return new self(
62-
name: 'jwt_token',
57+
fieldTypeName: 'jwt_token',
6358
pattern: '/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/i',
6459
confidenceThreshold: $confidenceThreshold,
6560
);
@@ -68,7 +63,7 @@ public static function jwtToken(float $confidenceThreshold = 0.8): self
6863
public static function hexColor(float $confidenceThreshold = 0.8): self
6964
{
7065
return new self(
71-
name: 'hex_color',
66+
fieldTypeName: 'hex_color',
7267
pattern: '/^#(?:[0-9a-fA-F]{3}){1,2}$/i',
7368
confidenceThreshold: $confidenceThreshold,
7469
);
@@ -77,7 +72,7 @@ public static function hexColor(float $confidenceThreshold = 0.8): self
7772
public static function md5(float $confidenceThreshold = 0.8): self
7873
{
7974
return new self(
80-
name: 'md5',
75+
fieldTypeName: 'md5',
8176
pattern: '/^[a-fA-F0-9]{32}$/',
8277
confidenceThreshold: $confidenceThreshold,
8378
);
@@ -86,7 +81,7 @@ public static function md5(float $confidenceThreshold = 0.8): self
8681
public static function sha1(float $confidenceThreshold = 0.8): self
8782
{
8883
return new self(
89-
name: 'sha1',
84+
fieldTypeName: 'sha1',
9085
pattern: '/^[a-fA-F0-9]{40}$/',
9186
confidenceThreshold: $confidenceThreshold,
9287
);
@@ -96,6 +91,10 @@ public function withConfidenceThreshold(float $confidenceThreshold): self
9691
{
9792
return $this->confidenceThreshold === $confidenceThreshold
9893
? $this
99-
: new self(name: $this->name, pattern: $this->pattern, confidenceThreshold: $confidenceThreshold);
94+
: new self(
95+
fieldTypeName: $this->fieldTypeName,
96+
pattern: $this->pattern,
97+
confidenceThreshold: $confidenceThreshold
98+
);
10099
}
101100
}

src/Schema/StructuredStringFieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,6 @@ public function test_metadata_contains_expected_structure(): void
154154
{
155155
$field = StructuredStringField::hexColor();
156156

157-
self::assertSame(['pattern' => $field->pattern], $field->metadata()->all());
157+
self::assertSame(['pattern' => $field->definition->pattern], $field->metadata()->all());
158158
}
159159
}

0 commit comments

Comments
 (0)