Skip to content

Commit

Permalink
Фикс сериализации enum'ов
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaoda committed May 13, 2020
1 parent 761f717 commit ff7c30d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/Core/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Appwilio\RussianPostSDK\Core;

abstract class Enum
abstract class Enum implements \JsonSerializable
{
protected static $cache = [];

Expand Down Expand Up @@ -54,6 +54,11 @@ public static function __callStatic($name, $arguments)
throw new \BadMethodCallException("No static method or enum constant '$name' in class ".static::class);
}

public function jsonSerialize()
{
return $this->getValue();
}

final public function equals(Enum $other): bool
{
return $this === $other || (\get_class($other) === static::class && $this->value === $other->value);
Expand All @@ -64,11 +69,6 @@ final public function getValue()
return $this->value;
}

final public function __toString(): string
{
return $this->getValue();
}

final private function __clone()
{
throw new \LogicException('Enums are not cloneable');
Expand Down
4 changes: 2 additions & 2 deletions src/Dispatching/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ private function buildHttpRequest(string $method, string $path, ?Arrayable $payl
private function serializeRequestData(array $data): array
{
return \array_map(function ($value) {
if (\is_object($value) && \method_exists($value, '__toString')) {
return (string) $value;
if (\is_object($value) && $value instanceof \JsonSerializable) {
return $value->jsonSerialize();
}

if (\is_array($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function test_can_get_data(): void
$this->assertEquals($lastName, $instance->getLastName());
$this->assertEquals($middleName, $instance->getMiddleName());
$this->assertEquals($original, $instance->getOriginalFio());
$this->assertEquals($quality, $instance->getQualityCode());
$this->assertEquals(new FioQuality($quality), $instance->getQualityCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function test_can_get_data(): void
$this->assertEquals($extension, $instance->getExtension());
$this->assertEquals($cityCode, $instance->getCityCode());
$this->assertEquals($original, $instance->getOriginalPhone());
$this->assertEquals($quality, $instance->getQualityCode());
$this->assertEquals(new PhoneQuality($quality), $instance->getQualityCode());
$this->assertEquals(true, $instance->isUseful());
}
}

0 comments on commit ff7c30d

Please sign in to comment.