diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Form')
-rw-r--r-- | src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php | 59 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php | 38 |
2 files changed, 97 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php new file mode 100644 index 00000000..23488d35 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Form\DataTransformer; | ||
4 | |||
5 | use Doctrine\Common\Persistence\ObjectManager; | ||
6 | use Symfony\Component\Form\DataTransformerInterface; | ||
7 | use Symfony\Component\Form\Exception\TransformationFailedException; | ||
8 | |||
9 | /** | ||
10 | * Transforms a comma-separated list to a proper PHP array. | ||
11 | * Example: the string "foo, bar" will become the array ["foo", "bar"] | ||
12 | */ | ||
13 | class StringToListTransformer implements DataTransformerInterface | ||
14 | { | ||
15 | /** | ||
16 | * @var string | ||
17 | */ | ||
18 | private $separator; | ||
19 | |||
20 | /** | ||
21 | * @param string $separator The separator used in the list. | ||
22 | */ | ||
23 | public function __construct($separator = ',') | ||
24 | { | ||
25 | $this->separator = $separator; | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Transforms a list to a string. | ||
30 | * | ||
31 | * @param array|null $list | ||
32 | * | ||
33 | * @return string | ||
34 | */ | ||
35 | public function transform($list) | ||
36 | { | ||
37 | if (null === $list) { | ||
38 | return ''; | ||
39 | } | ||
40 | |||
41 | return implode($this->separator, $list); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Transforms a string to a list. | ||
46 | * | ||
47 | * @param string $string | ||
48 | * | ||
49 | * @return array|null | ||
50 | */ | ||
51 | public function reverseTransform($string) | ||
52 | { | ||
53 | if ($string === null) { | ||
54 | return null; | ||
55 | } | ||
56 | |||
57 | return array_values(array_filter(array_map('trim', explode($this->separator, $string)))); | ||
58 | } | ||
59 | } | ||
diff --git a/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php b/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php new file mode 100644 index 00000000..7fbba38a --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php | |||
@@ -0,0 +1,38 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Form\Type; | ||
4 | |||
5 | use Symfony\Component\Form\AbstractType; | ||
6 | use Symfony\Component\Form\FormBuilderInterface; | ||
7 | use Symfony\Component\OptionsResolver\OptionsResolver; | ||
8 | |||
9 | use Wallabag\CoreBundle\Form\DataTransformer\StringToListTransformer; | ||
10 | |||
11 | class TaggingRuleType extends AbstractType | ||
12 | { | ||
13 | public function buildForm(FormBuilderInterface $builder, array $options) | ||
14 | { | ||
15 | $builder | ||
16 | ->add('rule', 'text', array('required' => true)) | ||
17 | ->add('save', 'submit') | ||
18 | ; | ||
19 | |||
20 | $tagsField = $builder | ||
21 | ->create('tags', 'text') | ||
22 | ->addModelTransformer(new StringToListTransformer(',')); | ||
23 | |||
24 | $builder->add($tagsField); | ||
25 | } | ||
26 | |||
27 | public function configureOptions(OptionsResolver $resolver) | ||
28 | { | ||
29 | $resolver->setDefaults(array( | ||
30 | 'data_class' => 'Wallabag\CoreBundle\Entity\TaggingRule', | ||
31 | )); | ||
32 | } | ||
33 | |||
34 | public function getName() | ||
35 | { | ||
36 | return 'tagging_rule'; | ||
37 | } | ||
38 | } | ||