Skip to content

Commit 5f87315

Browse files
committed
README: Add how to use.
1 parent af00ec9 commit 5f87315

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,47 @@ $ composer require baraja-core/variable-generator
2929

3030
You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.
3131

32+
How to use
33+
----------
34+
35+
At the beginning, create an instance of the Generator or get it from the DIC. If you are using Doctrine entities, there is an autoconfiguration that will automatically find your entity with an order (must meet the `OrderEntity` interface) and you can start generating numbers.
36+
37+
Example:
38+
39+
```php
40+
$generator = new VariableGenerator(
41+
variableLoader, // last used variable loader, default is DefaultOrderVariableLoader
42+
strategy, // generator strategy, default is YearPrefixIncrementStrategy
43+
entityManager, // if you want use default variable loader by Doctrine entity
44+
);
45+
```
46+
47+
The generator is easy to use.
48+
49+
Retrieve the next free number (using it without an argument automatically retrieves the last used number based on the variableLoader service).
50+
51+
```php
52+
echo $generator->generate();
53+
```
54+
55+
Getting the next available number based on the user's choice:
56+
57+
```php
58+
echo $generator->generate(21010034); // next will be 21010035
59+
```
60+
61+
Retrieving the last generated number:
62+
63+
```php
64+
echo $generator->getCurrent();
65+
```
66+
67+
You can always choose your own strategy for generating numbers:
68+
69+
```php
70+
$generator->setStrategy();
71+
```
72+
3273
📄 License
3374
-----------
3475

src/VariableGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function __construct(
3737
*/
3838
public function generate(?string $last = null): int
3939
{
40-
$new = ($last ??= $this->variableLoader->getCurrent()) === null
40+
$last ??= $this->variableLoader->getCurrent();
41+
$new = $last === null
4142
? $this->strategy->getFirst()
4243
: $this->strategy->generate((string) preg_replace('/\D+/', '', (string) $last));
4344

0 commit comments

Comments
 (0)