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