Completed
Push — 2.x ( b90950...037224 )
by Mikaël
63:07 queued 32:02
created

GeneratorOptions::dotNotationToArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.2
cc 4
eloc 5
nc 8
nop 3
crap 4
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
4
5
class GeneratorOptions extends AbstractYamlReader
6
{
7
    /**
8
     * Common values used as option's value
9
     */
10
    const VALUE_CAT = 'cat';
11
    const VALUE_END = 'end';
12
    const VALUE_FALSE = false;
13
    const VALUE_NONE = 'none';
14
    const VALUE_START = 'start';
15
    const VALUE_TRUE = true;
16
    /**
17
     * Possible option keys
18
     * @var string
19
     */
20
    const ADD_COMMENTS = 'add_comments';
21
    const ARRAYS_FOLDER = 'arrays_folder';
22
    const BASIC_LOGIN = 'basic_login';
23
    const BASIC_PASSWORD = 'basic_password';
24
    const CATEGORY = 'category';
25
    const COMPOSER_NAME = 'composer_name';
26
    const COMPOSER_SETTINGS = 'composer_settings';
27
    const DESTINATION = 'destination';
28
    const ENUMS_FOLDER = 'enums_folder';
29
    const GATHER_METHODS = 'gather_methods';
30
    const GENERATE_TUTORIAL_FILE = 'generate_tutorial_file';
31
    const GENERIC_CONSTANTS_NAME = 'generic_constants_names';
32
    const NAMESPACE_PREFIX = 'namespace_prefix';
33
    const ORIGIN = 'origin';
34
    const PREFIX = 'prefix';
35
    const PROXY_HOST = 'proxy_host';
36
    const PROXY_LOGIN = 'proxy_login';
37
    const PROXY_PASSWORD = 'proxy_password';
38
    const PROXY_PORT = 'proxy_port';
39
    const SERVICES_FOLDER = 'services_folder';
40
    const SOAP_CLIENT_CLASS = 'soap_client_class';
41
    const SOAP_OPTIONS = 'soap_options';
42
    const STANDALONE = 'standalone';
43
    const STRUCT_ARRAY_CLASS = 'struct_array_class';
44
    const STRUCT_CLASS = 'struct_class';
45
    const STRUCTS_FOLDER = 'structs_folder';
46
    const SUFFIX = 'suffix';
47
    const VALIDATION = 'validation';
48
    /**
49
     * Generator's options
50
     * @var array
51
     */
52
    protected $options;
53
    /**
54
     * @param string $filename
55 36
     */
56
    protected function __construct($filename)
57 36
    {
58 36
        $this->options = array();
59 32
        $this->parseOptions($filename);
60
    }
61
    /**
62
     * Parse options for generator
63
     * @param string $filename options's file to parse
64
     * @return GeneratorOptions
65 36
     */
66
    protected function parseOptions($filename)
67 36
    {
68 36
        $options = $this->loadYaml($filename);
69 32
        if (is_array($options)) {
70 24
            $this->options = $options;
71 4
        } else {
72
            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__);
73 32
        }
74
        return $this;
75
    }
76
    /**
77
     * Returns the option value
78
     * @throws InvalidArgumentException
79
     * @param string $optionName
80
     * @return mixed
81 1044
     */
82
    public function getOptionValue($optionName)
83 1044
    {
84 4
        if (!isset($this->options[$optionName])) {
85
            throw new \InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__);
86 1040
        }
87
        return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default'];
88
    }
89
    /**
90
     * Allows to add an option and set its value
91
     * @throws InvalidArgumentException
92
     * @param string $optionName
93
     * @return GeneratorOptions
94 788
     */
95
    public function setOptionValue($optionName, $optionValue, array $values = array())
96 788
    {
97 4
        if (!isset($this->options[$optionName])) {
98 4
            $this->options[$optionName] = array(
99 4
                'value' => $optionValue,
100
                'values' => $values,
101 787
            );
102 8
        } elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) {
103
            throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
104 776
        } else {
105
            $this->options[$optionName]['value'] = $optionValue;
106 780
        }
107
        return $this;
108
    }
109
    /**
110
     * @throws \InvalidArgumentException
111
     * @param string options's file to parse
112
     * @return GeneratorOptions
113 772
     */
114
    public static function instance($filename = null)
115 772
    {
116
        return parent::instance(empty($filename) ? self::getDefaultConfigurationPath() : $filename);
117
    }
118
    /**
119
     * @return string
120 60
     */
121
    public static function getDefaultConfigurationPath()
122 60
    {
123
        return dirname(__FILE__) . '/../resources/config/generator_options.yml';
124
    }
125
    /**
126
     * Get category option value
127
     * @return string
128 276
     */
129
    public function getCategory()
130 276
    {
131
        return $this->getOptionValue(self::CATEGORY);
132
    }
133
    /**
134
     * Set current category option value
135
     * @throws \InvalidArgumentException
136
     * @param string $category
137
     * @return GeneratorOptions
138 292
     */
139
    public function setCategory($category)
140 292
    {
141
        return $this->setOptionValue(self::CATEGORY, $category);
142
    }
143
    /**
144
     * Get add comments option value
145
     * @return array
146 216
     */
147
    public function getAddComments()
148 216
    {
149
        return $this->getOptionValue(self::ADD_COMMENTS);
150
    }
151
    /**
152
     * Set current add comments option value
153
     * @throws \InvalidArgumentException
154
     * @param array $addComments
155
     * @return GeneratorOptions
156 332
     */
157
    public function setAddComments(array $addComments = array())
158
    {
159
        /**
160
         * If array is type array("author:john Doe","Release:1",)
161 332
         */
162 332
        $comments = array();
163 276
        foreach ($addComments as $index=>$value) {
164 4
            if (is_numeric($index) && strpos($value, ':') > 0) {
165 4
                list($tag, $val) = explode(':', $value);
166 3
                $comments[$tag] = $val;
167 273
            } else {
168
                $comments[$index] = $value;
169 249
            }
170 332
        }
171
        return $this->setOptionValue(self::ADD_COMMENTS, $comments);
172
    }
173
    /**
174
     * Get gather methods option value
175
     * @return string
176 476
     */
177
    public function getGatherMethods()
178 476
    {
179
        return $this->getOptionValue(self::GATHER_METHODS);
180
    }
181
    /**
182
     * Set current gather methods option value
183
     * @throws \InvalidArgumentException
184
     * @param string $gatherMethods
185
     * @return GeneratorOptions
186 292
     */
187
    public function setGatherMethods($gatherMethods)
188 292
    {
189
        return $this->setOptionValue(self::GATHER_METHODS, $gatherMethods);
190
    }
191
    /**
192
     * Get generate tutorial file option value
193
     * @return bool
194 36
     */
195
    public function getGenerateTutorialFile()
196 36
    {
197
        return $this->getOptionValue(self::GENERATE_TUTORIAL_FILE);
198
    }
199
    /**
200
     * Set current generate tutorial file option value
201
     * @throws \InvalidArgumentException
202
     * @param bool $generateTutorialFile
203
     * @return GeneratorOptions
204 56
     */
205
    public function setGenerateTutorialFile($generateTutorialFile)
206 56
    {
207
        return $this->setOptionValue(self::GENERATE_TUTORIAL_FILE, $generateTutorialFile);
208
    }
209
    /**
210
     * Get namespace option value
211
     * @return string
212 252
     */
213
    public function getNamespace()
214 252
    {
215
        return $this->getOptionValue(self::NAMESPACE_PREFIX);
216
    }
217
    /**
218
     * Set current namespace option value
219
     * @throws \InvalidArgumentException
220
     * @param string $namespace
221
     * @return GeneratorOptions
222 36
     */
223
    public function setNamespace($namespace)
224 36
    {
225
        return $this->setOptionValue(self::NAMESPACE_PREFIX, $namespace);
226
    }
227
    /**
228
     * Get generic constants name option value
229
     * @return bool
230 68
     */
231
    public function getGenericConstantsName()
232 68
    {
233
        return $this->getOptionValue(self::GENERIC_CONSTANTS_NAME);
234
    }
235
    /**
236
     * Set current generic constants name option value
237
     * @throws \InvalidArgumentException
238
     * @param bool $genericConstantsName
239
     * @return GeneratorOptions
240 56
     */
241
    public function setGenericConstantsName($genericConstantsName)
242 56
    {
243
        return $this->setOptionValue(self::GENERIC_CONSTANTS_NAME, $genericConstantsName);
244
    }
245
    /**
246
     * Get standalone option value
247
     * @return bool
248 68
     */
249
    public function getStandalone()
250 68
    {
251
        return $this->getOptionValue(self::STANDALONE);
252
    }
253
    /**
254
     * Set current standalone option value
255
     * @throws \InvalidArgumentException
256
     * @param bool $standalone
257
     * @return GeneratorOptions
258 32
     */
259
    public function setStandalone($standalone)
260 32
    {
261
        return $this->setOptionValue(self::STANDALONE, $standalone);
262
    }
263
    /**
264
     * Get validation option value
265
     * @return bool
266 120
     */
267
    public function getValidation()
268 120
    {
269
        return $this->getOptionValue(self::VALIDATION);
270
    }
271
    /**
272
     * Set current validation option value
273
     * @throws \InvalidArgumentException
274
     * @param bool $validation
275
     * @return GeneratorOptions
276 16
     */
277
    public function setValidation($validation)
278 16
    {
279
        return $this->setOptionValue(self::VALIDATION, $validation);
280
    }
281
    /**
282
     * Get struct class option value
283
     * @return string
284 100
     */
285
    public function getStructClass()
286 100
    {
287
        return $this->getOptionValue(self::STRUCT_CLASS);
288
    }
289
    /**
290
     * Set current struct class option value
291
     * @throws \InvalidArgumentException
292
     * @param string $structClass
293
     * @return GeneratorOptions
294 36
     */
295
    public function setStructClass($structClass)
296 36
    {
297
        return $this->setOptionValue(self::STRUCT_CLASS, $structClass);
298
    }
299
    /**
300
     * Get struct array class option value
301
     * @return string
302 44
     */
303
    public function getStructArrayClass()
304 44
    {
305
        return $this->getOptionValue(self::STRUCT_ARRAY_CLASS);
306
    }
307
    /**
308
     * Set current struct array class option value
309
     * @throws \InvalidArgumentException
310
     * @param string $structArrayClass
311
     * @return GeneratorOptions
312 28
     */
313
    public function setStructArrayClass($structArrayClass)
314 28
    {
315
        return $this->setOptionValue(self::STRUCT_ARRAY_CLASS, $structArrayClass);
316
    }
317
    /**
318
     * Get struct array class option value
319
     * @return string
320 100
     */
321
    public function getSoapClientClass()
322 100
    {
323
        return $this->getOptionValue(self::SOAP_CLIENT_CLASS);
324
    }
325
    /**
326
     * Set current struct array class option value
327
     * @throws \InvalidArgumentException
328
     * @param string $soapClientClass
329
     * @return GeneratorOptions
330 28
     */
331
    public function setSoapClientClass($soapClientClass)
332 28
    {
333
        return $this->setOptionValue(self::SOAP_CLIENT_CLASS, $soapClientClass);
334
    }
335
    /**
336
     * Get origin option value
337
     * @return string
338 516
     */
339
    public function getOrigin()
340 516
    {
341
        return $this->getOptionValue(self::ORIGIN);
342
    }
343
    /**
344
     * Set current origin option value
345
     * @throws \InvalidArgumentException
346
     * @param string $origin
347
     * @return GeneratorOptions
348 524
     */
349
    public function setOrigin($origin)
350 524
    {
351
        return $this->setOptionValue(self::ORIGIN, $origin);
352
    }
353
    /**
354
     * Get destination option value
355
     * @return string
356 292
     */
357
    public function getDestination()
358 292
    {
359
        return $this->getOptionValue(self::DESTINATION);
360
    }
361
    /**
362
     * Set current destination option value
363
     * @throws \InvalidArgumentException
364
     * @param string $destination
365
     * @return GeneratorOptions
366 528
     */
367
    public function setDestination($destination)
368 528
    {
369
        return $this->setOptionValue(self::DESTINATION, $destination);
370
    }
371
    /**
372
     * Get prefix option value
373
     * @return string
374 532
     */
375
    public function getPrefix()
376 532
    {
377
        return $this->getOptionValue(self::PREFIX);
378
    }
379
    /**
380
     * Set current prefix option value
381
     * @throws \InvalidArgumentException
382
     * @param string $prefix
383
     * @return GeneratorOptions
384 308
     */
385
    public function setPrefix($prefix)
386 308
    {
387
        return $this->setOptionValue(self::PREFIX, $prefix);
388
    }
389
    /**
390
     * Get suffix option value
391
     * @return string
392 520
     */
393
    public function getSuffix()
394 520
    {
395
        return $this->getOptionValue(self::SUFFIX);
396
    }
397
    /**
398
     * Set current suffix option value
399
     * @throws \InvalidArgumentException
400
     * @param string $suffix
401
     * @return GeneratorOptions
402 52
     */
403
    public function setSuffix($suffix)
404 52
    {
405
        return $this->setOptionValue(self::SUFFIX, $suffix);
406
    }
407
    /**
408
     * Get basic login option value
409
     * @return string
410 520
     */
411
    public function getBasicLogin()
412 520
    {
413
        return $this->getOptionValue(self::BASIC_LOGIN);
414
    }
415
    /**
416
     * Set current basic login option value
417
     * @throws \InvalidArgumentException
418
     * @param string $basicLogin
419
     * @return GeneratorOptions
420 28
     */
421
    public function setBasicLogin($basicLogin)
422 28
    {
423
        return $this->setOptionValue(self::BASIC_LOGIN, $basicLogin);
424
    }
425
    /**
426
     * Get basic password option value
427
     * @return string
428 520
     */
429
    public function getBasicPassword()
430 520
    {
431
        return $this->getOptionValue(self::BASIC_PASSWORD);
432
    }
433
    /**
434
     * Set current basic password option value
435
     * @throws \InvalidArgumentException
436
     * @param string $basicPassword
437
     * @return GeneratorOptions
438 28
     */
439
    public function setBasicPassword($basicPassword)
440 28
    {
441
        return $this->setOptionValue(self::BASIC_PASSWORD, $basicPassword);
442
    }
443
    /**
444
     * Get basic proxy host option value
445
     * @return string
446 520
     */
447
    public function getProxyHost()
448 520
    {
449
        return $this->getOptionValue(self::PROXY_HOST);
450
    }
451
    /**
452
     * Set current proxy host option value
453
     * @throws \InvalidArgumentException
454
     * @param string $proxyHost
455
     * @return GeneratorOptions
456 28
     */
457
    public function setProxyHost($proxyHost)
458 28
    {
459
        return $this->setOptionValue(self::PROXY_HOST, $proxyHost);
460
    }
461
    /**
462
     * Get basic proxy port option value
463
     * @return string
464 520
     */
465
    public function getProxyPort()
466 520
    {
467
        return $this->getOptionValue(self::PROXY_PORT);
468
    }
469
    /**
470
     * Set current proxy port option value
471
     * @throws \InvalidArgumentException
472
     * @param string $proxyPort
473
     * @return GeneratorOptions
474 28
     */
475
    public function setProxyPort($proxyPort)
476 28
    {
477
        return $this->setOptionValue(self::PROXY_PORT, $proxyPort);
478
    }
479
    /**
480
     * Get basic proxy login option value
481
     * @return string
482 520
     */
483
    public function getProxyLogin()
484 520
    {
485
        return $this->getOptionValue(self::PROXY_LOGIN);
486
    }
487
    /**
488
     * Set current proxy login option value
489
     * @throws \InvalidArgumentException
490
     * @param string $proxyLogin
491
     * @return GeneratorOptions
492 28
     */
493
    public function setProxyLogin($proxyLogin)
494 28
    {
495
        return $this->setOptionValue(self::PROXY_LOGIN, $proxyLogin);
496
    }
497
    /**
498
     * Get basic proxy password option value
499
     * @return string
500 520
     */
501
    public function getProxyPassword()
502 520
    {
503
        return $this->getOptionValue(self::PROXY_PASSWORD);
504
    }
505
    /**
506
     * Set current proxy password option value
507
     * @throws \InvalidArgumentException
508
     * @param string $proxyPassword
509
     * @return GeneratorOptions
510 28
     */
511
    public function setProxyPassword($proxyPassword)
512 28
    {
513
        return $this->setOptionValue(self::PROXY_PASSWORD, $proxyPassword);
514
    }
515
    /**
516
     * Get basic soap options option value
517
     * @return array
518 512
     */
519
    public function getSoapOptions()
520 512
    {
521
        return $this->getOptionValue(self::SOAP_OPTIONS);
522
    }
523
    /**
524
     * Set current soap options option value
525
     * @throws \InvalidArgumentException
526
     * @param array $soapOptions
527
     * @return GeneratorOptions
528 32
     */
529
    public function setSoapOptions(array $soapOptions)
530 32
    {
531
        return $this->setOptionValue(self::SOAP_OPTIONS, $soapOptions);
532
    }
533
    /**
534
     * Get composer name option value
535
     * @return string
536 44
     */
537
    public function getComposerName()
538 44
    {
539
        return $this->getOptionValue(self::COMPOSER_NAME);
540
    }
541
    /**
542
     * Set current composer name option value
543
     * @throws \InvalidArgumentException
544
     * @param string $composerName
545
     * @return GeneratorOptions
546 68
     */
547
    public function setComposerName($composerName)
548 68
    {
549
        return $this->setOptionValue(self::COMPOSER_NAME, $composerName);
550
    }
551
    /**
552
     * Get composer settings option value
553
     * @return string
554 260
     */
555
    public function getComposerSettings()
556 260
    {
557
        return $this->getOptionValue(self::COMPOSER_SETTINGS);
558
    }
559
    /**
560
     * Set current composer settings option value
561
     * @throws \InvalidArgumentException
562
     * @param string $composerSettings
563
     * @return GeneratorOptions
564 28
     */
565
    public function setComposerSettings(array $composerSettings = array())
566 28
    {
567
        /**
568
         * If array is type array("config.value:true","require:libray/src",)
569
         */
570
        $settings = array();
571
        foreach ($composerSettings as $index=>$value) {
572 60
            if (is_numeric($index) && strpos($value, ':') > 0) {
573
                $path = implode('', array_slice(explode(':', $value), 0, 1));
574 60
                $val = implode(':', array_slice(explode(':', $value), 1));
575
                self::dotNotationToArray($path, $val, $settings);
576
            } else {
577
                $settings[$index] = $value;
578
            }
579
        }
580
        return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings);
581
    }
582 28
    /**
583
     * turns my.key.path to array('my' => array('key' => array('path' => $value)))
584 28
     * @param $path $string
0 ignored issues
show
Documentation introduced by
The doc-type $path could not be parsed: Unknown type name "$path" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
585
     * @param mixed $value
586
     * @param array $array
587
     */
588
    protected static function dotNotationToArray($string, $value, array &$array)
589
    {
590 100
        $keys = explode('.', $string);
591
        foreach ($keys as $key) {
592 100
            $array = &$array[$key];
593
        }
594
        $array = ($value === 'true' || $value === 'false') ? $value === 'true' : $value;
595
    }
596
    /**
597
     * Get structs folder option value
598
     * @return string
599
     */
600 28
    public function getStructsFolder()
601
    {
602 28
        return $this->getOptionValue(self::STRUCTS_FOLDER);
603
    }
604
    /**
605
     * Set current structs folder option value
606
     * @throws \InvalidArgumentException
607
     * @param string $structsFolder
608 512
     * @return GeneratorOptions
609
     */
610 512
    public function setStructsFolder($structsFolder)
611
    {
612
        return $this->setOptionValue(self::STRUCTS_FOLDER, $structsFolder);
613
    }
614
    /**
615
     * Get arrays folder option value
616
     * @return string
617
     */
618 28
    public function getArraysFolder()
619
    {
620 28
        return $this->getOptionValue(self::ARRAYS_FOLDER);
621
    }
622
    /**
623
     * Set current arrays folder option value
624
     * @throws \InvalidArgumentException
625 28
     * @param string $arraysFolder
626
     * @return GeneratorOptions
627 28
     */
628 28
    public function setArraysFolder($arraysFolder)
629 28
    {
630 21
        return $this->setOptionValue(self::ARRAYS_FOLDER, $arraysFolder);
631 28
    }
632
    /**
633
     * Get enums folder option value
634
     * @return string
635
     */
636
    public function getEnumsFolder()
637
    {
638
        return $this->getOptionValue(self::ENUMS_FOLDER);
639
    }
640
    /**
641
     * Set current enums folder option value
642
     * @throws \InvalidArgumentException
643
     * @param string $enumsFolder
644
     * @return GeneratorOptions
645
     */
646
    public function setEnumsFolder($enumsFolder)
647
    {
648
        return $this->setOptionValue(self::ENUMS_FOLDER, $enumsFolder);
649
    }
650
    /**
651
     * Get services folder option value
652
     * @return string
653
     */
654
    public function getServicesFolder()
655
    {
656
        return $this->getOptionValue(self::SERVICES_FOLDER);
657
    }
658
    /**
659
     * Set current services folder option value
660
     * @throws \InvalidArgumentException
661
     * @param string $servicesFolder
662
     * @return GeneratorOptions
663
     */
664
    public function setServicesFolder($servicesFolder)
665
    {
666
        return $this->setOptionValue(self::SERVICES_FOLDER, $servicesFolder);
667
    }
668
    /**
669
     * @return string[]
670
     */
671
    public function toArray()
672
    {
673
        $options = array();
674
        foreach (array_keys($this->options) as $name) {
675
            $options[$name] = $this->getOptionValue($name);
676
        }
677
        return $options;
678
    }
679
}
680