Skip to content

Commit

Permalink
deprecate(serializer): `\LastDragon_ru\LaraASP\Serializer\Casts\AsSer…
Browse files Browse the repository at this point in the history
…ializable`, please use `\LastDragon_ru\LaraASP\Serializer\Casts\Serialized` attribute instead.
  • Loading branch information
LastDragon-ru committed May 3, 2024
1 parent 77490f6 commit dd539f9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 125 deletions.
78 changes: 0 additions & 78 deletions packages/serializer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,84 +176,6 @@ The `$user->getAttributes()` is:

[//]: # (end: 211a5dc77435fa2d79fcb5afce955fdd851bd0be575b7eae03d69d3fe5111b0a)

# Eloquent Cast[^1]

You can use the [`LastDragon_ru\LaraASP\Serializer\Casts\AsSerializable`](./src/Casts/AsSerializable.php) cast class to cast a model string attribute to an object:

[include:example]: ./docs/Examples/AsSerializable.php
[//]: # (start: 17152ed9d4094e5a2bb12c34f6fdb2f223a90f75c9ae440580dded1082fe6dec)
[//]: # (warning: Generated automatically. Do not edit.)

```php
<?php declare(strict_types = 1);

// phpcs:disable PSR1.Files.SideEffects
// phpcs:disable PSR1.Classes.ClassDeclaration

namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\AsSerializable;

use Illuminate\Database\Eloquent\Model;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\Serializer\Casts\AsSerializable;
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable;
use Override;

use function array_merge;

class UserSettings implements Serializable {
public function __construct(
public int $perPage,
public bool $showSidebar,
) {
// empty
}
}

/**
* @property UserSettings|null $settings
*/
class User extends Model {
/**
* @return array<array-key, mixed>
*/
#[Override]
protected function casts(): array {
return array_merge(parent::casts(), [
'settings' => AsSerializable::using(UserSettings::class),
]);
}
}

$user = new User();
$user->settings = new UserSettings(35, false);

Example::dump($user->settings);
Example::dump($user->getAttributes());
```

The `$user->settings` is:

```plain
LastDragon_ru\LaraASP\Serializer\Docs\Examples\AsSerializable\UserSettings {
+perPage: 35
+showSidebar: false
}
```

The `$user->getAttributes()` is:

```plain
[
"settings" => "{"perPage":35,"showSidebar":false}",
]
```

[//]: # (end: 17152ed9d4094e5a2bb12c34f6fdb2f223a90f75c9ae440580dded1082fe6dec)

[include:file]: ../../docs/Shared/Upgrading.md
[//]: # (start: e9139abedb89f69284102c9112b548fd7add07cf196259916ea4f1c98977223b)
[//]: # (warning: Generated automatically. Do not edit.)

# Upgrading

Please follow [Upgrade Guide](UPGRADE.md).
Expand Down
44 changes: 0 additions & 44 deletions packages/serializer/docs/Examples/AsSerializable.php

This file was deleted.

3 changes: 0 additions & 3 deletions packages/serializer/docs/Examples/AsSerializable.run

This file was deleted.

8 changes: 8 additions & 0 deletions packages/serializer/src/Casts/AsSerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
use Illuminate\Database\Eloquent\Model;
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializer;
use LastDragon_ru\LaraASP\Serializer\Exceptions\FailedToCast;
use LastDragon_ru\LaraASP\Serializer\Package;
use Override;

use function is_string;
use function trigger_deprecation;

// phpcs:disable PSR1.Files.SideEffects

trigger_deprecation(Package::Name, '%{VERSION}', 'Please use `%s` instead.', Serialized::class);

/**
* @deprecated %{VERSION} Please use {@see Serialized} instead.
*
* @template TGet of object
* @template TSet of object
*
Expand Down
1 change: 1 addition & 0 deletions packages/serializer/src/Casts/AsSerializableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @deprecated %{VERSION}
* @internal
*/
#[CoversClass(AsSerializable::class)]
Expand Down

0 comments on commit dd539f9

Please sign in to comment.