File size: 7,157 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

namespace Mautic\CoreBundle\Helper;

use Mautic\CoreBundle\Helper\ListParser\ArrayListParser;
use Mautic\CoreBundle\Helper\ListParser\BarListParser;
use Mautic\CoreBundle\Helper\ListParser\Exception\FormatNotSupportedException;
use Mautic\CoreBundle\Helper\ListParser\JsonListParser;
use Mautic\CoreBundle\Helper\ListParser\ListParserInterface;
use Mautic\CoreBundle\Helper\ListParser\ValueListParser;
use Mautic\CoreBundle\Translation\Translator;

abstract class AbstractFormFieldHelper
{
    /**
     * Json encoded format.
     */
    public const FORMAT_JSON = 'json';

    /**
     * Bar format value1|value2.
     */
    public const FORMAT_BAR = 'bar';

    /**
     * Simple value => label array.
     */
    public const FORMAT_SIMPLE_ARRAY = 'simple_array';

    /**
     * Array [['value' => 'value', 'label' => 'label'] ..].
     */
    public const FORMAT_ARRAY = 'array';

    /**
     * @var string
     */
    protected $translationKeyPrefix;

    /**
     * @var Translator
     */
    protected $translator;

    /**
     * @return mixed
     */
    abstract public function setTranslationKeyPrefix();

    /**
     * @return mixed
     */
    abstract public function getTypes();

    public function __construct()
    {
        $this->setTranslationKeyPrefix();
    }

    public function setTranslator(Translator $translator): void
    {
        $this->translator = $translator;
    }

    /**
     * @param array $customFields
     *
     * @return array
     */
    public function getChoiceList($customFields = [])
    {
        $choices = [];

        foreach ($this->getTypes() as $v => $type) {
            $choices[$this->translator->transConditional("mautic.core.type.{$v}", "{$this->translationKeyPrefix}{$v}")] = $v;
        }

        foreach ($customFields as $v => $f) {
            $choices[$this->translator->trans($f['label'])] = $v;
        }

        ksort($choices);

        return $choices;
    }

    /**
     * Format a string into an array.
     *
     * @param mixed $list                      List to parse
     * @param bool  $removeEmpty               @deprecated Kept for BC with method signature
     * @param bool  $deprecatedIgnoreNumerical @deprecated Flag was introduced to support boolean choice lists; use parseBooleanList instead
     *
     * @return array
     */
    public static function parseList($list, $removeEmpty = true, $deprecatedIgnoreNumerical = false)
    {
        if ($deprecatedIgnoreNumerical) {
            // BC support for support
            return static::parseBooleanList($list);
        }

        return static::parseChoiceList(
            self::parseListsWithParsers(
                $list,
                [
                    new JsonListParser(),
                    new BarListParser(),
                    new ValueListParser(),
                    new ArrayListParser(),
                ]
            )
        );
    }

    /**
     * Same as parseList method above but it will return labels as keys.
     *
     * @param mixed $list
     *
     * @return mixed[]
     */
    public static function parseListForChoices($list): array
    {
        return static::parseChoiceList(
            self::parseListsWithParsers(
                $list,
                [
                    new JsonListParser(),
                    new BarListParser(),
                    new ValueListParser(),
                    new ArrayListParser(),
                ]
            ),
            true
        );
    }

    /**
     * @param mixed $list
     *
     * @return mixed[]
     */
    public static function parseBooleanList($list): array
    {
        return static::parseChoiceList(
            self::parseListsWithParsers(
                $list,
                [
                    new JsonListParser(),
                    new BarListParser(),
                    new ValueListParser(),
                ]
            )
        );
    }

    /**
     * @return mixed[]|string
     */
    public static function formatList($format, $choices)
    {
        switch ($format) {
            case self::FORMAT_JSON:
                return json_encode($choices);
            case self::FORMAT_BAR:
                return implode('|', $choices);
            case self::FORMAT_SIMPLE_ARRAY:
                if (isset($choices[0]) && isset($choices[0]['label'])) {
                    $array = [];

                    foreach ($choices as $choice) {
                        $array[$choice['value']] = $choice['label'];
                    }

                    return $array;
                }

                return $choices;
            case self::FORMAT_ARRAY:
                $array = [];
                foreach ($choices as $value => $label) {
                    $array[] = [
                        'label' => $label,
                        'value' => $value,
                    ];
                }

                return $array;
        }
    }

    protected static function parseChoiceList(array $list, bool $labelsAsKeys = false)
    {
        $choices = [];
        foreach ($list as $value => $label) {
            if (is_array($label) && array_key_exists('value', $label)) {
                $value = $label['value'];
                $label = $label['label'];

                if ('' === $value || null === $value) {
                    // Value is empty which can't work as a key
                    continue;
                }

                $choices = self::appendChoice($choices, $label, $value, $labelsAsKeys);

                continue;
            }

            if (('' === $label || null === $label) && ('' === $value || null === $value)) {
                // Both label and value are empty which can't work as choices
                continue;
            }

            if (is_array($label)) {
                // Process the label as an array as this is likely an option group
                $key = trim(html_entity_decode($value, ENT_QUOTES));

                $choices[$key] = static::parseChoiceList($label);
                continue;
            }

            $choices = self::appendChoice($choices, $label, $value, $labelsAsKeys);
        }

        return $choices;
    }

    /**
     * @param mixed[] $choices
     *
     * @return mixed[]
     */
    private static function appendChoice(array $choices, string $label, string $value, bool $labelsAsKeys = false): array
    {
        $label = trim(html_entity_decode($label, ENT_QUOTES));
        $value = trim(html_entity_decode($value, ENT_QUOTES));

        if ($labelsAsKeys) {
            $choices[$label] = $value;
        } else {
            $choices[$value] = $label;
        }

        return $choices;
    }

    /**
     * @param mixed                 $list
     * @param ListParserInterface[] $parsers
     *
     * @return mixed[]
     */
    private static function parseListsWithParsers($list, array $parsers): array
    {
        foreach ($parsers as $parser) {
            try {
                $list = $parser->parse($list);
            } catch (FormatNotSupportedException) {
                continue;
            }
        }

        return $list;
    }
}