Skip to content

Commit d316f0f

Browse files
committed
Bugfix Value type detection
1 parent edd5b21 commit d316f0f

File tree

5 files changed

+17
-42
lines changed

5 files changed

+17
-42
lines changed

src/Type.php

+4-24
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,15 @@ public static function tryFromVariable(mixed $variable): self|null
5858
is_int($variable) => Type::Integer,
5959
is_float($variable) => Type::Decimal,
6060
is_bool($variable) => Type::Boolean,
61-
is_string($variable) => match (true) {
62-
null !== Token::tryFromString($variable) => Type::Token,
63-
null !== ByteSequence::tryFromEncoded($variable) => Type::ByteSequence,
64-
1 === preg_match('/[^\x20-\x7f]/', $variable) => Type::DisplayString,
65-
default => Type::String,
66-
},
61+
is_string($variable) && 1 !== preg_match('/[^\x20-\x7f]/', $variable) => Type::String,
6762
default => null,
6863
};
6964
}
7065

71-
/**
72-
* @deprecated since version 1.2.0 will be removed in the next major release.
73-
* @see Type::fromVariable()
74-
* @codeCoverageIgnore
75-
*
76-
* @throws InvalidArgument if the value can not be resolved into a supported HTTP structured field data type
77-
*/
78-
public static function fromValue(mixed $value): self
66+
public function assert(mixed $value): bool
7967
{
80-
return self::fromVariable($value);
81-
}
68+
$new = self::tryFromVariable($value);
8269

83-
/**
84-
* @deprecated since version 1.2.0 will be removed in the next major release.
85-
* @see Type::tryFromVariable()
86-
* @codeCoverageIgnore
87-
*/
88-
public static function tryFromValue(mixed $value): self|null
89-
{
90-
return self::tryFromVariable($value);
70+
return null !== $new && $new->equals($this);
9171
}
9272
}

src/Value.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,19 @@ final class Value
3939
*/
4040
public function __construct(mixed $value)
4141
{
42-
$this->value = match (true) {
43-
$value instanceof Item => $value->value(),
42+
[$this->value, $this->type] = match (true) {
43+
$value instanceof Item => [$value->value(), $value->type()],
4444
$value instanceof Token,
4545
$value instanceof ByteSequence,
46-
$value instanceof DisplayString,
46+
$value instanceof DisplayString => [$value, $value->type()],
4747
false === $value,
48-
$value => $value,
49-
$value instanceof DateTimeInterface => self::filterDate($value),
50-
is_int($value) => self::filterIntegerRange($value, 'Integer'),
51-
is_float($value) => self::filterDecimal($value),
52-
is_string($value) => self::filterString($value),
48+
$value => [$value, Type::Boolean],
49+
$value instanceof DateTimeInterface => [self::filterDate($value), Type::Date],
50+
is_int($value) => [self::filterIntegerRange($value, 'Integer'), Type::Integer],
51+
is_float($value) => [self::filterDecimal($value), Type::Decimal],
52+
is_string($value) => [self::filterString($value), Type::String],
5353
default => throw new ValueError('Unknown or unsupported type.')
5454
};
55-
$this->type = Type::fromVariable($this->value);
5655
}
5756

5857
/**

tests/IetfTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function variableRfc8941Provider(): Iterator
4545
];
4646

4747
yield 'token' => [
48-
'value' => 'application/xml',
48+
'value' => Token::fromString('application/xml'),
4949
'expectedRfc8941' => true,
5050
'expectedRfc9651' => true,
5151
];
@@ -57,13 +57,13 @@ public static function variableRfc8941Provider(): Iterator
5757
];
5858

5959
yield 'bytesequence' => [
60-
'value' => 'cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==',
60+
'value' => ByteSequence::fromEncoded('cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg=='),
6161
'expectedRfc8941' => true,
6262
'expectedRfc9651' => true,
6363
];
6464

6565
yield 'displaystring' => [
66-
'value' => 'bébé',
66+
'value' => DisplayString::fromDecoded('bébé'),
6767
'expectedRfc8941' => false,
6868
'expectedRfc9651' => true,
6969
];

tests/ItemTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static function itemTypeProvider(): iterable
283283
],
284284
'string' => [
285285
'item' => Item::new('42'),
286-
'expectedType' => Type::ByteSequence,
286+
'expectedType' => Type::String,
287287
],
288288
'display string' => [
289289
'item' => Item::new(DisplayString::fromDecoded('😊')),

tests/TypeTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function itemTypeProvider(): iterable
5959
],
6060
'string as a Bytesequence' => [
6161
'value' => '42',
62-
'expectedType' => Type::ByteSequence,
62+
'expectedType' => Type::String,
6363
],
6464
'string as a String' => [
6565
'value' => 'Hello Boy!',
@@ -85,10 +85,6 @@ public static function itemTypeProvider(): iterable
8585
'value' => Item::new(new DateTimeImmutable('2020-07-12 13:37:00')),
8686
'expectedType' => Type::Date,
8787
],
88-
'a display string from a string' => [
89-
'value' => 'füü',
90-
'expectedType' => Type::DisplayString,
91-
],
9288
'a display string from an object' => [
9389
'value' => DisplayString::fromDecoded('füü'),
9490
'expectedType' => Type::DisplayString,

0 commit comments

Comments
 (0)