From f19f9f62d13c62f18884e8bd0fa67403e8cad8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 11 Oct 2015 17:30:58 +0200 Subject: Add a form to create tagging rules --- .../DataTransformer/StringToListTransformer.php | 49 ++++++++++++++++++++++ .../CoreBundle/Form/Type/TaggingRuleType.php | 38 +++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php create mode 100644 src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php (limited to 'src/Wallabag/CoreBundle/Form') diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php new file mode 100644 index 00000000..332a91b8 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php @@ -0,0 +1,49 @@ +separator = $separator; + } + + /** + * Transforms a list to a string. + * + * @param array|null $list + * + * @return string + */ + public function transform($list) + { + if (null === $list) { + return ''; + } + + return implode($this->separator, $list); + } + + /** + * Transforms a string to a list. + * + * @param string $string + * + * @return array|null + */ + public function reverseTransform($string) + { + if (!$string) { + return null; + } + + return array_filter(array_map('trim', explode($this->separator, $string))); + } +} 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 @@ +add('rule', 'text', array('required' => true)) + ->add('save', 'submit') + ; + + $tagsField = $builder + ->create('tags', 'text') + ->addModelTransformer(new StringToListTransformer(',')); + + $builder->add($tagsField); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\CoreBundle\Entity\TaggingRule', + )); + } + + public function getName() + { + return 'tagging_rule'; + } +} -- cgit v1.2.3 From 003fa77438b13ceb9ceda245d6ad257801453d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 12 Oct 2015 21:43:24 +0200 Subject: Add tests for the StringToListTransformer class --- .../Form/DataTransformer/StringToListTransformer.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Form') diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php index 332a91b8..23488d35 100644 --- a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php +++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php @@ -6,10 +6,20 @@ use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; +/** + * Transforms a comma-separated list to a proper PHP array. + * Example: the string "foo, bar" will become the array ["foo", "bar"] + */ class StringToListTransformer implements DataTransformerInterface { + /** + * @var string + */ private $separator; + /** + * @param string $separator The separator used in the list. + */ public function __construct($separator = ',') { $this->separator = $separator; @@ -40,10 +50,10 @@ class StringToListTransformer implements DataTransformerInterface */ public function reverseTransform($string) { - if (!$string) { + if ($string === null) { return null; } - return array_filter(array_map('trim', explode($this->separator, $string))); + return array_values(array_filter(array_map('trim', explode($this->separator, $string)))); } } -- cgit v1.2.3