Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
OptionFactory
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
2 / 2
22
100.00% covered (success)
100.00%
1 / 1
 createOption
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
19
 createOptions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace WebPConvert\Options;
4
5use WebPConvert\Options\ArrayOption;
6use WebPConvert\Options\BooleanOption;
7use WebPConvert\Options\IntegerOption;
8use WebPConvert\Options\IntegerOrNullOption;
9use WebPConvert\Options\MetadataOption;
10use WebPConvert\Options\StringOption;
11use WebPConvert\Options\SensitiveStringOption;
12use WebPConvert\Options\QualityOption;
13
14/**
15 * Abstract option class
16 *
17 * @package    WebPConvert
18 * @author     Bjørn Rosell <it@rosell.dk>
19 * @since      Class available since Release 2.7.0
20 */
21class OptionFactory
22{
23
24    public static function createOption($optionName, $optionType, $def)
25    {
26        $option = null;
27        switch ($optionType) {
28            case 'int':
29                $minValue = (isset($def['minimum']) ? $def['minimum'] : null);
30                $maxValue = (isset($def['maximum']) ? $def['maximum'] : null);
31                unset($def['minimum']);
32                unset($def['maximum']);
33                if (isset($def['allow-null']) && $def['allow-null']) {
34                    $option = new IntegerOrNullOption($optionName, $def['default'], $minValue, $maxValue);
35                } else {
36                    if ($optionName == 'quality') {
37                        $option = new QualityOption($optionName, $def['default']);
38                    } else {
39                        $option = new IntegerOption($optionName, $def['default'], $minValue, $maxValue);
40                    }
41                }
42                break;
43
44            case 'string':
45                if ($optionName == 'metadata') {
46                    $option = new MetadataOption($optionName, $def['default']);
47                } else {
48                    $enum = (isset($def['enum']) ? $def['enum'] : null);
49                    if (isset($def['sensitive']) && ($def['sensitive'] == true)) {
50                        unset($def['sensitive']);
51                        $option = new SensitiveStringOption($optionName, $def['default'], $enum);
52                    } else {
53                        $option = new StringOption($optionName, $def['default'], $enum);
54                    }
55                }
56                break;
57
58            case 'boolean':
59                $option = new BooleanOption($optionName, $def['default']);
60                break;
61
62            case 'array':
63                if (isset($def['sensitive']) && ($def['sensitive'] == true)) {
64                    $option = new SensitiveArrayOption($optionName, $def['default']);
65                } else {
66                    $option = new ArrayOption($optionName, $def['default']);
67                }
68                break;
69        }
70        unset($def['default']);
71
72        if (!is_null($option)) {
73            if (isset($def['deprecated'])) {
74                $option->markDeprecated();
75            }
76            if (isset($def['ui'])) {
77                $option->setUI($def['ui']);
78                unset($def['ui']);
79            }
80        }
81        $option->setExtraSchemaDefs($def);
82        return $option;
83    }
84
85    public static function createOptions($def)
86    {
87        $result = [];
88        foreach ($def as $i => list($optionName, $optionType, $optionDef)) {
89            $option = self::createOption($optionName, $optionType, $optionDef);
90            if (!is_null($option)) {
91                $result[] = $option;
92            }
93        }
94        return $result;
95    }
96}