Skip to content

Commit af00ec9

Browse files
committed
Add soo much internal doc descriptions.
1 parent 0323314 commit af00ec9

File tree

7 files changed

+79
-15
lines changed

7 files changed

+79
-15
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ Variable generator
55

66
Generate new variable symbol by last variable and selected strategy.
77

8+
Idea
9+
----
10+
11+
A series of smart tools for generating variable symbols and order numbers in your e-shop.
12+
13+
Generating order numbers or other number series hides a number of complex problems. For example, adhering to the specified format according to the specification, handling transaction entries (to avoid duplication) and handling the case when the generated value overflows.
14+
15+
This package contains a set of algorithms and ready-made strategies to elegantly solve these problems. If any of the algorithms do not suit you, you can implement your own just by satisfying the defined interface.
16+
817
📦 Installation
918
---------------
1019

src/Order/OrderEntity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
namespace Baraja\VariableGenerator\Order;
66

77

8+
/**
9+
* A generic definition of an entity (most often an entity representing an order) in your database schema.
10+
* Each variant entity has a unique identifier (often a row ID) and a unique human-friendly generated number.
11+
* This data serves as the basis for generating a new number.
12+
*/
813
interface OrderEntity
914
{
1015
public function getId(): string|int|null;

src/Strategy/FormatStrategy.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
namespace Baraja\VariableGenerator\Strategy;
66

77

8+
/**
9+
* Generates the next number in the sequence based on the implemented strategy.
10+
* The next number is always determined by a deterministic algorithm based on the defined input.
11+
* If you are servicing a large number of orders in parallel,
12+
* you can request a number of numbers in advance and cache the results.
13+
*
14+
* Each strategy guarantees that a valid number in the defined range
15+
* (or the first number if none existed before) will always be returned.
16+
*/
817
interface FormatStrategy
918
{
19+
/**
20+
* Returns the next number in sequence based on the input.
21+
* The generation is always done by a deterministic algorithm.
22+
*/
1023
public function generate(string $last): string;
1124

1225
/**
13-
* Generate first variable, if last does not exist.
26+
* Generate the first variable, if last does not exist.
1427
*/
1528
public function getFirst(): string;
1629
}

src/Strategy/SimpleIncrementStrategy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
namespace Baraja\VariableGenerator\Strategy;
66

77

8+
/**
9+
* The service just adds one to the last generated number and tries to keep the range.
10+
* No further logic is performed.
11+
* If the number did not exist before, it generates a new one with the prefix of the current year.
12+
*/
813
final class SimpleIncrementStrategy implements FormatStrategy
914
{
1015
public function __construct(

src/Strategy/YearPrefixIncrementStrategy.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
namespace Baraja\VariableGenerator\Strategy;
66

77

8+
/**
9+
* The general formatting strategy that generates this format:
10+
*
11+
* YYXXXXXX
12+
* ^ ^
13+
* | \_ Generated number
14+
* \___ Year prefix
15+
*
16+
* Sample generated number can be 21000001 for first number in 2021.
17+
*
18+
* This strategy tries to maintain a defined number of characters.
19+
* The number of characters may overflow in case of a large number of orders.
20+
* The minimum length of the generated number is 3 characters.
21+
*
22+
* If there is a natural year change (during New Year's Eve),
23+
* the number series will automatically reset and the new number will be the first in the sequence of the new year.
24+
*/
825
final class YearPrefixIncrementStrategy implements FormatStrategy
926
{
1027
public function __construct(
@@ -16,20 +33,16 @@ public function __construct(
1633

1734
public function generate(string $last): string
1835
{
19-
if (preg_match('/^(?<year>\d{2})(?<count>\d+)$/', $last, $variableParser)) {
20-
$year = date('y');
21-
if ($year === $variableParser['year']) {
22-
$length = $this->length ?? strlen($variableParser['count']);
23-
$new = $variableParser['year']
24-
. str_pad(
25-
string: (string) ($variableParser['count'] + 1),
26-
length: $length,
27-
pad_string: '0',
28-
pad_type: STR_PAD_LEFT,
29-
);
30-
} else {
31-
$new = $year . str_repeat('0', strlen($variableParser['count']) - 1) . '1';
32-
}
36+
$year = date('y');
37+
if (preg_match('/^' . $year . '(?<count>\d+)$/', $last, $parser)) {
38+
$length = $this->length ?? strlen($parser['count']);
39+
$new = $year
40+
. str_pad(
41+
string: (string) ($parser['count'] + 1),
42+
length: $length,
43+
pad_string: '0',
44+
pad_type: STR_PAD_LEFT,
45+
);
3346
} else {
3447
$new = $this->getFirst();
3548
}

src/VariableGenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Baraja\VariableGenerator\Strategy\YearPrefixIncrementStrategy;
1212
use Doctrine\ORM\EntityManagerInterface;
1313

14+
/**
15+
* A generic service for generating a unique order number, variable symbol or other unique identifier.
16+
*/
1417
final class VariableGenerator
1518
{
1619
private VariableLoader $variableLoader;
@@ -42,6 +45,10 @@ public function generate(?string $last = null): int
4245
}
4346

4447

48+
/**
49+
* Returns the current latest number. For example, the order number.
50+
* Warning: Never use this number for create a new entity, in this case use the generate() method.
51+
*/
4552
public function getCurrent(bool $findReal = true): int
4653
{
4754
$currentValue = $findReal === true
@@ -58,6 +65,13 @@ public function setStrategy(FormatStrategy $strategy): void
5865
}
5966

6067

68+
/**
69+
* Automatically finds a unique Doctrine entity that implements the OrderEntity interface.
70+
* In most applications, there is only one unique entity for an order,
71+
* so this interface can be used as the key for the search.
72+
* If your application uses multiple entities for which you need to generate variable symbols,
73+
* implement a custom service for VariableLoader.
74+
*/
6175
private function resolveVariableLoader(?VariableLoader $loader, ?EntityManagerInterface $em): VariableLoader
6276
{
6377
if ($loader !== null) {

src/VariableLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
namespace Baraja\VariableGenerator;
66

77

8+
/**
9+
* A generic interface that gets the number of the last stored entity of the type.
10+
* It is most commonly used as a DIC service to retrieve the last order from the database.
11+
* This service also solves the loading problem about database transactions.
12+
*/
813
interface VariableLoader
914
{
1015
public function getCurrent(): ?string;

0 commit comments

Comments
 (0)