Skip to content

Commit

Permalink
Support null values across all property types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Aug 20, 2023
1 parent 030ba36 commit 2c4b0da
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/Formatter/ArrayBasedFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function rootField(Serializer|Deserializer $serializer, string $type): Fi
/**
* @param array<string, mixed> $runningValue
* @param Field $field
* @param int $next
* @param int|null $next
* @return array<string, mixed>
*/
public function serializeInt(mixed $runningValue, Field $field, int $next): array
public function serializeInt(mixed $runningValue, Field $field, ?int $next): array
{
$runningValue[$field->serializedName] = $next;
return $runningValue;
Expand All @@ -40,10 +40,10 @@ public function serializeInt(mixed $runningValue, Field $field, int $next): arra
/**
* @param array<string, mixed> $runningValue
* @param Field $field
* @param float $next
* @param float|null $next
* @return array<string, mixed>
*/
public function serializeFloat(mixed $runningValue, Field $field, float $next): array
public function serializeFloat(mixed $runningValue, Field $field, ?float $next): array
{
$runningValue[$field->serializedName] = $next;
return $runningValue;
Expand All @@ -64,10 +64,10 @@ public function serializeString(mixed $runningValue, Field $field, ?string $next
/**
* @param array<string, mixed> $runningValue
* @param Field $field
* @param bool $next
* @param bool|null $next
* @return array<string, mixed>
*/
public function serializeBool(mixed $runningValue, Field $field, bool $next): array
public function serializeBool(mixed $runningValue, Field $field, ?bool $next): array
{
$runningValue[$field->serializedName] = $next;
return $runningValue;
Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public function serializeInitialize(ClassSettings $classDef, Field $rootField):

public function serializeFinalize(mixed $runningValue, ClassSettings $classDef): mixed;

public function serializeInt(mixed $runningValue, Field $field, int $next): mixed;
public function serializeInt(mixed $runningValue, Field $field, ?int $next): mixed;

public function serializeFloat(mixed $runningValue, Field $field, float $next): mixed;
public function serializeFloat(mixed $runningValue, Field $field, ?float $next): mixed;

public function serializeString(mixed $runningValue, Field $field, ?string $next): mixed;

public function serializeBool(mixed $runningValue, Field $field, bool $next): mixed;
public function serializeBool(mixed $runningValue, Field $field, ?bool $next): mixed;

public function serializeSequence(mixed $runningValue, Field $field, Sequence $next, Serializer $serializer): mixed;

Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/StreamFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function serializeFinalize(mixed $runningValue, ClassSettings $classDef):
return $runningValue;
}

public function serializeInt(mixed $runningValue, Field $field, int $next): mixed
public function serializeInt(mixed $runningValue, Field $field, ?int $next): mixed
{
$runningValue->write((string)$next);
return $runningValue;
}

public function serializeFloat(mixed $runningValue, Field $field, float $next): mixed
public function serializeFloat(mixed $runningValue, Field $field, ?float $next): mixed
{
$runningValue->write((string)$next);
return $runningValue;
Expand All @@ -44,7 +44,7 @@ public function serializeString(mixed $runningValue, Field $field, ?string $next
return $runningValue;
}

public function serializeBool(mixed $runningValue, Field $field, bool $next): mixed
public function serializeBool(mixed $runningValue, Field $field, ?bool $next): mixed
{
$runningValue->write($next ? 'true' : 'false');
return $runningValue;
Expand Down
6 changes: 5 additions & 1 deletion tests/ArrayBasedFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ public function null_stuff_validate(mixed $serialized): void
{
$toTest = $this->arrayify($serialized);

self::assertNull($toTest['examples']);
self::assertNull($toTest['int']);
self::assertNull($toTest['float']);
self::assertNull($toTest['string']);
self::assertNull($toTest['array']);
self::assertNull($toTest['object']);
}
}
10 changes: 0 additions & 10 deletions tests/Records/NullProp.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Records/NullProps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Crell\Serde\Records;

class NullProps
{
public function __construct(
public ?int $int = null,
public ?float $float = null,
public ?string $string = null,
public ?array $array = null,
public ?NullProps $object = null,
) {}
}
14 changes: 9 additions & 5 deletions tests/SerdeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
use Crell\Serde\Records\NestedFlattenObject;
use Crell\Serde\Records\NestedObject;
use Crell\Serde\Records\NullArrays;
use Crell\Serde\Records\NullProp;
use Crell\Serde\Records\NullProps;
use Crell\Serde\Records\OptionalPoint;
use Crell\Serde\Records\Pagination\DetailedResults;
use Crell\Serde\Records\Pagination\NestedPagination;
Expand Down Expand Up @@ -1558,20 +1558,24 @@ public function scopes_examples(): iterable
/**
* @test
*/
public function null_stuff(): void
public function nullable_null_properties_are_allowed(): void
{
$s = new SerdeCommon(formatters: $this->formatters);

$data = new NullProp(null);
$data = new NullProps();

$serialized = $s->serialize($data, $this->format);

$this->null_stuff_validate($serialized);

/** @var NullProp $result */
/** @var NullProps $result */
$result = $s->deserialize($serialized, from: $this->format, to: $data::class);

self::assertNull($result->examples);
self::assertNull($result->int);
self::assertNull($result->float);
self::assertNull($result->string);
self::assertNull($result->array);
self::assertNull($result->object);

self::assertEquals($data, $result);
}
Expand Down

0 comments on commit 2c4b0da

Please sign in to comment.