]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php
Add a form to create tagging rules
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Form / DataTransformer / StringToListTransformer.php
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 class StringToListTransformer implements DataTransformerInterface
10 {
11 private $separator;
12
13 public function __construct($separator = ',')
14 {
15 $this->separator = $separator;
16 }
17
18 /**
19 * Transforms a list to a string.
20 *
21 * @param array|null $list
22 *
23 * @return string
24 */
25 public function transform($list)
26 {
27 if (null === $list) {
28 return '';
29 }
30
31 return implode($this->separator, $list);
32 }
33
34 /**
35 * Transforms a string to a list.
36 *
37 * @param string $string
38 *
39 * @return array|null
40 */
41 public function reverseTransform($string)
42 {
43 if (!$string) {
44 return null;
45 }
46
47 return array_filter(array_map('trim', explode($this->separator, $string)));
48 }
49 }