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

Drop laravel data v3 #36

Merged
merged 5 commits into from
Mar 15, 2024
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
6 changes: 1 addition & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ jobs:
php: [ 8.1, 8.2 , 8.3 ]
laravel: [ ^10.0, ^11.0 ]
stability: [ prefer-stable, prefer-lowest ]
laravelData: [ ^3.0, ^4.0 ]
exclude:
- php: 8.1
laravel: ^11.0
- laravel: ^11.0
laravelData: ^3.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - D${{ matrix.laravelData }} - ${{ matrix.stability }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}

steps:
- name: Checkout code
Expand All @@ -44,7 +41,6 @@ jobs:
- name: Install dependencies
run: |
composer require "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update
composer require "spatie/laravel-data:${{ matrix.laravelData }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Execute tests
Expand Down
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,35 @@ This package adds some laravel specific options for serialization/deserializatio
- Eloquent models can be correctly serialized/deserialized (with relations) adding `TemporalSerializable` interface and `TemporalEloquentSerialize` trait.
- [spatie/laravel-data](https://github.com/spatie/laravel-data) data objects are supported out of the box.

> To improve laravel-data support, this package provides `TemporalSerializableCastAndTransformer` (which implements cast and transformer for laravel-data).
> to add support for serialization/deserialization of `TemporalSerializable` objects used as `Data` properties.
> You can add them to `data.casts` and `data.transformers` config to add support globally,
> or use it with `WithCast`, `WithTransformer` attributes to add support to specific data objects (in v4 the attributes can be combined with `WithCastAndTransform`).
#### Spatie/Laravel-Data support

`spatie/laravel-data` is a package that provides a simple way to work with data objects in Laravel.
In order to take full advantage of `laravel-data`, it is suggested to use `v4.3.0` or higher.

> [!NOTE]
> The provided `TemporalSerializableCastAndTransformer` is compatible only with `laravel-data` `v4.3` or higher,
> if you are using an older version you can create your cast/transform.

Changes to be made in `config/data.php`:

```php
// Enable iterables cast/transform
'features' => [
'cast_and_transform_iterables' => true,
],

// Add support for TemporalSerializable transform
'transformers' => [
//...
\Keepsuit\LaravelTemporal\Contracts\TemporalSerializable::class => \Keepsuit\LaravelTemporal\Integrations\LaravelData\TemporalSerializableCastAndTransformer::class,
],

// Add support for TemporalSerializable cast
'casts' => [
//...
\Keepsuit\LaravelTemporal\Contracts\TemporalSerializable::class => \Keepsuit\LaravelTemporal\Integrations\LaravelData\TemporalSerializableCastAndTransformer::class,
],
```

### Interceptors

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"phpstan/phpstan-phpunit": "^1.0",
"rector/rector": "^1.0",
"spatie/invade": "^2.0",
"spatie/laravel-data": "^4.0",
"spatie/laravel-data": "^4.3",
"spatie/laravel-ray": "^1.26",
"thecodingmachine/phpstan-safe-rule": "^1.2"
},
"suggest": {
"spatie/laravel-data": "Can be used for workflows payloads (^3.0 || ^4.0)"
"spatie/laravel-data": "Can be used for workflows payloads (recommended ^4.3)"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 0 additions & 22 deletions src/Integrations/LaravelData/LaravelDataHelpers.php

This file was deleted.

84 changes: 0 additions & 84 deletions src/Integrations/LaravelData/LaravelDataTemporalSerializer.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,51 @@

namespace Keepsuit\LaravelTemporal\Integrations\LaravelData;

use Illuminate\Container\Container;
use Keepsuit\LaravelTemporal\Contracts\TemporalSerializable;
use Keepsuit\LaravelTemporal\Exceptions\TemporalSerializerException;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Casts\IterableItemCast;
use Spatie\LaravelData\Casts\Uncastable;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Support\Transformation\TransformationContext;
use Spatie\LaravelData\Transformers\Transformer;

if (LaravelDataHelpers::version() === 4) {
class TemporalSerializableCastAndTransformer implements Cast, Transformer
class TemporalSerializableCastAndTransformer implements Cast, IterableItemCast, Transformer
{
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): TemporalSerializable|Uncastable
{
public function __construct(
protected ?string $type = null
) {
}
return $this->castValue($property->type->type->findAcceptedTypeForBaseType(TemporalSerializable::class), $value);
}

public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed
{
$serializer = Container::getInstance()->make(LaravelDataTemporalSerializer::class);
assert($serializer instanceof LaravelDataTemporalSerializer);
public function castIterableItem(DataProperty $property, mixed $value, array $properties, CreationContext $context): TemporalSerializable|Uncastable
{
return $this->castValue($property->type->iterableItemType, $value);
}

return $serializer->cast($property, $value, $this->type);
public function transform(DataProperty $property, mixed $value, TransformationContext $context): mixed
{
if ($value instanceof TemporalSerializable) {
return $value->toTemporalPayload();
}

public function transform(DataProperty $property, mixed $value, TransformationContext $context): mixed
{
$serializer = Container::getInstance()->make(LaravelDataTemporalSerializer::class);

return $serializer->transform($value);
}
return $value;
}
} else {
class TemporalSerializableCastAndTransformer implements Cast, Transformer

/**
* @throws TemporalSerializerException
*/
protected function castValue(string $className, mixed $value): TemporalSerializable|Uncastable
{
public function __construct(
protected ?string $type = null
) {
if (! class_exists($className)) {
return Uncastable::create();
}

public function cast(DataProperty $property, mixed $value, array $context): mixed
{
$serializer = Container::getInstance()->make(LaravelDataTemporalSerializer::class);
assert($serializer instanceof LaravelDataTemporalSerializer);

return $serializer->cast($property, $value, $this->type);
if (! is_array($value)) {
return Uncastable::create();
}

public function transform(DataProperty $property, mixed $value): mixed
{
$serializer = Container::getInstance()->make(LaravelDataTemporalSerializer::class);
assert($serializer instanceof LaravelDataTemporalSerializer);

return $serializer->transform($value);
}
/** @var class-string<TemporalSerializable> $className */
return $className::fromTemporalPayload($value);
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/Converter/AdvancedDataItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\Converter;

use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;

class AdvancedDataItem extends Data
{
public function __construct(
public TemporalSerializableItem $item,
/**
* @var Collection<array-key,TemporalSerializableItem>|null
*/
public ?Collection $collection = null,
) {
}
}
24 changes: 0 additions & 24 deletions tests/Fixtures/Converter/AdvancedDataItemV3.php

This file was deleted.

22 changes: 0 additions & 22 deletions tests/Fixtures/Converter/AdvancedDataItemV4.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;

class DataItemV4 extends Data
class DataItem extends Data
{
public function __construct(
public int $id,
public ?array $values = null,
/**
* @var Collection<array-key,DataItemV4>|null
* @var Collection<array-key,DataItem>|null
*/
public ?Collection $collection = null,
) {
Expand Down
19 changes: 0 additions & 19 deletions tests/Fixtures/Converter/DataItemV3.php

This file was deleted.

Loading