1313use Selective \Transformer \Filter \NumberFormatFilter ;
1414use Selective \Transformer \Filter \StringFilter ;
1515
16+ /**
17+ * Transformer.
18+ */
1619final class ArrayTransformer
1720{
1821 /**
@@ -25,35 +28,52 @@ final class ArrayTransformer
2528 */
2629 private $ converter ;
2730
31+ /**
32+ * @var string[]
33+ */
34+ private $ internalFilters = [
35+ 'string ' => StringFilter::class,
36+ 'blank-to-null ' => BlankToNullFilter::class,
37+ 'boolean ' => BooleanFilter::class,
38+ 'integer ' => IntegerFilter::class,
39+ 'float ' => FloatFilter::class,
40+ 'number ' => NumberFormatFilter::class,
41+ 'date ' => DateTimeFilter::class,
42+ 'array ' => ArrayFilter::class,
43+ 'callback ' => CallbackFilter::class,
44+ ];
45+
46+ /**
47+ * The constructor.
48+ */
2849 public function __construct ()
2950 {
3051 $ this ->converter = new ArrayValueConverter ();
31- $ this ->registerFilter ('string ' , StringFilter::class);
32- $ this ->registerFilter ('blank-to-null ' , BlankToNullFilter::class);
33- $ this ->registerFilter ('boolean ' , BooleanFilter::class);
34- $ this ->registerFilter ('integer ' , IntegerFilter::class);
35- $ this ->registerFilter ('float ' , FloatFilter::class);
36- $ this ->registerFilter ('number ' , NumberFormatFilter::class);
37- $ this ->registerFilter ('date ' , DateTimeFilter::class);
38- $ this ->registerFilter ('array ' , ArrayFilter::class);
39- $ this ->registerFilter ('callback ' , CallbackFilter::class);
52+
53+ foreach ($ this ->internalFilters as $ name => $ class ) {
54+ $ this ->registerFilter ($ name , new $ class ());
55+ }
4056 }
4157
4258 /**
43- * @param string $string
44- * @param callable|string $filter
59+ * Register custom filter.
60+ *
61+ * @param string $name The name
62+ * @param callable $filter The filter callback
4563 */
46- public function registerFilter (string $ string , $ filter ): void
64+ public function registerFilter (string $ name , callable $ filter ): void
4765 {
48- $ this ->converter ->registerFilter ($ string , $ filter );
66+ $ this ->converter ->registerFilter ($ name , $ filter );
4967 }
5068
5169 /**
52- * @param string $destination
53- * @param string $source
54- * @param ArrayTransformerRule|string|null $rule
70+ * Add mapping rule.
71+ *
72+ * @param string $destination The destination element
73+ * @param string $source The source element
74+ * @param ArrayTransformerRule|string|null $rule The rule
5575 *
56- * @return $this
76+ * @return $this The transformer
5777 */
5878 public function map (string $ destination , string $ source , $ rule = null ): self
5979 {
@@ -68,11 +88,23 @@ public function map(string $destination, string $source, $rule = null): self
6888 return $ this ;
6989 }
7090
91+ /**
92+ * Create transformer rule.
93+ *
94+ * @return ArrayTransformerRule The rule
95+ */
7196 public function rule (): ArrayTransformerRule
7297 {
7398 return new ArrayTransformerRule ();
7499 }
75100
101+ /**
102+ * Convert rule string to rule object.
103+ *
104+ * @param string $rules The rules, separated by '|'
105+ *
106+ * @return ArrayTransformerRule The rule object
107+ */
76108 private function ruleFromString (string $ rules ): ArrayTransformerRule
77109 {
78110 $ rule = $ this ->rule ();
@@ -90,10 +122,12 @@ private function ruleFromString(string $rules): ArrayTransformerRule
90122 }
91123
92124 /**
93- * @param array<mixed> $source
94- * @param array<mixed> $target
125+ * Transform array to array.
126+ *
127+ * @param array<mixed> $source The source
128+ * @param array<mixed> $target The target (optional)
95129 *
96- * @return array<mixed>
130+ * @return array<mixed> The result
97131 */
98132 public function toArray (array $ source , array $ target = []): array
99133 {
@@ -105,7 +139,7 @@ public function toArray(array $source, array $target = []): array
105139 $ value = $ this ->converter ->convert ($ value , $ rule );
106140
107141 if ($ value === null && !$ rule ->isRequired ()) {
108- // Don't add item to result
142+ // Skip item
109143 continue ;
110144 }
111145
@@ -114,4 +148,21 @@ public function toArray(array $source, array $target = []): array
114148
115149 return $ targetData ->export ();
116150 }
151+
152+ /**
153+ * Transform list of arrays to list of arrays.
154+ *
155+ * @param array<mixed> $source The source
156+ * @param array<mixed> $target The target (optional)
157+ *
158+ * @return array<mixed> The result
159+ */
160+ public function toArrays (array $ source , array $ target = []): array
161+ {
162+ foreach ($ source as $ item ) {
163+ $ target [] = $ this ->toArray ($ item );
164+ }
165+
166+ return $ target ;
167+ }
117168}
0 commit comments