1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Black\Component\Common\Application\Form\Transformer; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
15
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Transforms between a delimited string (e.g. a string delimited |
19
|
|
|
* by commas) and an array. |
20
|
|
|
* |
21
|
|
|
* @author Daniel Leech <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ArrayToDelimitedStringTransformer implements DataTransformerInterface |
24
|
|
|
{ |
25
|
|
|
private $delimiter; |
26
|
|
|
private $paddingLeft; |
27
|
|
|
private $paddingRight; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param string $delimiter The delimiter to use when transforming from |
33
|
|
|
* a string to an array and vice-versa |
34
|
|
|
* @param integer Number of spaces before each transformed array element |
35
|
|
|
* @param integer Number of spaces after each transformed array element |
36
|
|
|
*/ |
37
|
|
|
public function __construct($delimiter = ',', $paddingLeft = 0, $paddingRight = 1) |
38
|
|
|
{ |
39
|
|
|
$this->delimiter = $delimiter; |
40
|
|
|
$this->paddingLeft = $paddingLeft; |
41
|
|
|
$this->paddingRight = $paddingRight; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Transforms an array into a delimited string |
46
|
|
|
* |
47
|
|
|
* @param array $array Array to transform |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
* |
51
|
|
|
* @throws TransformationFailedException If the given value is not an array |
52
|
|
|
*/ |
53
|
|
|
public function transform($array) |
54
|
|
|
{ |
55
|
|
|
if (null === $array) { |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!is_array($array)) { |
60
|
|
|
throw new TransformationFailedException('Expected an array.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($array as &$value) { |
64
|
|
|
$value = sprintf('%s%s%s', |
65
|
|
|
str_repeat(' ', $this->paddingLeft), |
66
|
|
|
$value, |
67
|
|
|
str_repeat(' ', $this->paddingRight) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$string = trim(implode($this->delimiter, $array)); |
72
|
|
|
|
73
|
|
|
return $string; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Transforms a delimited string into an array |
78
|
|
|
* |
79
|
|
|
* @param string $string String to transform |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
* |
83
|
|
|
* @throws TransformationFailedException If the given value is not a string |
84
|
|
|
*/ |
85
|
|
|
public function reverseTransform($string) |
86
|
|
|
{ |
87
|
|
|
if (null !== $string && !is_string($string)) { |
88
|
|
|
throw new TransformationFailedException('Expected a string.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$string = trim($string); |
92
|
|
|
if (empty($string)) { |
93
|
|
|
return array(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$values = explode($this->delimiter, $string); |
97
|
|
|
|
98
|
|
|
if (0 === count($values)) { |
99
|
|
|
return array(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($values as &$value) { |
103
|
|
|
$value = trim($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $values; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|