Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add true/false as primitive types #1582

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Handler/UnionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function matchSimpleType(mixed $data, array $type, Context $context): mi

private function isPrimitiveType(string $type): bool
{
return in_array($type, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'string', 'array'], true);
return in_array($type, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'true', 'false', 'string', 'array'], true);
}

private function testPrimitive(mixed $data, string $type, string $format): bool
Expand All @@ -136,6 +136,12 @@ private function testPrimitive(mixed $data, string $type, string $format): bool
case 'boolean':
return (string) (bool) $data === (string) $data;

case 'true':
return true === $data;

case 'false':
return false === $data;

case 'string':
return is_string($data);
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Fixtures/TypedProperties/UnionTypedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class UnionTypedProperties
{
private int|bool|float|string|array $data;

private int|bool|float|string|null $nullableData;
private int|bool|float|string|null $nullableData = null;

private string|false $valueTypedUnion;

public function __construct($data)
public function __construct($data, $nullableData, $valueTypedUnion)
{
$this->data = $data;
$this->nullableData = $nullableData;
$this->valueTypedUnion = $valueTypedUnion;
}
}
4 changes: 2 additions & 2 deletions tests/Serializer/BaseSerializationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1973,9 +1973,9 @@ public function testSerializingUnionTypedProperties()
$this->markTestSkipped(sprintf('%s requires PHP 8.0', TypedPropertiesDriver::class));
}

$object = new TypedProperties\UnionTypedProperties(10000);
$object = new TypedProperties\UnionTypedProperties(10000, null, false);

self::assertEquals(static::getContent('data_integer'), $this->serialize($object));
self::assertEquals(static::getContent('union_typed_properties_integer'), $this->serialize($object));
}

public function testSerializingUnionDocBlockTypesProperties()
Expand Down
19 changes: 13 additions & 6 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ protected static function getContent($key)
$outputs['object_with_enums'] = '{"ordinary":"Clubs","backed_value":"C","backed_without_param":"C","ordinary_array":["Clubs","Spades"],"backed_array":["C","H"],"backed_array_without_param":["C","H"],"ordinary_auto_detect":"Clubs","backed_auto_detect":"C","backed_int_auto_detect":3,"backed_int":3,"backed_name":"C","backed_int_forced_str":3}';
$outputs['object_with_autodetect_enums'] = '{"ordinary_array_auto_detect":["Clubs","Spades"],"backed_array_auto_detect":["C","H"],"mixed_array_auto_detect":["Clubs","H"]}';
$outputs['object_with_enums_disabled'] = '{"ordinary_array_auto_detect":[{"name":"Clubs"},{"name":"Spades"}],"backed_array_auto_detect":[{"name":"Clubs","value":"C"},{"name":"Hearts","value":"H"}],"mixed_array_auto_detect":[{"name":"Clubs"},{"name":"Hearts","value":"H"}]}';
$outputs['union_typed_properties_integer'] = '{"data":10000,"value_typed_union":false}';
$outputs['union_typed_properties_float'] = '{"data":1.236,"value_typed_union":false}';
$outputs['union_typed_properties_bool'] = '{"data":false,"value_typed_union":false}';
$outputs['union_typed_properties_string'] = '{"data":"foo","value_typed_union":false}';
$outputs['union_typed_properties_array'] = '{"data":[1,2,3],"value_typed_union":false}';
$outputs['union_typed_properties_false_string'] = '{"data":false,"value_typed_union":"foo"}';
}

if (!isset($outputs[$key])) {
Expand Down Expand Up @@ -446,11 +452,12 @@ public static function getTypeHintedArraysAndStdClass()

public static function getSimpleUnionProperties(): iterable
{
yield 'int' => [10000, 'data_integer'];
yield [1.236, 'data_float'];
yield [false, 'data_bool'];
yield ['foo', 'data_string'];
yield [[1, 2, 3], 'data_array'];
yield 'int' => [[10000, null, false], 'union_typed_properties_integer'];
yield 'float' => [[1.236, null, false], 'union_typed_properties_float'];
yield 'bool' => [[false, null, false], 'union_typed_properties_bool'];
yield 'string' => [['foo', null, false], 'union_typed_properties_string'];
yield 'array' => [[[1, 2, 3], null, false], 'union_typed_properties_array'];
yield 'false_array' => [[false, null, 'foo'], 'union_typed_properties_false_string'];
}

/**
Expand All @@ -465,7 +472,7 @@ public function testUnionProperties($data, string $expected): void
return;
}

$object = new UnionTypedProperties($data);
$object = new UnionTypedProperties(...$data);
self::assertEquals($object, $this->deserialize(static::getContent($expected), UnionTypedProperties::class));
self::assertEquals($this->serialize($object), static::getContent($expected));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function testThrowingExceptionWhenDeserializingUnionProperties()

$this->expectException(RuntimeException::class);

$object = new UnionTypedProperties(10000);
$object = new UnionTypedProperties(10000, null, false);
self::assertEquals($object, $this->deserialize(static::getContent('data_integer'), UnionTypedProperties::class));
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Serializer/xml/union_typed_properties_integer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<data>10000</data>
<value_typed_union>false</value_typed_union>
</result>
Loading