Skip to content

maximaster/alterator

Repository files navigation

maximaster/alterator

Alternatives iterator.

composer require maximaster/alterator

Example

Imagine you have an object with the name "Document (5)" and a user decides to copy it. You need to generate a new name for this copy.

Here's how you can do it using the library:

// create the library classes manually or get them from your DI container.
$unusedSeeker = new AlteratorUnsusedSeeker(new PostfixIndexAlterator());

$copyName = $unusedSeeker->suggest(
    // Old name.
    'Document (5)',
    // Your strategy to ensure that the suggested name is not already used.
    static fn (string $alternativeName): bool => $database->exists('
        SELECT 1 FROM documents WHERE NAME = ?
    ', [$alternativeName])
);

// This would be "Document (6)" unless your strategy found it already used.
return $copyName;

See other examples in spec folder.

Development

devbox run lint
devbox run test