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

[8.x] Adds parameter casting for cursor paginated items #37785

Merged
merged 1 commit into from
Jun 24, 2021

Conversation

jspekken
Copy link
Contributor

This fixes the problem when the primary-key (or other attributes) are
objects (e.g.: value-objects) and can be cast to a string using the
__toString method.

Before, the CursorPaginator would always return a pointer to the current
page instead of the next page because the parameter, which was an
object, could not be converted.

This fixes the problem when the primary-key (or other attributes) are
objects (e.g.: value-objects) and can be cast to a string using the
__toString method.

Before, the CursorPaginator would always return a pointer to the current
page instead of the next page because the parameter, which was an
object, could not be converted.
@GrahamCampbell GrahamCampbell changed the title Adds parameter casting for cursor paginated items. [8.x] Adds parameter casting for cursor paginated items Jun 23, 2021
@GrahamCampbell
Copy link
Member

I don't think we support doing that? You should use primitives for primary key values.

@jspekken
Copy link
Contributor Author

Why am I limited to primitives? I'd like to make extensive use of value-objects and use attribute casting to facilitate that:

final class PrimaryKeyAttributeCast implements CastsAttributes
{
    public function get($model, string $key, $value, array $attributes): PrimaryKey
    {
        if ($value === null) {
            return PrimaryKey::generate();
        }

        return PrimaryKey::fromString($value);
    }

    public function set($model, string $key, $value, array $attributes): string
    {
        Assert::isInstanceOf($value, PrimaryKey::class);

        return $value->toString();
    }
}

final class PrimaryKey implements Stringable
{
    private Ulid $value;

    public function __toString(): string
    {
        return $this->toString();
    }
    
    public function toString(): string
    {
        return (string) $this->value;
    }

    public function equals(mixed $other): bool
    {
        if ($other instanceof self && get_class($other) === self::class) {
            return (string) $this->value === (string) $other->value;
        }

        return false;
    }
    
    public static function fromString(string $value): self
    {
        return new self(Ulid::fromString($value, lowercase: false));
    }

    public static function generate(): self
    {
        return new self(Ulid::generate(lowercase: false));
    }

    private function __construct(Ulid $value)
    {
        $this->value = $value;
    }
}

That way I'm able to do something like this:

$item->id->equals($otherItem->id); 
// or even:
$item->equals($otherItem);

This hides hides the implementation details in the value objects.
This works perfectly except that the cursor paginator won't cast an object to a string. This fixes that issue :)

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.

None yet

3 participants