Skip to content

Commit 7dc94fd

Browse files
committed
Merge pull request #10 from robcaw/develop
1.0 Release
2 parents 3ec9f77 + 324a210 commit 7dc94fd

16 files changed

Lines changed: 712 additions & 69 deletions

File tree

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### Composer ###
2+
composer.phar
3+
composer.lock
4+
vendor/
5+
bin/
6+
7+
### PhpStorm ###
8+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
9+
10+
/*.iml
11+
12+
## Directory-based project format:
13+
.idea/
14+
# if you remove the above rule, at least ignore the follwing:
15+
16+
# User-specific stuff:
17+
# .idea/workspace.xml
18+
# .idea/tasks.xml
19+
# .idea/dictionaries
20+
21+
# Sensitive or high-churn files:
22+
# .idea/dataSources.ids
23+
# .idea/dataSources.xml
24+
# .idea/sqlDataSources.xml
25+
# .idea/dynamic.xml
26+
# .idea/uiDesigner.xml
27+
28+
# Gradle:
29+
# .idea/gradle.xml
30+
# .idea/libraries
31+
32+
# Mongo Explorer plugin:
33+
# .idea/mongoSettings.xml
34+
35+
## File-based project format:
36+
*.ipr
37+
*.iws
38+
39+
## Plugin-specific files:
40+
41+
# IntelliJ
42+
out/
43+
44+
# mpeltonen/sbt-idea plugin
45+
.idea_modules/
46+
47+
# JIRA plugin
48+
atlassian-ide-plugin.xml
49+
50+
# Crashlytics plugin (for Android Studio and IntelliJ)
51+
com_crashlytics_export_strings.xml

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+
```

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
"email":"robin@vivait.co.uk"
88
}
99
],
10+
"minimum-stability": "dev",
11+
"prefer-stable": true,
1012
"require": {
1113
"php": ">=5.3.3",
12-
"doctrine/common": "~2.2"
14+
"doctrine/common": "~2.2",
15+
"symfony/security": "~2.1"
1316
},
1417
"require-dev": {
1518
"phpspec/phpspec": "~2.0"
@@ -19,8 +22,7 @@
1922
},
2023
"autoload":{
2124
"psr-0":{
22-
"Vivait\\StringGeneratorBundle":""
25+
"Vivait\\StringGeneratorBundle\\": "src/"
2326
}
24-
},
25-
"target-dir":"Vivait/StringGeneratorBundle"
27+
}
2628
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace spec\Vivait\StringGeneratorBundle\EventListener;
4+
5+
use Doctrine\Common\Annotations\Reader;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation;
9+
use Vivait\StringGeneratorBundle\Generator\StringGenerator;
10+
use Vivait\StringGeneratorBundle\Registry\Registry;
11+
12+
class GeneratorListenerSpec extends ObjectBehavior
13+
{
14+
function it_is_initializable()
15+
{
16+
$this->shouldHaveType('Vivait\StringGeneratorBundle\EventListener\GeneratorListener');
17+
}
18+
19+
function let(Reader $reader, Registry $registry)
20+
{
21+
$this->beConstructedWith($reader, $registry);
22+
}
23+
24+
function it_performs_callbacks_on_the_generator(StringGenerator $generator, Entity $mockEntity)
25+
{
26+
$annotation = new GeneratorAnnotation([]);
27+
$annotation->callbacks = ['setChars' => 'abcdef'];
28+
$this->shouldNotThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity);
29+
30+
$annotation->callbacks = ['noMethod' => 'something'];
31+
$this->shouldThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity);
32+
}
33+
34+
function it_can_get_callback_values_from_annotated_object(StringGenerator $generator, Entity $mockEntity)
35+
{
36+
$annotation = new GeneratorAnnotation([]);
37+
$annotation->callbacks = ['setPrefix' => 'getPrefix'];
38+
39+
$mockEntity->getPrefix()->willReturn('VIVA_');
40+
$generator->setPrefix('VIVA_')->shouldBeCalled();
41+
$this->performCallbacks($generator, $annotation, $mockEntity);
42+
}
43+
}
44+
45+
class Entity
46+
{
47+
public function getPrefix()
48+
{
49+
50+
}
51+
}
52+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace spec\Vivait\StringGeneratorBundle\Generator;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Symfony\Component\Security\Core\Util\SecureRandom;
8+
9+
class SecureBytesGeneratorSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType('Vivait\StringGeneratorBundle\Generator\SecureBytesGenerator');
14+
}
15+
16+
function let()
17+
{
18+
$secureRandom = new SecureRandom();
19+
$this->beConstructedWith($secureRandom);
20+
}
21+
22+
function it_generates_random_string()
23+
{
24+
$this->setLength(10);
25+
$this->generate()->shouldHaveStrlen(10);
26+
}
27+
28+
function getMatchers()
29+
{
30+
return [
31+
'haveStrlen' => function($string, $length){
32+
return strlen($string) == $length;
33+
}
34+
];
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace spec\Vivait\StringGeneratorBundle\Generator;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class StringGeneratorSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Vivait\StringGeneratorBundle\Generator\StringGenerator');
12+
}
13+
14+
function it_generates_a_string_based_on_length()
15+
{
16+
$this->setLength(10);
17+
$this->generate()->shouldBeString();
18+
$this->generate()->shouldHaveStrlen(10);
19+
}
20+
21+
function it_should_throw_an_exception_if_it_has_no_chars()
22+
{
23+
$this->shouldThrow('\OutOfBoundsException')->duringSetChars('');
24+
$this->shouldThrow('\OutOfBoundsException')->duringSetChars(null);
25+
$this->shouldThrow('\OutOfBoundsException')->duringSetChars(true);
26+
}
27+
28+
function it_should_error_if_its_length_is_set_to_less_than_1()
29+
{
30+
$this->shouldThrow('\OutOfBoundsException')->duringSetLength(0);
31+
$this->shouldThrow('\OutOfBoundsException')->duringSetLength(-1);
32+
$this->shouldThrow('\OutOfBoundsException')->duringSetLength("ten");
33+
}
34+
35+
function getMatchers()
36+
{
37+
return [
38+
'haveStrlen' => function($string, $length){
39+
return strlen($string) == $length;
40+
}
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)