Skip to content

Commit

Permalink
code formatting and missed file.
Browse files Browse the repository at this point in the history
  • Loading branch information
idbentley committed Jul 3, 2024
1 parent 8869cbe commit ef5413c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Handler/UnionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
final class UnionHandler implements SubscribingHandlerInterface
{
static $aliases = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'];

/**
* {@inheritdoc}
*/
Expand All @@ -37,6 +38,7 @@ public static function getSubscribingMethods()
'method' => 'serializeUnion',
];
}

return $methods;
}

Expand Down Expand Up @@ -84,41 +86,49 @@ private function matchSimpleType(mixed $data, array $type, Context $context)
* these types will be returned first, in the order in which they were declared.
* - static and all built-in types (iterable replaced by array) will come next. They will always be returned in this order:
* static, callable, array, string, int, float, bool (or false or true), null.
*
*
* For determining types of primitives, it is necessary to reorder primitives so that they are tested from lowest specificity to highest:
* i.e. null, true, false, int, float, bool, string
*/
private function reorderTypes(array $type): array
{
if ($type['params']) {
uasort($type['params'], function($a, $b) {
uasort($type['params'], static function ($a, $b) {
$order = ['null' => 0, 'true' => 1, 'false' => 2, 'bool' => 3, 'int' => 4, 'float' => 5, 'string' => 6];

return (array_key_exists($a['name'], $order) ? $order[$a['name']] : 7) <=> (array_key_exists($b['name'], $order) ? $order[$b['name']] : 7);
});
}

return $type;
}

private function determineType(mixed $data, array $type, string $format): string {
private function determineType(mixed $data, array $type, string $format): string
{
foreach ($this->reorderTypes($type)['params'] as $possibleType) {
if ($this->testPrimitive($data, $possibleType['name'], $format)) {
return $possibleType['name'];
}
}
}
private function testPrimitive(mixed $data, string $type, string $format): bool {
switch($type) {

private function testPrimitive(mixed $data, string $type, string $format): bool
{
switch ($type) {
case 'integer':
case 'int':
return (string)(int)$data === (string)$data;
return (string) (int) $data === (string) $data;

case 'double':
case 'float':
return (string)(float)$data === (string)$data;
return (string) (float) $data === (string) $data;

case 'bool':
case 'boolean':
return (string)(bool)$data === (string)$data;
return (string) (bool) $data === (string) $data;

case 'string':
return (string)$data === (string)$data;
return (string) $data === (string) $data;
}
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/DocBlockType/UnionTypedDocBlockProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType;

class UnionTypedDocBlockProperty
{
/**
* @var int|bool|float|string
*/
private $data;

public function __construct($data)
{
$this->data = $data;
}
}

0 comments on commit ef5413c

Please sign in to comment.