Skip to content

Commit 6e9c6c5

Browse files
author
Robin Cawser
committed
Update readme to describe configurable generators and new secure string generator
1 parent 6a5e17e commit 6e9c6c5

1 file changed

Lines changed: 75 additions & 15 deletions

File tree

README.md

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,35 @@ vivait_string_generator:
3131
generators:
3232
string: vivait_generator.generator.string
3333
secure_bytes: vivait_generator.generator.secure_bytes
34+
secure_string: vivait_generator.generator.secure_bytes
35+
```
36+
37+
## Bundled generators
38+
39+
### `StringGenerator`
40+
Generates a random string based on a pool or characters. Defaults:
41+
42+
```php
43+
@Generate(generator="string", options={"length"=8, "chars"="abcdefjhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345567890", "prefix"=""})
3444
```
35-
### Bundled generators
36-
* `StringGenerator` generates a random string based on a pool or characters
37-
* `SecureBytesGenerator` generates a secure random string using the `Symfony\Component\Security\Core\Util\SecureRandom` class
3845

39-
### Custom generators
40-
You can use your own generators by implementing `GeneratorInterface` and defining the generator in the configuration,
41-
using either its service or classname.
4246

43-
## Basic usage
47+
### `SecureBytesGenerator`
48+
Generates a secure random byte string using the `Symfony\Component\Security\Core\Util\SecureRandom` class. Defaults:
49+
50+
```php
51+
@Generate(generator="secure_bytes", options={"length"=8})
52+
```
53+
54+
### `SecureStringGenerator`
55+
Generates a secure random string using [ircmaxell's RandomLib](https://github.com/ircmaxell/RandomLib). The library provides three different strengths of
56+
strings(currently `high` is unavailable), `low` and `medium`. Defaults:
57+
58+
```php
59+
@Generate(generator="secure_string", options={"length"=32, "chars"="", "strength"="medium"})
60+
```
4461

62+
## Usage
4563
Add the `@Generate(generator="generator_name")` annotation to an entity property
4664
(where `generator_name` is the name of a generator defined in the configuration).
4765

@@ -75,24 +93,26 @@ class Api
7593
*/
7694
private $api_key;
7795
```
78-
## Options
7996

80-
### Length
97+
### Options
98+
99+
Generators that implement `ConfigurableGeneratorInterface`, such as the bundled generators have options which can be configured.
100+
101+
To do this, set the options parameter on the annotation:
81102

82-
To change the length of the generated string, add `length` to the annotation.
83103
```php
84-
@Generate(length=4)
85-
```
104+
@Generate(generator="string", options={"length"=32, "chars"="ABCDEFG"})
105+
```
86106

87107
### Callbacks
88108

89109
It's possible to define callbacks on the `Generator` class that you are using.
90-
For example, with the bundled StringGenerator, you may wish to set the character pool.
110+
For example, with the bundled StringGenerator, you may wish to set the a prefix on the generated string
91111

92112
This can be achieved by setting the `callbacks` option. For example:
93113

94114
```php
95-
@Generate(generator="string", callbacks={"setChars"="ABCDEFG"})
115+
@Generate(generator="my_generator", callbacks={"setPrefix"="VIVA_"})
96116
```
97117

98118
Here, `setChars()` is called in the `StringGenerator` class, passing `ABCDEFG` as a parameter.
@@ -104,7 +124,7 @@ It's even possible to set a callback value dynamically:
104124
* @var string
105125
*
106126
* @ORM\Column(name="short_id", type="string", length=255, nullable=false)
107-
* @Generate(generator="string", length=5, callbacks={"setPrefix"="getPrefix"})
127+
* @Generate(generator="string", options={"length"=32}, callbacks={"setPrefix"="getPrefix"})
108128
*/
109129
private $short_id;
110130
@@ -133,3 +153,43 @@ However, by setting `override` to false, only null properties will have a string
133153
```php
134154
@Generate(generator="string", override=false)
135155
```
156+
157+
158+
## Custom generators
159+
You can use your own generators by implementing `GeneratorInterface` and defining the generator in the configuration,
160+
using either its service or classname.
161+
162+
To create configurable generators, implement `ConfigurableGeneratorInterface`. This interface uses
163+
[`Symfony\Component\OptionsResolver\OptionsResolver`](http://symfony.com/doc/current/components/options_resolver.html) to set the generator configuration.
164+
165+
Set default options:
166+
167+
```php
168+
/**
169+
* @param OptionsResolver $resolver
170+
* @return mixed
171+
*/
172+
public function getDefaultOptions(OptionsResolver $resolver)
173+
{
174+
$resolver->setDefaults([
175+
'chars' => $this->chars,
176+
'length' => $this->length,
177+
'prefix' => $this->prefix,
178+
]);
179+
}
180+
```
181+
182+
Do something with options:
183+
184+
```php
185+
/**
186+
* @param array $options
187+
* @return mixed|void
188+
*/
189+
public function setOptions(array $options)
190+
{
191+
$this->chars = $options['chars'];
192+
$this->length = $options['length'];
193+
$this->prefix = $options['prefix'];
194+
}
195+
```

0 commit comments

Comments
 (0)