Passed
Push — develop ( 333d24...82ab60 )
by Mikaël
09:54
created

GeneratorOptions::instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
6
7
use InvalidArgumentException;
8
use JsonSerializable;
9
use WsdlToPhp\PackageGenerator\Model\StructValue;
10
11
/**
12
 * @method string getCategory()
13
 * @method self   setCategory(string $category)
14
 * @method string getGatherMethods()
15
 * @method self   setGatherMethods(string $gatherMethods)
16
 * @method bool   getGenerateTutorialFile()
17
 * @method self   setGenerateTutorialFile(bool $generateTutorialFile)
18
 * @method bool   getGenericConstantsNames()
19
 * @method self   setGenericConstantsNames(bool $genericConstantsNames)
20
 * @method bool   getNamespaceDictatesDirectories()
21
 * @method self   setNamespaceDictatesDirectories(bool $namespaceDictatesDirectories)
22
 * @method bool   getStandalone()
23
 * @method self   setStandalone(bool $standalone)
24
 * @method bool   getValidation()
25
 * @method self   setValidation(bool $validation)
26
 * @method string getStructClass()
27
 * @method self   setStructClass(string $structClass)
28
 * @method string getStructArrayClass()
29
 * @method self   setStructArrayClass(string $structArrayClass)
30
 * @method string getStructEnumClass()
31
 * @method self   setStructEnumClass(string $structEnumClass)
32
 * @method string getSoapClientClass()
33
 * @method self   setSoapClientClass(string $soapClientClass)
34
 * @method string getOptionNamespace()
35
 * @method self   setOptionNamespace(string $namespace)
36
 * @method string getOrigin()
37
 * @method self   setOrigin(string $origin)
38
 * @method string getDestination()
39
 * @method self   setDestination(string $destination)
40
 * @method string getSrcDirname()
41
 * @method self   setSrcDirname(string $srcDirname)
42
 * @method string getPrefix()
43
 * @method self   setPrefix(string $prefix)
44
 * @method string getSuffix()
45
 * @method self   setSuffix(string $suffix)
46
 * @method string getBasicLogin()
47
 * @method self   setBasicLogin(string $basicLogin)
48
 * @method string getBasicPassword()
49
 * @method self   setBasicPassword(string $basicPassword)
50
 * @method string getProxyHost()
51
 * @method self   setProxyHost(string $proxyHost)
52
 * @method string getProxyPort()
53
 * @method self   setProxyPort(string $proxyPort)
54
 * @method string getProxyLogin()
55
 * @method self   setProxyLogin(string $proxyLogin)
56
 * @method string getProxyPassword()
57
 * @method self   setProxyPassword(string $proxyPassword)
58
 * @method string getSoapOptions()
59
 * @method self   setSoapOptions(array $soapOptions)
60
 * @method string getComposerName()
61
 * @method self   setComposerName(string $composerName)
62
 * @method array  getComposerSettings()
63
 * @method string getStructsFolder()
64
 * @method self   setStructsFolder(string $structsFolder)
65
 * @method string getArraysFolder()
66
 * @method self   setArraysFolder(string $arraysFolder)
67
 * @method string getEnumsFolder()
68
 * @method self   setEnumsFolder(string $enumsFolder)
69
 * @method string getServicesFolder()
70
 * @method self   setServicesFolder(string $servicesFolder)
71
 * @method bool   getSchemasSave()
72
 * @method self   setSchemasSave(bool $saveSchemas)
73
 * @method string getSchemasFolder()
74
 * @method self   setSchemasFolder(string $schemasFolder)
75
 * @method string getXsdTypesPath()
76
 * @method self   setXsdTypesPath(string $xsdTypesPath)
77
 */
78
final class GeneratorOptions extends AbstractYamlReader implements JsonSerializable
79
{
80
    /**
81
     * Common values used as option's value.
82
     */
83
    public const VALUE_CAT = 'cat';
84
    public const VALUE_END = 'end';
85
    public const VALUE_FALSE = false;
86
    public const VALUE_NONE = 'none';
87
    public const VALUE_START = 'start';
88
    public const VALUE_TRUE = true;
89
90
    /**
91
     * Possible option keys.
92
     */
93
    public const ADD_COMMENTS = 'add_comments';
94
    public const ARRAYS_FOLDER = 'arrays_folder';
95
    public const BASIC_LOGIN = 'basic_login';
96
    public const BASIC_PASSWORD = 'basic_password';
97
    public const CATEGORY = 'category';
98
    public const COMPOSER_NAME = 'composer_name';
99
    public const COMPOSER_SETTINGS = 'composer_settings';
100
    public const DESTINATION = 'destination';
101
    public const ENUMS_FOLDER = 'enums_folder';
102
    public const GATHER_METHODS = 'gather_methods';
103
    public const GENERATE_TUTORIAL_FILE = 'generate_tutorial_file';
104
    public const GENERIC_CONSTANTS_NAME = 'generic_constants_names';
105
    public const NAMESPACE_PREFIX = 'namespace_prefix';
106
    public const NAMESPACE_DICTATES_DIRECTORIES = 'namespace_dictates_directories';
107
    public const ORIGIN = 'origin';
108
    public const PREFIX = 'prefix';
109
    public const PROXY_HOST = 'proxy_host';
110
    public const PROXY_LOGIN = 'proxy_login';
111
    public const PROXY_PASSWORD = 'proxy_password';
112
    public const PROXY_PORT = 'proxy_port';
113
    public const SERVICES_FOLDER = 'services_folder';
114
    public const SOAP_CLIENT_CLASS = 'soap_client_class';
115
    public const SOAP_OPTIONS = 'soap_options';
116
    public const SRC_DIRNAME = 'src_dirname';
117
    public const STANDALONE = 'standalone';
118
    public const STRUCT_ARRAY_CLASS = 'struct_array_class';
119
    public const STRUCT_ENUM_CLASS = 'struct_enum_class';
120
    public const STRUCT_CLASS = 'struct_class';
121
    public const STRUCTS_FOLDER = 'structs_folder';
122
    public const SUFFIX = 'suffix';
123
    public const VALIDATION = 'validation';
124
    public const SCHEMAS_SAVE = 'schemas_save';
125
    public const SCHEMAS_FOLDER = 'schemas_folder';
126
    public const XSD_TYPES_PATH = 'xsd_types_path';
127
128
    protected array $options;
129
130 20
    protected function __construct(string $filename)
131
    {
132 20
        $this->options = [];
133 20
        $this->parseOptions($filename);
134
    }
135
136 612
    public static function instance(?string $filename = null): self
137
    {
138 612
        return parent::instance($filename);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::instance($filename) returns the type WsdlToPhp\PackageGenerat...ader\AbstractYamlReader which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerat...Reader\GeneratorOptions.
Loading history...
139
    }
140
141 776
    public function __call($name, $arguments)
142
    {
143 776
        if ('set' === substr($name, 0, 3) && 1 === count($arguments)) {
144 424
            return $this->setOptionValue(self::methodNameToOptionName($name), array_shift($arguments));
145
        }
146 772
        if ('get' === substr($name, 0, 3) && empty($arguments)) {
147 772
            return $this->getOptionValue(self::methodNameToOptionName($name));
148
        }
149
150
        throw new \BadMethodCallException(sprintf('Method %s undefined', $name));
151
    }
152
153 802
    public function getOptionValue(string $optionName)
154
    {
155 802
        if (!array_key_exists($optionName, $this->options)) {
156 2
            throw new InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__);
157
        }
158
159 800
        return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default'];
160
    }
161
162 618
    public function setOptionValue(string $optionName, $optionValue, array $values = []): self
163
    {
164 618
        if (!array_key_exists($optionName, $this->options)) {
165 4
            $this->options[$optionName] = [
166
                'value' => $optionValue,
167
                'values' => $values,
168
            ];
169 616
        } elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) {
170 4
            throw new InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
171
        } else {
172 612
            $this->options[$optionName]['value'] = $optionValue;
173
        }
174
175 614
        return $this;
176
    }
177
178 238
    public static function getDefaultConfigurationPath(): string
179
    {
180 238
        return __DIR__.'/../resources/config/generator_options.yml';
181
    }
182
183 776
    public static function methodNameToOptionName(string $name): string
184
    {
185 776
        return trim(strtolower(preg_replace(StructValue::MATCH_PATTERN, StructValue::REPLACEMENT_PATTERN, substr($name, 3))), '_');
186
    }
187
188 18
    public function setAddComments(array $addComments = []): self
189
    {
190
        /**
191
         * If array is type array("author:john Doe","Release:1",).
192
         */
193 18
        $comments = [];
194 18
        foreach ($addComments as $index => $value) {
195 8
            if (is_numeric($index) && mb_strpos($value, ':') > 0) {
196 2
                [$tag, $val] = explode(':', $value);
197 2
                $comments[$tag] = $val;
198
            } else {
199 6
                $comments[$index] = $value;
200
            }
201
        }
202
203 18
        return $this->setOptionValue(self::ADD_COMMENTS, $comments);
204
    }
205
206 248
    public function getNamespace(): string
207
    {
208 248
        return $this->getOptionValue(self::NAMESPACE_PREFIX);
209
    }
210
211 30
    public function setNamespace(string $namespace): self
212
    {
213 30
        return $this->setOptionValue(self::NAMESPACE_PREFIX, $namespace);
214
    }
215
216 16
    public function setComposerSettings(array $composerSettings = []): self
217
    {
218
        /**
219
         * If array is type array("config.value:true","require:library/src",).
220
         */
221 16
        $settings = [];
222 16
        foreach ($composerSettings as $index => $value) {
223 14
            if (is_numeric($index) && mb_strpos($value, ':') > 0) {
224 14
                $path = implode('', array_slice(explode(':', $value), 0, 1));
225 14
                $val = implode(':', array_slice(explode(':', $value), 1));
226 14
                self::dotNotationToArray($path, $val, $settings);
227
            } else {
228 2
                $settings[$index] = $value;
229
            }
230
        }
231
232 16
        return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings);
233
    }
234
235 18
    public function toArray(): array
236
    {
237 18
        $options = [];
238 18
        foreach (array_keys($this->options) as $name) {
239 18
            $options[$name] = $this->getOptionValue($name);
240
        }
241
242 18
        return $options;
243
    }
244
245 4
    public function jsonSerialize(): array
246
    {
247 4
        return $this->toArray();
248
    }
249
250 20
    protected function parseOptions(string $filename): self
251
    {
252 20
        $options = $this->loadYaml($filename);
253 20
        if (is_array($options)) {
254 18
            $this->options = $options;
255
        } else {
256 2
            throw new InvalidArgumentException(sprintf('Settings contained by "%s" are not valid as the settings are not contained by an array: "%s"', $filename, gettype($options)), __LINE__);
257
        }
258
259 18
        return $this;
260
    }
261
262
    /**
263
     * turns my.key.path to array('my' => array('key' => array('path' => $value))).
264
     *
265
     * @param mixed $value
266
     */
267 14
    protected static function dotNotationToArray(string $string, $value, array &$array)
268
    {
269 14
        $keys = explode('.', $string);
270 14
        foreach ($keys as $key) {
271 14
            $array = &$array[$key];
272
        }
273 14
        $array = ('true' === $value || 'false' === $value) ? 'true' === $value : $value;
274
    }
275
}
276