Skip to content

Is it possible to force casting to be applied #352

Answered by rubenvanassche
splatEric asked this question in Q&A
Discussion options

You must be logged in to vote

Could you try this again with the newest version of the package?

I cannot reproduce it:

it('cacas', function () {
    class CarbonImmutableCaster implements Cast
    {
        public function cast(DataProperty $property, mixed $value, array $context): mixed
        {
            if ($value instanceof CarbonImmutable) {
                return $value;
            }

            if ($value instanceof DateTime) {
                return CarbonImmutable::createFromMutable($value);
            }

            if (is_string($value)) {
                return CarbonImmutable::parse($value);
            }

            return new Uncastable();
        }
    }

    $dataClass = new class extends Data {…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@splatEric
Comment options

@splatEric
Comment options

Answer selected by splatEric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants