Skip to content

Commit

Permalink
Fix Response objects returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-b committed Jan 24, 2023
1 parent 509e050 commit 4b1c516
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Response/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public readonly bool $value,
) {}

public function value(): bool
public function payload(): bool
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
/**
* @return array<int,mixed>
*/
public function value(): array
public function payload(): array
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Delegated.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public readonly object $value,
) {}

public function value(): object
public function payload(): object
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public readonly int $value,
) {}

public function value(): int
public function payload(): int
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
public readonly array $value,
) {}

public function value(): array
public function payload(): array
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class None extends Response
/**
* @return null
*/
public function value(): mixed
public function payload(): mixed
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public readonly float $value,
) {}

public function value(): float
public function payload(): float
{
return $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public readonly string $value,
) {}

public function value(): string
public function payload(): string
{
return $this->value;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Response/TypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public function map(mixed $response): Response
return match (true) {
$response instanceof Response => $response,
is_null($response) => new None(),
is_int($response) => new Integer(value: $response),
is_float($response) => new Numeric(value: $response),
is_bool($response) => new Boolean(value: $response),
is_string($response) => new Text(value: $response),
is_array($response) && !empty($response) && array_is_list($response) => new Collection(value: $response),
is_array($response) && !empty($response) && !array_is_list($response) => new Map(value: $response),
is_array($response) && empty($response) => new Collection(value: $response),
default => new Delegated(value: $response),
is_int($response) => new Integer($response),
is_float($response) => new Numeric($response),
is_bool($response) => new Boolean($response),
is_string($response) => new Text($response),
is_array($response) && !empty($response) && array_is_list($response) => new Collection($response),
is_array($response) && !empty($response) && !array_is_list($response) => new Map($response),
is_array($response) && empty($response) => new Collection($response),
default => new Delegated($response),
};
}
}
1 change: 1 addition & 0 deletions tests/Unit/TypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function setUp(): void
public function test_it_can_map_type_to_response(mixed $data, Response $response): void
{
$this->assertEquals($this->typeMapper->map($data), $response);
$this->assertSame($data, $response->payload());
}

public function providesDataTypes(): iterable
Expand Down

0 comments on commit 4b1c516

Please sign in to comment.