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

Allow casts without property #1267

Merged
merged 2 commits into from
Oct 25, 2021
Merged

Allow casts without property #1267

merged 2 commits into from
Oct 25, 2021

Conversation

Sparclex
Copy link
Contributor

@Sparclex Sparclex commented Oct 14, 2021

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • Existing tests have been adapted and/or new tests have been added
  • Add a CHANGELOG.md entry
  • Code style has been fixed via composer fix-style

@Sparclex Sparclex marked this pull request as ready for review October 14, 2021 08:54
@barryvdh barryvdh merged commit a091b9c into barryvdh:master Oct 25, 2021
d3v2a pushed a commit to d3v2a/laravel-ide-helper that referenced this pull request Feb 16, 2024
* Allow casts without property

* Update changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants