Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dark: add serializable to the handler params #6

Merged
merged 2 commits into from
Sep 14, 2023
Merged
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
8 changes: 5 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class User implements Serializable
The `serializes` method returns an array of the properties that you want to serialize. Then the package will automatically serialize and deserialize the data based on the type
hinted properties.

> Note: The package will only serialize public properties that are type hinted.
> **Note**
> - The package will only serialize public properties that are type hinted.
> - The package will serialize properties in the order they are defined in the `serialize` method.

### Serialization Handlers

Expand All @@ -72,7 +74,7 @@ class UserHandler implements SerializationHandler {
$this->userRepository = $userRepository;
}

public function serialize($value): string
public function serialize(Serializable $serializable, $value): string
{
return $value->getId();
}
Expand All @@ -82,7 +84,7 @@ class UserHandler implements SerializationHandler {
*
* @return ?User
*/
public function deserialize($value): ?User
public function deserialize(Serializable $serializable, $value): ?User
{
return $this->userRepository->find($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/SerializationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ interface SerializationHandler
*
* @return mixed
*/
public function serialize($value);
public function serialize(Serializable $serializable, $value);

/**
* @param mixed $value
*
* @return mixed
*/
public function deserialize($value);
public function deserialize(Serializable $serializable, $value);
}
10 changes: 6 additions & 4 deletions src/Laravel/ModelSerializationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace YouCanShop\Cereal\Laravel;

use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use YouCanShop\Cereal\Contracts\Serializable;
use YouCanShop\Cereal\Contracts\SerializationHandler;

final class ModelSerializationHandler implements SerializationHandler
Expand All @@ -13,19 +15,19 @@ final class ModelSerializationHandler implements SerializationHandler
/**
* @param Model $value
*
* @return mixed
* @return ModelIdentifier|mixed
*/
public function serialize($value)
public function serialize(Serializable $serializable, $value)
{
return $this->getSerializedPropertyValue($value);
}

/**
* @param $value
* @param ModelIdentifier|mixed $value
*
* @return mixed
*/
public function deserialize($value)
public function deserialize(Serializable $serializable, $value)
{
return $this->getRestoredPropertyValue($value);
}
Expand Down
1 change: 1 addition & 0 deletions src/SerializationHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function getInstance(): self
return self::$instance;
}


public function getHandler(string $type): SerializationHandler
{
if (!isset($this->handlers[$type])) {
Expand Down
10 changes: 8 additions & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ private function serialize(): void

$this->serializations[$propertyName] = $this->getSerializationHandlerFactory()
->getHandler($type->getName())
->serialize($this->serializable->$propertyName);
->serialize(
$this->serializable,
$this->serializable->$propertyName
);
}
}

Expand Down Expand Up @@ -98,7 +101,10 @@ public function deserialize(): void

$this->serializable->$propertyName = $this->getSerializationHandlerFactory()
->getHandler($type->getName())
->deserialize($serialized);
->deserialize(
$this->serializable,
$serialized
);
}
}
}
85 changes: 82 additions & 3 deletions tests/Unit/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tests;

use YouCanShop\Cereal\Cereal;
use YouCanShop\Cereal\Contracts\Serializable;
use YouCanShop\Cereal\Contracts\SerializationHandler;
use YouCanShop\Cereal\SerializationHandlerFactory;
use YouCanShop\Cereal\Cereal;

it('serializes scalar types', function () {
class User implements Serializable
Expand Down Expand Up @@ -77,21 +77,23 @@ public function __construct(array $lookupTable)
}

/**
* @param Serializable $serializable
* @param Something $value
*
* @return string
*/
public function serialize($value): string
public function serialize(Serializable $serializable, $value): string
{
return $value->getId();
}

/**
* @param Serializable $serializable
* @param string $value
*
* @return Something
*/
public function deserialize($value): Something
public function deserialize(Serializable $serializable, $value): Something
{
return $this->lookupTable[$value];
}
Expand Down Expand Up @@ -125,3 +127,80 @@ public function serializes(): array
expect($deserialized->thing->getId())
->toEqual($wrapper->thing->getId());
});

it('respects the serialization order', function () {
class Number
{
public int $value;

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

class SomeList implements Serializable
{
use Cereal;

public Number $first;
public Number $second;
public Number $third;

public function __construct(Number $first, Number $second, Number $third)
{
$this->first = $first;
$this->second = $second;
$this->third = $third;
}

public function serializes(): array
{
return ['first', 'second', 'third'];
}
}

class Handler implements SerializationHandler
{
/**
* @param Serializable $serializable
* @param Number $value
*
* @return mixed
*/
public function serialize(Serializable $serializable, $value)
{
return $value->value;
}

/**
* @param Serializable $serializable
* @param int $value
*
* @return \Tests\Number
*/
public function deserialize(Serializable $serializable, $value): Number
{
if ($value === 4) {
expect($serializable->first)
->toBeInstanceOf(Number::class)
->and($serializable->first->value)
->toEqual(2);
}

return new Number($value);
}
}

SerializationHandlerFactory::getInstance()
->addHandler(Number::class, new Handler);

$list = new SomeList(
new Number(2),
new Number(4),
new Number(8)
);

$serialized = serialize($list);
unserialize($serialized);
});