mercredi 20 février 2019

What kind of pattern would i need

I have a Sequence class that can generate codes.

$sequence = new Sequence();

$sequence->startsWith('FACT')
    ->followedBy('-')
    ->followedBy(new NumberPart(4, 'Year')) //The four indicates that the number will be 4 digits long. Zero-padded if needed
    ->followedBy((new NumberPart(4, 'InvoiceNo'))) //The four indicates that the number will be 4 digits long. Zero-padded if needed
    ->startingAt('FACT-20170020');

$sequence->checkUniqueness()->inDatabase('orders', 'order_number'); //Does not exist yet

$nextValue = $sequence->next(); //FACT-20170021

The sequence class works perfectly. And i am now trying to extend it with the checkUniqueness method. But i am not sure how to build it. Because i maybe also want to use the method like this:

$sequence->checkUniqueness()->inArray(['FACT-20170020', 'FACT-20170021']);

When the next number is not unique in either the array of database i want it to throw an exception.

This is what i am trying:

The checkUniqueness method returns a factory (not sure if this is the correct pattern). The factory has both inArray and inDatabase methods. Which respectively can make an inArrayUniquenessChecker and a inDatabaseUniqueness checker instance. These both implement en interface called UniquenessCheckerInterface which defines the function signature:

public function isUnique(string $value): bool;

But the issue with this is that i don't expect it to return for example the inDatabaseUniqueness class instance. I expect the inArray and inDatabase methods to give their results to the Sequence instance.

How can i do this. And is this the correct mindset / pattern for this problem?

Aucun commentaire:

Enregistrer un commentaire