Skip to content

Commit 1676636

Browse files
author
Robin Cawser
committed
Add configurable generators
1 parent 53e349e commit 1676636

6 files changed

Lines changed: 78 additions & 2 deletions

File tree

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"php": ">=5.3.3",
1515
"doctrine/common": "~2.2",
1616
"doctrine/orm": "~2.2",
17-
"symfony/security": "~2.1"
17+
"symfony/security": "~2.1",
18+
"symfony/options-resolver": "~2.1",
19+
"ircmaxell/random-lib": "~1.0"
1820
},
1921
"require-dev": {
2022
"phpspec/phpspec": "~2.0"

spec/Vivait/StringGeneratorBundle/EventListener/GeneratorListenerSpec.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Doctrine\ORM\Mapping\ClassMetadata;
1212
use PhpSpec\ObjectBehavior;
1313
use Prophecy\Argument;
14+
use Symfony\Component\OptionsResolver\OptionsResolver;
1415
use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation;
1516
use Vivait\StringGeneratorBundle\Generator\StringGenerator;
17+
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
1618
use Vivait\StringGeneratorBundle\Registry\Registry;
1719

1820
class GeneratorListenerSpec extends ObjectBehavior
@@ -96,6 +98,24 @@ function it_generates_a_string_on_a_property(Registry $registry, Reader $reader,
9698

9799
$this->prePersist($args);
98100
}
101+
102+
function it_can_configure_a_generator(Registry $registry, Reader $reader, LifecycleEventArgs $args, ConfigurableGeneratorInterface $generator)
103+
{
104+
$annotation = new GeneratorAnnotation([]);
105+
106+
$reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]);
107+
108+
$registry->get(Argument::any())->willReturn($generator);
109+
$generator->generate()->shouldBeCalled();
110+
$generator->setLength(Argument::any())->shouldBeCalled();
111+
112+
$resolver = new OptionsResolver();
113+
114+
$generator->getDefaultOptions($resolver)->shouldBeCalled();
115+
$generator->setOptions(Argument::any())->shouldBeCalled();
116+
117+
$this->prePersist($args);
118+
}
99119
}
100120

101121
class Entity

src/Vivait/StringGeneratorBundle/Annotation/GeneratorAnnotation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@
1212
*/
1313
class GeneratorAnnotation extends Annotation
1414
{
15+
/**
16+
* @var array
17+
*/
1518
public $callbacks = [];
19+
20+
/**
21+
* @var array
22+
*/
23+
public $options = [];
24+
1625
/**
1726
* @var
1827
*/
@@ -28,5 +37,8 @@ class GeneratorAnnotation extends Annotation
2837
*/
2938
public $unique = true;
3039

40+
/**
41+
* @var bool
42+
*/
3143
public $override = true;
3244
}

src/Vivait/StringGeneratorBundle/EventListener/GeneratorListener.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Doctrine\ORM\EntityRepository;
77
use Doctrine\ORM\Event\LifecycleEventArgs;
88
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Symfony\Component\OptionsResolver\OptionsResolver;
910
use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation;
11+
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
1012
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
1113
use Vivait\StringGeneratorBundle\Registry\Registry;
1214

@@ -88,7 +90,7 @@ public function prePersist(LifecycleEventArgs $args)
8890
*/
8991
private function generateString($property, GeneratorAnnotation $annotation, $object)
9092
{
91-
/** @var GeneratorInterface $generator */
93+
/** @var GeneratorInterface|ConfigurableGeneratorInterface $generator */
9294
$generator = $this->getRegistry()->get($annotation->generator);
9395

9496
$generator->setLength($annotation->length);
@@ -97,6 +99,10 @@ private function generateString($property, GeneratorAnnotation $annotation, $obj
9799
$this->performCallbacks($generator, $annotation, $object);
98100
}
99101

102+
if($generator instanceof ConfigurableGeneratorInterface){
103+
$generator = $this->configureGenerator($generator, $annotation->options);
104+
}
105+
100106
$str = $generator->generate();
101107

102108
if (!$annotation->unique) {
@@ -110,6 +116,22 @@ private function generateString($property, GeneratorAnnotation $annotation, $obj
110116
}
111117
}
112118

119+
/**
120+
* @param ConfigurableGeneratorInterface $generator
121+
* @param $options
122+
* @return \Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface
123+
*/
124+
public function configureGenerator(ConfigurableGeneratorInterface $generator, $options)
125+
{
126+
$resolver = new OptionsResolver();
127+
$generator->getDefaultOptions($resolver);
128+
129+
$options = $resolver->resolve($options);
130+
$generator->setOptions($options);
131+
132+
return $generator;
133+
}
134+
113135
/**
114136
* @param GeneratorInterface $generator
115137
* @param GeneratorAnnotation $annotation

src/Vivait/StringGeneratorBundle/Generator/StringGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ public function generate()
6666

6767
return $this->prefix . implode($str);
6868
}
69+
70+
/**
71+
* @param array $options
72+
*/
73+
public function setOptions(array $options)
74+
{
75+
// TODO: Implement setOptions() method.
76+
}
6977
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Vivait\StringGeneratorBundle\Model;
4+
5+
use Symfony\Component\OptionsResolver\OptionsResolver;
6+
7+
interface ConfigurableGeneratorInterface extends GeneratorInterface
8+
{
9+
public function setOptions(array $options = []);
10+
11+
public function getDefaultOptions(OptionsResolver $resolver);
12+
}

0 commit comments

Comments
 (0)