maximaster/alterator

Alternatives iterator.

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/maximaster/alterator

v1.0.0 2024-08-23 12:28 UTC

This package is auto-updated.

Last update: 2025-09-23 14:41:07 UTC


README

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