barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.17k stars 1.16k forks source link

Allow casts without property #1267

Closed Sparclex closed 2 years ago

Sparclex commented 2 years ago

Summary

Laravel allows to create customCasts for property that do not exist in the database table. This allows to create magic properties as ValueObject which combine multiple attributes together. Currently the ide helper does not provide any docblock for these kind of casts. This PR allows casts without existing properties to be defined as properties.

// model
class CustomCast extends Model
{
    protected $casts = [
        'price' => MoneyCast::class,
    ];
}
// cast
class MoneyCast implements CastsAttributes
{

    public function get($model, string $key, $value, array $attributes): Money
    {
        return new Money($attributes['amount'], $attributes['currency']);
    }

    public function set($model, string $key, $value, array $attributes)
    {
      return  [
          $value->amount,
          $value->currency,
      ]
    }
}

Type of change

Checklist