Skip to content

Commit

Permalink
Add marshaller instead of sf serializer (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Jul 1, 2023
1 parent 99602db commit 6770e6e
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 27 deletions.
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"spiral/encrypter": "^3.7",
"spiral/scaffolder": "^3.7",
"symfony/property-access": "^5.4.22 || ^6.0",
"spiral-packages/symfony-serializer": "^2.0"
"spiral/marshaller-bridge": "^1.0"
},
"require-dev": {
"spiral/twig-bridge": "^2.0.1",
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^10.1",
"friendsofphp/php-cs-fixer": "^3.8",
"phpunit/phpunit": "^10.2",
"friendsofphp/php-cs-fixer": "^3.19",
"spiral/testing": "^2.3",
"vimeo/psalm": "^5.9",
"vimeo/psalm": "^5.13",
"spiral/validator": "^1.3",
"spiral/translator": "^3.7",
"spiral/nyholm-bridge": "^1.3",
Expand Down Expand Up @@ -66,6 +66,16 @@
]
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/spiral/marshaller-bridge"
},
{
"type": "vcs",
"url": "https://github.com/spiral/marshaller"
}
],
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 2 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
backupGlobals="false"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="false"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
backupStaticProperties="false">
displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="Livewire Integration Bridge">
<directory>tests</directory>
Expand Down
4 changes: 2 additions & 2 deletions src/Bootloader/LivewireBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Spiral\Bootloader\Security\EncrypterBootloader;
use Spiral\Livewire\Controller\LivewireController;
use Spiral\Livewire\WireTrait;
use Spiral\MarshallerBridge\Bootloader\MarshallerBootloader;
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
use Spiral\Serializer\Symfony\Bootloader\SerializerBootloader;
use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader;
use Spiral\Views\Bootloader\ViewsBootloader;

Expand All @@ -25,7 +25,7 @@ final class LivewireBootloader extends Bootloader
ComponentBootloader::class,
ComponentMiddlewareBootloader::class,
ListenerBootloader::class,
SerializerBootloader::class,
MarshallerBootloader::class,
];

public function init(ViewsBootloader $views, DirectoriesInterface $dirs): void
Expand Down
20 changes: 8 additions & 12 deletions src/Component/DataAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
use Adbar\Dot;
use Spiral\Attributes\ReaderInterface;
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Exception\Component\ModelNotWritableException;
use Spiral\Marshaller\MarshallerInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Serializer;

final class DataAccessor implements DataAccessorInterface
{
public function __construct(
private readonly Serializer $serializer,
private readonly MarshallerInterface $marshaller,
private readonly ReaderInterface $reader
) {
}
Expand All @@ -30,26 +28,24 @@ public function getData(LivewireComponent $component): array
}

/** @var array $data */
$data = $this->serializer->normalize(data: $component, context: [AbstractNormalizer::ATTRIBUTES => $models]);
$data = \array_filter(
$this->marshaller->marshal($component),
static fn (string $key) => \in_array($key, $models, true),
\ARRAY_FILTER_USE_KEY
);

return $data;
}

/**
* @param non-empty-string $propertyPath
*
* @throws ModelNotWritableException
*/
public function setValue(LivewireComponent $component, string $propertyPath, mixed $value): void
{
$data = new Dot($this->getData($component));
$data->set($propertyPath, $value);

$this->serializer->denormalize(
data: $data->all(),
type: $component::class,
context: [AbstractNormalizer::OBJECT_TO_POPULATE => $component]
);
$this->marshaller->unmarshal($data->all(), $component);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/app/Component/DataAccessorTest/Component/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Livewire\Tests\App\Component\DataAccessorTest\DataTransferObject\Address;
use Spiral\Livewire\Tests\App\Component\DataAccessorTest\DataTransferObject\Order;
use Spiral\Marshaller\Meta\MarshalArray;

#[Component(name: 'component-data-accessor-test-user')]
final class User extends LivewireComponent
Expand All @@ -28,6 +30,7 @@ final class User extends LivewireComponent
public array $phoneNumbers = [];

#[Model]
#[MarshalArray(of: Order::class)]
public array $orders = [];

public string $publicNonModelProperty = 'foo';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Spiral\Livewire\Tests\App\Component\DataAccessorTest\DataTransferObject;

use Spiral\Marshaller\Meta\MarshalArray;

final class Order
{
public int $id;

#[MarshalArray(of: Item::class)]
public array $items;

public static function create(int $id, array $items): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Marshaller\Meta\Marshal;

final class InvalidPrivateComponent extends LivewireComponent
{
#[Marshal]
#[Model]
private array $data = [
3 => 'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Livewire\Tests\App\Middleware\Component\NormalizeComponentPropertiesForJavaScriptTest\Entity\User;
use Spiral\Marshaller\Meta\Marshal;

final class InvalidPrivateWithObjectComponent extends LivewireComponent
{
#[Marshal]
#[Model]
private array $data = [
3 => 'foo',
0 => 'bar',
4 => 'baz'
];

#[Marshal]
#[Model]
private User $user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Marshaller\Meta\Marshal;

final class InvalidProtectedComponent extends LivewireComponent
{
#[Marshal]
#[Model]
protected array $data = [
3 => 'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Livewire\Tests\App\Middleware\Component\NormalizeComponentPropertiesForJavaScriptTest\Entity\User;
use Spiral\Marshaller\Meta\Marshal;

final class InvalidProtectedWithObjectComponent extends LivewireComponent
{
#[Marshal]
#[Model]
protected array $data = [
3 => 'foo',
0 => 'bar',
4 => 'baz'
];

#[Marshal]
#[Model]
protected User $user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Marshaller\Meta\Marshal;

final class ValidPrivateComponent extends LivewireComponent
{
#[Marshal]
#[Model]
private array $data = [
'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Livewire\Tests\App\Middleware\Component\NormalizeComponentPropertiesForJavaScriptTest\Entity\User;
use Spiral\Marshaller\Meta\Marshal;

final class ValidPrivateWithObjectComponent extends LivewireComponent
{
#[Marshal]
#[Model]
private array $data = [
'foo',
'bar',
'baz'
];

#[Marshal]
#[Model]
private User $user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Marshaller\Meta\Marshal;

final class ValidProtectedComponent extends LivewireComponent
{
#[Marshal]
#[Model]
protected array $data = [
'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use Spiral\Livewire\Attribute\Model;
use Spiral\Livewire\Component\LivewireComponent;
use Spiral\Livewire\Tests\App\Middleware\Component\NormalizeComponentPropertiesForJavaScriptTest\Entity\User;
use Spiral\Marshaller\Meta\Marshal;

final class ValidProtectedWithObjectComponent extends LivewireComponent
{
#[Marshal]
#[Model]
protected array $data = [
'foo',
'bar',
'baz'
];

#[Marshal]
#[Model]
protected User $user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
final class User
{
public function __construct(
public readonly int $id,
public readonly string $name
public int $id,
public string $name
) {
}
}
7 changes: 7 additions & 0 deletions tests/src/Functional/Component/DataAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Spiral\Livewire\Tests\App\Component\DataAccessorTest\DataTransferObject\Item;
use Spiral\Livewire\Tests\App\Component\DataAccessorTest\DataTransferObject\Order;
use Spiral\Livewire\Tests\Functional\TestCase;
use Spiral\Marshaller\Meta\Marshal;
use Spiral\Marshaller\Meta\MarshalArray;

final class DataAccessorTest extends TestCase
{
Expand Down Expand Up @@ -133,6 +135,7 @@ public static function getValueDataProvider(): \Traversable
yield [
new class extends LivewireComponent
{
#[Marshal]
#[Model]
private string $name = 'foo';

Expand All @@ -156,6 +159,7 @@ public function getName(): string
yield [
new class extends LivewireComponent
{
#[Marshal]
#[Model]
private array $prices = [10, 100.0];

Expand Down Expand Up @@ -193,6 +197,7 @@ public function __construct() {
new class extends LivewireComponent
{
#[Model]
#[MarshalArray(of: Order::class)]
public array $orders = [];

public function __construct()
Expand Down Expand Up @@ -223,6 +228,7 @@ public static function setValueDataProvider(): \Traversable
new class extends LivewireComponent
{
#[Model]
#[Marshal]
private string $name;

public function setName(string $name): void
Expand Down Expand Up @@ -251,6 +257,7 @@ public function getName(): string
new class extends LivewireComponent
{
#[Model]
#[Marshal]
private array $prices;

public function setPrices(array $prices): void
Expand Down

0 comments on commit 6770e6e

Please sign in to comment.