Passed
Push — master ( be610c...3593e9 )
by Andreas
25:04
created

multipleTransformer   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 77.35%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 102
ccs 41
cts 53
cp 0.7735
rs 10
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A get_imploded_options() 0 15 3
C transform() 0 34 12
B reverseTransform() 0 27 7
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension\transformer;
7
8
use Symfony\Component\Form\DataTransformerInterface;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
use midcom_error;
11
12
/**
13
 * Experimental jsdate transformer
14
 */
15
class multipleTransformer implements DataTransformerInterface
16
{
17
    private array $config;
18
19
    private string $multiple_separator = '|';
20
21
    private string $multiple_storagemode = 'serialized';
22
23 35
    public function __construct(array $config)
24
    {
25 35
        $this->config = $config;
26 35
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
27 34
            $this->multiple_storagemode = $this->config['type_config']['multiple_storagemode'];
28
        }
29
    }
30
31 31
    public function transform($input)
32
    {
33 31
        if (in_array($input, [false, null], true)) {
34 28
            return [];
35
        }
36
37 20
        switch ($this->multiple_storagemode) {
38 20
            case 'serialized':
39 16
            case 'array':
40 4
                if (empty($input)) {
41 1
                    return [];
42
                }
43
44 3
                return unserialize($input);
45
46 16
            case 'imploded':
47
                if (!is_string($input)) {
48
                    return [];
49
                }
50
                return explode($this->multiple_separator, $input);
51
52 16
            case 'imploded_wrapped':
53 16
                if (!is_string($input) || substr($input, 1, -1) == '') {
54 15
                    return [];
55
                }
56 1
                $results = explode($this->multiple_separator, substr($input, 1, -1));
57 1
                if (   !empty($this->config['widget_config']['id_field'])
58 1
                    && $this->config['widget_config']['id_field'] == 'id') {
59
                    $results = array_map('intval', $results);
60
                }
61 1
                return $results;
62
63
            default:
64
                throw new midcom_error("The multiple_storagemode '{$this->multiple_storagemode}' is invalid, cannot continue.");
65
        }
66
    }
67
68 5
    public function reverseTransform($array)
69
    {
70 5
        if (!is_array($array) ) {
71
            throw new TransformationFailedException('Expected an array.');
72
        }
73
74 5
        if (empty($array)) {
75 3
            return;
76
        }
77
78 2
        switch ($this->multiple_storagemode) {
79 2
            case 'array':
80
                return $array;
81
82 2
            case 'serialized':
83 1
                return serialize($array);
84
85 1
            case 'imploded':
86
                return $this->get_imploded_options($array);
87
88 1
            case 'imploded_wrapped':
89 1
                $glue = $this->multiple_separator;
90 1
                $options = $this->get_imploded_options($array);
91 1
                return "{$glue}{$options}{$glue}";
92
93
            default:
94
                throw new TransformationFailedException('Invalid storage mode ' . $this->multiple_storagemode);
95
        }
96
    }
97
98
    /**
99
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
100
     * will be logged and skipped silently.
101
     */
102 1
    private function get_imploded_options(array $array) : string
103
    {
104 1
        $glue = $this->multiple_separator;
105
106 1
        $result = [];
107 1
        foreach ($array as $key) {
108 1
            if (str_contains($key, $glue)) {
109
                debug_add("The option key '{$key}' contained the multiple separator ($this->multiple_storagemode) char, which is not allowed for imploded storage targets. ignoring silently.",
110
                MIDCOM_LOG_WARN);
111
                continue;
112
            }
113
114 1
            $result[] = $key;
115
        }
116 1
        return implode($glue, $result);
117
    }
118
}
119