Skip to content

Commit a08d8fe

Browse files
author
Robin Cawser
committed
Add updated README
1 parent dc04599 commit a08d8fe

1 file changed

Lines changed: 84 additions & 65 deletions

File tree

README.md

Lines changed: 84 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,95 +7,114 @@ generates a new string.
77

88
## Install
99

10-
Add `"vivait/string-generator-bundle": "dev-master"` to your composer.json and run `composer update`
10+
Add `"vivait/string-generator-bundle": "~1.0"` to your composer.json and run `composer update`
1111

1212
Update your `AppKernel`:
13-
14-
public function registerBundles()
15-
{
16-
$bundles = array(
17-
...
18-
new Vivait\StringGeneratorBundle\VivaitStringGeneratorBundle(),
19-
}
13+
```php
14+
public function registerBundles()
15+
{
16+
$bundles = array(
17+
...
18+
new Vivait\StringGeneratorBundle\VivaitStringGeneratorBundle(),
19+
}
20+
```
21+
## Configure
22+
23+
The default configuration is shown below:
24+
25+
```yaml
26+
vivait_string_generator:
27+
generators:
28+
string: vivait_generator.generator.string
29+
secure_bytes: vivait_generator.generator.secure_bytes
30+
```
31+
### Bundled generators
32+
* `StringGenerator` generates a random string based on a pool or characters
33+
* `SecureBytesGenerator` generates a secure random string using the `Symfony\Component\Security\Core\Util\SecureRandom` class
34+
35+
### Custom generators
36+
You can use your own generators by implementing `GeneratorInterface` and defining the generator in the configuration,
37+
using either its service or classname.
2038

2139
## Basic usage
2240

23-
Add the `@Vivait\StringGenerator()` annotation to an entity property
41+
Add the `@Generate(generator="generator_name")` annotation to an entity property
42+
(where `generator_name` is the name of a generator defined in the configuration).
2443

25-
use Vivait\StringGeneratorBundle\Annotation as Vivait;
26-
44+
```php
45+
use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation as Generate;
46+
47+
/**
48+
* Api
49+
*
50+
* @ORM\Table()
51+
* @ORM\Entity()
52+
*/
53+
class Api
54+
{
2755
/**
28-
* Api
56+
* @var integer
2957
*
30-
* @ORM\Table()
31-
* @ORM\Entity()
58+
* @ORM\Column(name="id", type="guid")
59+
* @ORM\Id
60+
* @ORM\GeneratedValue(strategy="UUID")
3261
*/
33-
class Api
34-
{
35-
/**
36-
* @var integer
37-
*
38-
* @ORM\Column(name="id", type="guid")
39-
* @ORM\Id
40-
* @ORM\GeneratedValue(strategy="UUID")
41-
*/
42-
private $id;
43-
44-
/**
45-
* @var string
46-
*
47-
* @ORM\Column(name="api_id", type="string", nullable=false)
48-
* @Vivait\StringGenerator()
49-
*/
50-
private $api_key;
62+
private $id;
5163
64+
/**
65+
* @var string
66+
*
67+
* @ORM\Column(name="api_id", type="string", nullable=false)
68+
* @Generate(generator="string")
69+
*/
70+
private $api_key;
71+
```
5272
## Options
5373

5474
### Length
5575

56-
To change the length of the generated string, add `length` to the annotation. The default is 8. Length specifies the length
57-
of the resulting generated string, and does not include the prefix or separator
76+
To change the length of the generated string, add `length` to the annotation.
77+
```php
78+
@Generate(length=4)
79+
```
5880

59-
@Vivait\StringGenerator(length=4)
81+
### Callbacks
6082

61-
### Prefix the key
83+
It's possible to define callbacks on the `Generator` class that you are using.
84+
For example, with the bundled StringGenerator, you may wish to set the character pool.
6285

63-
To prefix the string, add the `prefix` option to the annotation. The default seperator between the prefix and generated
64-
string is a `-`, but this can be changed using `separator`:
86+
This can be achieved by setting the `callbacks` option. For example:
6587

66-
@Vivait\StringGenerator(prefix="user", separator="_")
88+
```php
89+
@Generate(generator="string", callbacks={"setChars"="ABCDEFG"})
90+
```
6791

68-
A prefix can be obtained via a callback to a method in the entity using `prefix_callback`, which overrides `prefix`.
92+
Here, `setChars()` is called in the `StringGenerator` class, passing `ABCDEFG` as a parameter.
6993

70-
/**
71-
* @var string
72-
*
73-
* @ORM\Column(name="friendly_id", type="string", nullable=false)
74-
* @Vivait\StringGenerator(prefix_callback="createPrefix", length=8)
75-
*/
76-
private $friendly_id;
77-
78-
public function createPrefix()
79-
{
80-
return $this->category->getCode();
81-
}
82-
83-
### Alphabet
94+
It's even possible to set a callback value dynamically:
8495

85-
Setting `alphabet` limits the characters the generator can choose from. Defaults to alphanumeric.
96+
```php
97+
/**
98+
* @var string
99+
*
100+
* @ORM\Column(name="short_id", type="string", length=255, nullable=false)
101+
* @Generate(generator="string", length=5, callbacks={"setPrefix"="getPrefix"})
102+
*/
103+
private $short_id;
86104
87-
@Vivait\StringGenerator(alphabet="abcdefghkmnpqrstuwxyz")
88-
89-
### Unique
105+
public function getPrefix()
106+
{
107+
return $this->getType(); //"default"
108+
}
109+
```
90110

91-
Setting `unique` is boolean and tell if the string must be unique or not, by default `true`
111+
In this case `StringGenerator::setPrefix("default")` will be called
92112

93-
@Vivait\StringGenerator(unique="false")
94113

95-
## Custom generator
114+
### Unique
96115

97-
If you want to use a different generator to create your string, create a class that implements namespace
98-
`Vivait\StringGeneratorBundle\Model\GeneratorInterface`. Add the fully qualified classname to your config.yml:
116+
Setting `unique` is boolean and tell if the string must be unique or not, by default `true`
99117

100-
vivait_string_generator:
101-
generator_class: Acme\BlogBundle\Generator\UsernameGenerator
118+
```php
119+
@Generate(generator="secure_bytes", unique=false)
120+
```

0 commit comments

Comments
 (0)