Skip to content

Commit 0bcc79f

Browse files
authored
prefix casts with "As" (#10343)
while this is definitely a subjective change, I think it provides some significant benefits: - consistent naming pattern with Laravel internal casts - prefix helps indicate a cast object, and avoid confusion with names of value objects - avoids having to alias imports. value objects get the noun, and casts get the prefix. generally speaking, I think these changes improve readability and consistency.
1 parent 1615524 commit 0bcc79f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

eloquent-mutators.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ $users = User::select([
669669
Laravel has a variety of built-in, helpful cast types; however, you may occasionally need to define your own cast types. To create a cast, execute the `make:cast` Artisan command. The new cast class will be placed in your `app/Casts` directory:
670670

671671
```shell
672-
php artisan make:cast Json
672+
php artisan make:cast AsJson
673673
```
674674

675675
All custom cast classes implement the `CastsAttributes` interface. Classes that implement this interface must define a `get` and `set` method. The `get` method is responsible for transforming a raw value from the database into a cast value, while the `set` method should transform a cast value into a raw value that can be stored in the database. As an example, we will re-implement the built-in `json` cast type as a custom cast type:
@@ -682,7 +682,7 @@ namespace App\Casts;
682682
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
683683
use Illuminate\Database\Eloquent\Model;
684684

685-
class Json implements CastsAttributes
685+
class AsJson implements CastsAttributes
686686
{
687687
/**
688688
* Cast the given value.
@@ -714,7 +714,7 @@ Once you have defined a custom cast type, you may attach it to a model attribute
714714

715715
namespace App\Models;
716716

717-
use App\Casts\Json;
717+
use App\Casts\AsJson;
718718
use Illuminate\Database\Eloquent\Model;
719719

720720
class User extends Model
@@ -727,7 +727,7 @@ class User extends Model
727727
protected function casts(): array
728728
{
729729
return [
730-
'options' => Json::class,
730+
'options' => AsJson::class,
731731
];
732732
}
733733
}
@@ -738,28 +738,28 @@ class User extends Model
738738

739739
You are not limited to casting values to primitive types. You may also cast values to objects. Defining custom casts that cast values to objects is very similar to casting to primitive types; however, if your value object encompasses more than one database column, the `set` method must return an array of key / value pairs that will be used to set raw, storable values on the model. If your value object only affects a single column, you should simply return the storable value.
740740

741-
As an example, we will define a custom cast class that casts multiple model values into a single `Address` value object. We will assume the `Address` value has two public properties: `lineOne` and `lineTwo`:
741+
As an example, we will define a custom cast class that casts multiple model values into a single `Address` value object. We will assume the `Address` value object has two public properties: `lineOne` and `lineTwo`:
742742

743743
```php
744744
<?php
745745

746746
namespace App\Casts;
747747

748-
use App\ValueObjects\Address as AddressValueObject;
748+
use App\ValueObjects\Address;
749749
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
750750
use Illuminate\Database\Eloquent\Model;
751751
use InvalidArgumentException;
752752

753-
class Address implements CastsAttributes
753+
class AsAddress implements CastsAttributes
754754
{
755755
/**
756756
* Cast the given value.
757757
*
758758
* @param array<string, mixed> $attributes
759759
*/
760-
public function get(Model $model, string $key, mixed $value, array $attributes): AddressValueObject
760+
public function get(Model $model, string $key, mixed $value, array $attributes): Address
761761
{
762-
return new AddressValueObject(
762+
return new Address(
763763
$attributes['address_line_one'],
764764
$attributes['address_line_two']
765765
);
@@ -773,7 +773,7 @@ class Address implements CastsAttributes
773773
*/
774774
public function set(Model $model, string $key, mixed $value, array $attributes): array
775775
{
776-
if (! $value instanceof AddressValueObject) {
776+
if (! $value instanceof Address) {
777777
throw new InvalidArgumentException('The given value is not an Address instance.');
778778
}
779779

@@ -808,7 +808,7 @@ When attributes that are cast to value objects are resolved, they are cached by
808808
If you would like to disable the object caching behavior of custom cast classes, you may declare a public `withoutObjectCaching` property on your custom cast class:
809809

810810
```php
811-
class Address implements CastsAttributes
811+
class AsAddress implements CastsAttributes
812812
{
813813
public bool $withoutObjectCaching = true;
814814

@@ -843,7 +843,7 @@ Occasionally, you may need to write a custom cast class that only transforms val
843843
Inbound only custom casts should implement the `CastsInboundAttributes` interface, which only requires a `set` method to be defined. The `make:cast` Artisan command may be invoked with the `--inbound` option to generate an inbound only cast class:
844844

845845
```shell
846-
php artisan make:cast Hash --inbound
846+
php artisan make:cast AsHash --inbound
847847
```
848848

849849
A classic example of an inbound only cast is a "hashing" cast. For example, we may define a cast that hashes inbound values via a given algorithm:
@@ -856,7 +856,7 @@ namespace App\Casts;
856856
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
857857
use Illuminate\Database\Eloquent\Model;
858858

859-
class Hash implements CastsInboundAttributes
859+
class AsHash implements CastsInboundAttributes
860860
{
861861
/**
862862
* Create a new cast class instance.
@@ -893,7 +893,7 @@ When attaching a custom cast to a model, cast parameters may be specified by sep
893893
protected function casts(): array
894894
{
895895
return [
896-
'secret' => Hash::class.':sha256',
896+
'secret' => AsHash::class.':sha256',
897897
];
898898
}
899899
```
@@ -922,7 +922,7 @@ Objects that implement the `Castable` interface must define a `castUsing` method
922922
namespace App\ValueObjects;
923923

924924
use Illuminate\Contracts\Database\Eloquent\Castable;
925-
use App\Casts\Address as AddressCast;
925+
use App\Casts\AsAddress;
926926

927927
class Address implements Castable
928928
{
@@ -933,7 +933,7 @@ class Address implements Castable
933933
*/
934934
public static function castUsing(array $arguments): string
935935
{
936-
return AddressCast::class;
936+
return AsAddress::class;
937937
}
938938
}
939939
```

0 commit comments

Comments
 (0)