Skip to content

Commit 80cf2ab

Browse files
committed
Simplify String Field
1 parent 0026985 commit 80cf2ab

17 files changed

Lines changed: 403 additions & 308 deletions

src/Schema/DateField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function type(): FieldType
154154

155155
public function name(): string
156156
{
157-
return FieldType::Date->value;
157+
return FieldType::Date->value.'(format='.$this->format.',timezone='.$this->timezone->getName().')';
158158
}
159159

160160
public function parse(mixed $value): ?DateTimeInterface

src/Schema/DateFieldTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function test_it_can_return_another_implementing_datetime_interface(): vo
7575
self::assertSame(MyDate::class, $field->metadata()->get('class'));
7676
self::assertSame('Y-m-d', $field->metadata()->get('format'));
7777
self::assertSame('UTC', $field->metadata()->get('timezone'));
78+
self::assertSame('date(format=Y-m-d,timezone=UTC)', $field->name());
7879
}
7980
}
8081

src/Schema/EnumField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function type(): FieldType
7070

7171
public function name(): string
7272
{
73-
return FieldType::Enum->value;
73+
return FieldType::Enum->value.'('.$this->enumClass.')';
7474
}
7575

7676
public function parse(mixed $value): ?UnitEnum

src/Schema/EnumFieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testItParsesEnumByInstance(): void
5252
self::assertSame($value, $result);
5353
self::assertSame(FieldType::Enum, $this->field->type());
5454
self::assertSame(TestEnum::class, $this->field->enumClass);
55-
self::assertSame('enum', $this->field->name());
55+
self::assertSame('enum(League\Csv\Schema\TestEnum)', $this->field->name());
5656
}
5757

5858
public function testItParsesEnumByName(): void

src/Schema/FieldEvaluator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function score(iterable $values): float
5959
return 0 < $counted ? $valid / $counted : 0.0;
6060
}
6161

62+
/**
63+
* @return int<-1, 1>
64+
*/
6265
public function evaluate(mixed $value): int
6366
{
6467
if (null === $value) {

src/Schema/FieldType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ enum FieldType: string
2222
case Json = 'json';
2323
case Numeric = 'numeric';
2424
case String = 'string';
25-
case StructuredString = 'structured_string';
2625
case Set = 'set';
2726
}

src/Schema/NumericField.php

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,100 @@
2424

2525
final class NumericField extends FieldEvaluator implements Field
2626
{
27+
public function __construct(
28+
public readonly int|float|null $min = null,
29+
public readonly int|float|null $max = null,
30+
float $confidenceThreshold = 0.8
31+
) {
32+
if (null !== $min && null !== $max && $min > $max) {
33+
throw new \ValueError('Minimum length can not be greater than maximum length.');
34+
}
35+
36+
parent::__construct($confidenceThreshold);
37+
}
38+
39+
public static function min(int $value, float $confidenceThreshold = 0.8): self
40+
{
41+
return new self(min: $value, max: null, confidenceThreshold: $confidenceThreshold);
42+
}
43+
44+
public static function max(int $value, float $confidenceThreshold = 0.8): self
45+
{
46+
return new self(min: null, max: $value, confidenceThreshold: $confidenceThreshold);
47+
}
48+
49+
public static function fixed(int $value, float $confidenceThreshold = 0.8): self
50+
{
51+
return new self(min: $value, max: $value, confidenceThreshold: $confidenceThreshold);
52+
}
53+
54+
public static function between(int $min, int $max, float $confidenceThreshold = 0.8): self
55+
{
56+
return new self(min: $min, max: $max, confidenceThreshold: $confidenceThreshold);
57+
}
58+
59+
public static function positive(float $confidenceThreshold = 0.8): self
60+
{
61+
return new self(min: 0, confidenceThreshold: $confidenceThreshold);
62+
}
63+
64+
public static function negative(float $confidenceThreshold = 0.8): self
65+
{
66+
return new self(max: 0, confidenceThreshold: $confidenceThreshold);
67+
}
68+
2769
public function type(): FieldType
2870
{
2971
return FieldType::Numeric;
3072
}
3173

3274
public function name(): string
3375
{
34-
return FieldType::Numeric->value;
76+
$range = (null === $this->min && null === $this->max)
77+
? '' :
78+
(
79+
$this->min === $this->max
80+
? '['.$this->min.']'
81+
: '['.$this->min.','.$this->max.']'
82+
);
83+
84+
return FieldType::Numeric->value.$range;
3585
}
3686

3787
public function parse(mixed $value): int|float|null
3888
{
39-
if (is_float($value) || is_int($value)) {
40-
return $value;
89+
if (is_string($value)) {
90+
$value = trim($value);
91+
if ('' === $value || !is_numeric($value)) {
92+
return null;
93+
}
94+
95+
$filterValue = filter_var($value, FILTER_VALIDATE_INT);
96+
$value = false === $filterValue ? (float) $value : $filterValue;
4197
}
4298

43-
if (!is_string($value)) {
99+
if (!is_float($value) && !is_int($value)) {
44100
return null;
45101
}
46102

47-
$value = trim($value);
48-
if ('' === $value || !is_numeric($value)) {
103+
if (null !== $this->min && $value < $this->min) {
49104
return null;
50105
}
51106

52-
$filterValue = filter_var($value, FILTER_VALIDATE_INT);
107+
if (null !== $this->max && $value > $this->max) {
108+
return null;
109+
}
53110

54-
return false === $filterValue ? (float) $value : $filterValue;
111+
return $value;
55112
}
56113

57114
public function metadata(): FieldMetadata
58115
{
59-
return new FieldMetadata();
116+
return new FieldMetadata([
117+
'constraints' => [
118+
'min_value' => $this->min,
119+
'max_value' => $this->max,
120+
],
121+
]);
60122
}
61123
}

src/Schema/NumericFieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ public function testParseInvalidValues(mixed $input): void
8383

8484
public function test_metadata_contains_expected_structure(): void
8585
{
86-
self::assertTrue($this->field->metadata()->isEmpty());
86+
self::assertFalse($this->field->metadata()->isEmpty());
8787
}
8888
}

src/Schema/SetField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function type(): FieldType
6767

6868
public function name(): string
6969
{
70-
return FieldType::Set->value;
70+
return FieldType::Set->value.'('.$this->enumField->name().')';
7171
}
7272

7373
/**

src/Schema/SetFieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function test_it_returns_the_correct_name(): void
5959
{
6060
$field = SetField::fromEnum(TestSetEnum::class);
6161

62-
self::assertSame(FieldType::Set->value, $field->name());
62+
self::assertSame('set(enum(League\Csv\Schema\Tests\TestSetEnum))', $field->name());
6363
}
6464

6565
public function test_it_returns_null_for_non_string_values(): void

0 commit comments

Comments
 (0)