Skip to content

Commit 5edf9a1

Browse files
committed
Create specific transformers
1 parent 4501cf1 commit 5edf9a1

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

FieldType/CheckboxField.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Sherlockode\ConfigurationBundle\FieldType;
44

55
use Sherlockode\ConfigurationBundle\Parameter\ParameterDefinition;
6-
use Sherlockode\ConfigurationBundle\Transformer\CallbackTransformer;
6+
use Sherlockode\ConfigurationBundle\Transformer\BooleanTransformer;
77
use Sherlockode\ConfigurationBundle\Transformer\TransformerInterface;
88
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
99

@@ -36,13 +36,6 @@ public function getName()
3636
*/
3737
public function getModelTransformer(ParameterDefinition $definition)
3838
{
39-
return new CallbackTransformer(
40-
function ($data) {
41-
return (bool)$data;
42-
},
43-
function ($data) {
44-
return $data ? 1 : 0;
45-
}
46-
);
39+
return new BooleanTransformer();
4740
}
4841
}

FieldType/ChoiceField.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Sherlockode\ConfigurationBundle\FieldType;
44

55
use Sherlockode\ConfigurationBundle\Parameter\ParameterDefinition;
6-
use Sherlockode\ConfigurationBundle\Transformer\CallbackTransformer;
6+
use Sherlockode\ConfigurationBundle\Transformer\ArrayTransformer;
77
use Sherlockode\ConfigurationBundle\Transformer\TransformerInterface;
88
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
99

@@ -37,24 +37,6 @@ public function getName()
3737
*/
3838
public function getModelTransformer(ParameterDefinition $definition)
3939
{
40-
return new CallbackTransformer(
41-
function ($data) {
42-
if (!$data) {
43-
return null;
44-
}
45-
if (false !== ($unserialized = @unserialize($data))) {
46-
return $unserialized;
47-
}
48-
49-
return $data;
50-
},
51-
function ($data) {
52-
if (is_array($data)) {
53-
return serialize($data);
54-
}
55-
56-
return $data;
57-
}
58-
);
40+
return new ArrayTransformer();
5941
}
6042
}

Transformer/ArrayTransformer.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Sherlockode\ConfigurationBundle\Transformer;
4+
5+
class ArrayTransformer implements TransformerInterface
6+
{
7+
public function transform($data)
8+
{
9+
if (!$data) {
10+
return null;
11+
}
12+
if (false !== ($unserialized = @unserialize($data))) {
13+
return $unserialized;
14+
}
15+
16+
return null;
17+
}
18+
19+
public function reverseTransform($data)
20+
{
21+
if (is_array($data)) {
22+
return serialize($data);
23+
}
24+
25+
return $data;
26+
}
27+
}

Transformer/BooleanTransformer.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Sherlockode\ConfigurationBundle\Transformer;
4+
5+
class BooleanTransformer implements TransformerInterface
6+
{
7+
public function transform($data)
8+
{
9+
return (bool)$data;
10+
}
11+
12+
public function reverseTransform($data)
13+
{
14+
return $data ? 1 : 0;
15+
}
16+
}

0 commit comments

Comments
 (0)