]> git.immae.eu Git - github/wallabag/wallabag.git/blame - 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
CommitLineData
f19f9f62
KG
1<?php
2
3namespace Wallabag\CoreBundle\Form\DataTransformer;
4
5use Doctrine\Common\Persistence\ObjectManager;
6use Symfony\Component\Form\DataTransformerInterface;
7use Symfony\Component\Form\Exception\TransformationFailedException;
8
9class 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}