Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid]: Improved Ulid::isValid #61589

@mms-uret

Description

@mms-uret

Description

Ulid::isValid(string): bool is at the moment very strict about which text representation of the Ulid is valid and which not.

I would expect that this would work:

$ulid = new Ulid();
$string = $ulid->toRfc4122();
Ulid::isValid($string) // I would expect this to be true

The workarround we are using is to try to test if a string is a valid Ulid:

function isUlidTextRepresentation(string $string): bool
{
  try {
    Ulid::fromString($string);
  } catch (\InvalidArgumentException) {
    return false;
  }
  return true;
}

I think it would be possible to either change the Ulid::isValid method with the above implementation, or have another static function of the Ulid class to do something similar to this, to not change the behaviour of Ulid::isValid(). I could create a PR for either option....

Example

$ulid = new Ulid();
$string = $ulid->toRfc4122();
Ulid::isValid($string) // This returns true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions