Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/2-features/01-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ final readonly class AddressSerializer implements Serializer

:::

Of course, Tempest provides casters and serializers for the most common data types, including arrays, booleans, dates, enumerations, integers and value objects.
Of course, Tempest provides casters and serializers for the most common data types, including arrays, booleans, dates, enumerations, floats, integers and value objects.

### Registering casters and serializers globally

Expand Down
4 changes: 2 additions & 2 deletions packages/mapper/src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface Serializer
{
/**
* Serializes the given input into a string, array, or integer.
* Serializes the given input into an array, boolean, float, integer, or string.
*/
public function serialize(mixed $input): array|string|int;
public function serialize(mixed $input): array|bool|float|int|string;
}
4 changes: 2 additions & 2 deletions packages/mapper/src/Serializers/BooleanSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static function accepts(PropertyReflector|TypeReflector $input): bool
return in_array($type->getName(), ['bool', 'boolean'], strict: true);
}

public function serialize(mixed $input): string
public function serialize(mixed $input): bool
{
if (! is_bool($input)) {
throw new ValueCouldNotBeSerialized('boolean');
}

return $input ? 'true' : 'false';
return $input;
}
}
4 changes: 2 additions & 2 deletions packages/mapper/src/Serializers/FloatSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static function accepts(PropertyReflector|TypeReflector $input): bool
return in_array($type->getName(), ['double', 'float'], strict: true);
}

public function serialize(mixed $input): string
public function serialize(mixed $input): float
{
if (! is_float($input)) {
throw new ValueCouldNotBeSerialized('float');
}

return (string) $input;
return $input;
}
}
4 changes: 2 additions & 2 deletions packages/mapper/src/Serializers/IntegerSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static function accepts(PropertyReflector|TypeReflector $input): bool
return in_array($type->getName(), ['int', 'integer'], strict: true);
}

public function serialize(mixed $input): string
public function serialize(mixed $input): int
{
if (! is_int($input)) {
throw new ValueCouldNotBeSerialized('integer');
}

return (string) $input;
return $input;
}
}
17 changes: 17 additions & 0 deletions tests/Integration/Mapper/Fixtures/ObjectWithScalarValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

use Tempest\Mapper\Strict;

#[Strict]
final class ObjectWithScalarValues
{
public function __construct(
public bool $active,
public float $score,
public int $count,
) {}
}
22 changes: 21 additions & 1 deletion tests/Integration/Mapper/Mappers/ObjectToArrayMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithJsonSerialize;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithNullableProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithScalarValues;

use function Tempest\Mapper\map;

Expand Down Expand Up @@ -43,10 +44,29 @@ public function object_with_nullable_properties_to_array(): void
$this->assertSame(
[
'a' => 'a',
'b' => '3.1416',
'b' => 3.1416,
'c' => null,
],
$array,
);
}

#[Test]
public function object_with_scalar_values_to_array(): void
{
$array = map(new ObjectWithScalarValues(
active: true,
score: 1.5,
count: 3,
))->toArray();

$this->assertSame(
[
'active' => true,
'score' => 1.5,
'count' => 3,
],
$array,
);
}
}
20 changes: 20 additions & 0 deletions tests/Integration/Mapper/Mappers/ObjectToJsonMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\Attributes\Test;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithScalarValues;

use function Tempest\Mapper\map;

Expand All @@ -22,4 +23,23 @@ public function object_to_json(): void

$this->assertSame('{"a":"a","b":"b"}', $json);
}

#[Test]
public function object_with_scalar_values_to_json(): void
{
$json = map(new ObjectWithScalarValues(
active: true,
score: 1.5,
count: 3,
))->toJson();

$this->assertSame(
[
'active' => true,
'score' => 1.5,
'count' => 3,
],
json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public function serialize(): void
[
'stringProp' => 'a',
'stringableProp' => 'a',
'intProp' => '1',
'intProp' => 1,
'nullableIntProp' => null,
'floatProp' => '0.1',
'floatProp' => 0.1,
'nullableFloatProp' => null,
'boolProp' => 'true',
'boolProp' => true,
'nullableBoolProp' => null,
'arrayProp' => '["a"]',
'serializeWithProp' => 'aa',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ final class BooleanSerializerTest extends TestCase
public function serialize(): void
{
$this->assertSame(
'true',
true,
new BooleanSerializer()->serialize(true),
);

$this->assertSame(
'false',
false,
new BooleanSerializer()->serialize(false),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class FloatSerializerTest extends TestCase
public function serialize(): void
{
$this->assertSame(
'0.1',
0.1,
new FloatSerializer()->serialize(0.1),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class IntegerSerializerTest extends TestCase
public function serialize(): void
{
$this->assertSame(
'1',
1,
new IntegerSerializer()->serialize(1),
);
}
Expand Down
Loading