Skip to content

Stringable ID types #203

@theofidry

Description

@theofidry

In IDType, you are checking that the value is a scalar and return a string value:

    /**
     * @param mixed $value
     * @return string
     */
    public function serialize($value)
    {
        if ($value === true) {
            return 'true';
        }
        if ($value === false) {
            return 'false';
        }
        if ($value === null) {
            return 'null';
        }
        if (!is_scalar($value)) {
            throw new InvariantViolation("ID type cannot represent non scalar value: " . Utils::printSafe($value));
        }
        return (string) $value;
    }

This is actually wrong as there is one more kind of value that could be valid: classes implementing __toString. For example you could have an ID implementing a Uuid class or even something more specific to your domain like UserId.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions