Skip to content

Opt-in support for property hooks in IsDatabaseModel #2213

Description

@gglnx

Description

I've got a trait to generate a public ID (Stripe-style with prefix, e.g. ws_9mJrX3hPMFMRPx3X):

trait HasPublicId
{
    #[IsBindingValue]
    public PublicId $publicId {
        get {
            $this->publicId ??= PublicId::create(self::idPrefix());

            return $this->publicId;
        }
        set(string|PublicId $value) {
            $this->publicId = new PublicId($value);
        }
    }

    /**
     * @return non-empty-lowercase-string
     */
    public static function idPrefix(): string
    {
        $classReflector = new ClassReflector(self::class);
        $objectIdPrefix = $classReflector->getAttribute(PublicIdPrefix::class)
            ?? throw new MissingAttributeException(PublicIdPrefix::class);

        return $objectIdPrefix->prefix;
    }
}

Currently IsDatabaseModel / ModelInspector ignores it. Any way to add an option for an opt-in via an attribute?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions