Passed
Push — feature/issue-262 ( cafbaa...b58e35 )
by Mikaël
07:31
created

GeneratorOptions::getDefaultConfigurationPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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