Completed
Push — feature/issue-91 ( f0947a...f31c52 )
by Mikaël
46:39
created

GeneratorOptions::getStructClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 SRC_DIRNAME = 'src_dirname';
43
    const STANDALONE = 'standalone';
44
    const STRUCT_ARRAY_CLASS = 'struct_array_class';
45
    const STRUCT_CLASS = 'struct_class';
46
    const STRUCTS_FOLDER = 'structs_folder';
47
    const SUFFIX = 'suffix';
48
    const VALIDATION = 'validation';
49
    /**
50
     * Generator's options
51
     * @var array
52
     */
53
    protected $options;
54
    /**
55
     * @param string $filename
56 54
     */
57
    protected function __construct($filename)
58 54
    {
59 54
        $this->options = array();
60 48
        $this->parseOptions($filename);
61
    }
62
    /**
63
     * Parse options for generator
64
     * @param string $filename options's file to parse
65
     * @return GeneratorOptions
66 54
     */
67
    protected function parseOptions($filename)
68 54
    {
69 54
        $options = $this->loadYaml($filename);
70 48
        if (is_array($options)) {
71 48
            $this->options = $options;
72 6
        } else {
73
            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__);
74 48
        }
75
        return $this;
76
    }
77
    /**
78
     * Returns the option value
79
     * @throws \InvalidArgumentException
80
     * @param string $optionName
81
     * @return mixed
82 1650
     */
83
    public function getOptionValue($optionName)
84 1650
    {
85 6
        if (!isset($this->options[$optionName])) {
86
            throw new \InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__);
87 1644
        }
88
        return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default'];
89
    }
90
    /**
91
     * Allows to add an option and set its value
92
     * @throws \InvalidArgumentException
93
     * @param string $optionName
94
     * @return GeneratorOptions
95 1254
     */
96
    public function setOptionValue($optionName, $optionValue, array $values = array())
97 1254
    {
98 12
        if (!isset($this->options[$optionName])) {
99 12
            $this->options[$optionName] = array(
100 12
                'value' => $optionValue,
101
                'values' => $values,
102 1254
            );
103 12
        } elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) {
104
            throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
105 1236
        } else {
106
            $this->options[$optionName]['value'] = $optionValue;
107 1242
        }
108
        return $this;
109
    }
110
    /**
111
     * @throws \InvalidArgumentException
112
     * @param string $filename options's file to parse
113
     * @return GeneratorOptions
114 1212
     */
115
    public static function instance($filename = null)
116 1212
    {
117
        return parent::instance(empty($filename) ? self::getDefaultConfigurationPath() : $filename);
118
    }
119
    /**
120
     * @return string
121 90
     */
122
    public static function getDefaultConfigurationPath()
123 90
    {
124
        return __DIR__ . '/../resources/config/generator_options.yml';
125
    }
126
    /**
127
     * Get category option value
128
     * @return string
129 456
     */
130
    public function getCategory()
131 456
    {
132
        return $this->getOptionValue(self::CATEGORY);
133
    }
134
    /**
135
     * Set current category option value
136
     * @throws \InvalidArgumentException
137
     * @param string $category
138
     * @return GeneratorOptions
139 474
     */
140
    public function setCategory($category)
141 474
    {
142
        return $this->setOptionValue(self::CATEGORY, $category);
143
    }
144
    /**
145
     * Get add comments option value
146
     * @return array
147 348
     */
148
    public function getAddComments()
149 348
    {
150
        return $this->getOptionValue(self::ADD_COMMENTS);
151
    }
152
    /**
153
     * Set current add comments option value
154
     * @throws \InvalidArgumentException
155
     * @param array $addComments
156
     * @return GeneratorOptions
157 534
     */
158
    public function setAddComments(array $addComments = array())
159
    {
160
        /**
161
         * If array is type array("author:john Doe","Release:1",)
162 534
         */
163 534
        $comments = array();
164 450
        foreach ($addComments as $index => $value) {
165 6
            if (is_numeric($index) && strpos($value, ':') > 0) {
166 6
                list($tag, $val) = explode(':', $value);
167 6
                $comments[$tag] = $val;
168 444
            } else {
169
                $comments[$index] = $value;
170 534
            }
171 534
        }
172
        return $this->setOptionValue(self::ADD_COMMENTS, $comments);
173
    }
174
    /**
175
     * Get gather methods option value
176
     * @return string
177 762
     */
178
    public function getGatherMethods()
179 762
    {
180
        return $this->getOptionValue(self::GATHER_METHODS);
181
    }
182
    /**
183
     * Set current gather methods option value
184
     * @throws \InvalidArgumentException
185
     * @param string $gatherMethods
186
     * @return GeneratorOptions
187 474
     */
188
    public function setGatherMethods($gatherMethods)
189 474
    {
190
        return $this->setOptionValue(self::GATHER_METHODS, $gatherMethods);
191
    }
192
    /**
193
     * Get generate tutorial file option value
194
     * @return bool
195 54
     */
196
    public function getGenerateTutorialFile()
197 54
    {
198
        return $this->getOptionValue(self::GENERATE_TUTORIAL_FILE);
199
    }
200
    /**
201
     * Set current generate tutorial file option value
202
     * @throws \InvalidArgumentException
203
     * @param bool $generateTutorialFile
204
     * @return GeneratorOptions
205 84
     */
206
    public function setGenerateTutorialFile($generateTutorialFile)
207 84
    {
208
        return $this->setOptionValue(self::GENERATE_TUTORIAL_FILE, $generateTutorialFile);
209
    }
210
    /**
211
     * Get namespace option value
212
     * @return string
213 420
     */
214
    public function getNamespace()
215 420
    {
216
        return $this->getOptionValue(self::NAMESPACE_PREFIX);
217
    }
218
    /**
219
     * Set current namespace option value
220
     * @throws \InvalidArgumentException
221
     * @param string $namespace
222
     * @return GeneratorOptions
223 66
     */
224
    public function setNamespace($namespace)
225 66
    {
226
        return $this->setOptionValue(self::NAMESPACE_PREFIX, $namespace);
227
    }
228
    /**
229
     * Get generic constants name option value
230
     * @return bool
231 102
     */
232
    public function getGenericConstantsName()
233 102
    {
234
        return $this->getOptionValue(self::GENERIC_CONSTANTS_NAME);
235
    }
236
    /**
237
     * Set current generic constants name option value
238
     * @throws \InvalidArgumentException
239
     * @param bool $genericConstantsName
240
     * @return GeneratorOptions
241 84
     */
242
    public function setGenericConstantsName($genericConstantsName)
243 84
    {
244
        return $this->setOptionValue(self::GENERIC_CONSTANTS_NAME, $genericConstantsName);
245
    }
246
    /**
247
     * Get standalone option value
248
     * @return bool
249 102
     */
250
    public function getStandalone()
251 102
    {
252
        return $this->getOptionValue(self::STANDALONE);
253
    }
254
    /**
255
     * Set current standalone option value
256
     * @throws \InvalidArgumentException
257
     * @param bool $standalone
258
     * @return GeneratorOptions
259 48
     */
260
    public function setStandalone($standalone)
261 48
    {
262
        return $this->setOptionValue(self::STANDALONE, $standalone);
263
    }
264
    /**
265
     * Get validation option value
266
     * @return bool
267 204
     */
268
    public function getValidation()
269 204
    {
270
        return $this->getOptionValue(self::VALIDATION);
271
    }
272
    /**
273
     * Set current validation option value
274
     * @throws \InvalidArgumentException
275
     * @param bool $validation
276
     * @return GeneratorOptions
277 48
     */
278
    public function setValidation($validation)
279 48
    {
280
        return $this->setOptionValue(self::VALIDATION, $validation);
281
    }
282
    /**
283
     * Get struct class option value
284
     * @return string
285 174
     */
286
    public function getStructClass()
287 174
    {
288
        return $this->getOptionValue(self::STRUCT_CLASS);
289
    }
290
    /**
291
     * Set current struct class option value
292
     * @throws \InvalidArgumentException
293
     * @param string $structClass
294
     * @return GeneratorOptions
295 54
     */
296
    public function setStructClass($structClass)
297 54
    {
298
        return $this->setOptionValue(self::STRUCT_CLASS, $structClass);
299
    }
300
    /**
301
     * Get struct array class option value
302
     * @return string
303 66
     */
304
    public function getStructArrayClass()
305 66
    {
306
        return $this->getOptionValue(self::STRUCT_ARRAY_CLASS);
307
    }
308
    /**
309
     * Set current struct array class option value
310
     * @throws \InvalidArgumentException
311
     * @param string $structArrayClass
312
     * @return GeneratorOptions
313 42
     */
314
    public function setStructArrayClass($structArrayClass)
315 42
    {
316
        return $this->setOptionValue(self::STRUCT_ARRAY_CLASS, $structArrayClass);
317
    }
318
    /**
319
     * Get struct array class option value
320
     * @return string
321 150
     */
322
    public function getSoapClientClass()
323 150
    {
324
        return $this->getOptionValue(self::SOAP_CLIENT_CLASS);
325
    }
326
    /**
327
     * Set current struct array class option value
328
     * @throws \InvalidArgumentException
329
     * @param string $soapClientClass
330
     * @return GeneratorOptions
331 42
     */
332
    public function setSoapClientClass($soapClientClass)
333 42
    {
334
        return $this->setOptionValue(self::SOAP_CLIENT_CLASS, $soapClientClass);
335
    }
336
    /**
337
     * Get origin option value
338
     * @return string
339 816
     */
340
    public function getOrigin()
341 816
    {
342
        return $this->getOptionValue(self::ORIGIN);
343
    }
344
    /**
345
     * Set current origin option value
346
     * @throws \InvalidArgumentException
347
     * @param string $origin
348
     * @return GeneratorOptions
349 828
     */
350
    public function setOrigin($origin)
351 828
    {
352
        return $this->setOptionValue(self::ORIGIN, $origin);
353
    }
354
    /**
355
     * Get destination option value
356
     * @return string
357 480
     */
358
    public function getDestination()
359 480
    {
360
        return $this->getOptionValue(self::DESTINATION);
361
    }
362
    /**
363
     * Set current destination option value
364
     * @throws \InvalidArgumentException
365
     * @param string $destination
366
     * @return GeneratorOptions
367 834
     */
368
    public function setDestination($destination)
369 834
    {
370
        return $this->setOptionValue(self::DESTINATION, $destination);
371
    }
372
    /**
373
     * Get src dirname option value
374
     * @return string
375 858
     */
376
    public function getSrcDirname()
377 858
    {
378
        return $this->getOptionValue(self::SRC_DIRNAME);
379
    }
380
    /**
381
     * Set current src dirname option value
382
     * @throws \InvalidArgumentException
383
     * @param string $srcDirname
384
     * @return GeneratorOptions
385 504
     */
386
    public function setSrcDirname($srcDirname)
387 504
    {
388
        return $this->setOptionValue(self::SRC_DIRNAME, $srcDirname);
389
    }
390
    /**
391
     * Get prefix option value
392
     * @return string
393 834
     */
394
    public function getPrefix()
395 834
    {
396
        return $this->getOptionValue(self::PREFIX);
397
    }
398
    /**
399
     * Set current prefix option value
400
     * @throws \InvalidArgumentException
401
     * @param string $prefix
402
     * @return GeneratorOptions
403 78
     */
404
    public function setPrefix($prefix)
405 78
    {
406
        return $this->setOptionValue(self::PREFIX, $prefix);
407
    }
408
    /**
409
     * Get suffix option value
410
     * @return string
411 822
     */
412
    public function getSuffix()
413 822
    {
414
        return $this->getOptionValue(self::SUFFIX);
415
    }
416
    /**
417
     * Set current suffix option value
418
     * @throws \InvalidArgumentException
419
     * @param string $suffix
420
     * @return GeneratorOptions
421 42
     */
422
    public function setSuffix($suffix)
423 42
    {
424
        return $this->setOptionValue(self::SUFFIX, $suffix);
425
    }
426
    /**
427
     * Get basic login option value
428
     * @return string
429 822
     */
430
    public function getBasicLogin()
431 822
    {
432
        return $this->getOptionValue(self::BASIC_LOGIN);
433
    }
434
    /**
435
     * Set current basic login option value
436
     * @throws \InvalidArgumentException
437
     * @param string $basicLogin
438
     * @return GeneratorOptions
439 42
     */
440
    public function setBasicLogin($basicLogin)
441 42
    {
442
        return $this->setOptionValue(self::BASIC_LOGIN, $basicLogin);
443
    }
444
    /**
445
     * Get basic password option value
446
     * @return string
447 822
     */
448
    public function getBasicPassword()
449 822
    {
450
        return $this->getOptionValue(self::BASIC_PASSWORD);
451
    }
452
    /**
453
     * Set current basic password option value
454
     * @throws \InvalidArgumentException
455
     * @param string $basicPassword
456
     * @return GeneratorOptions
457 42
     */
458
    public function setBasicPassword($basicPassword)
459 42
    {
460
        return $this->setOptionValue(self::BASIC_PASSWORD, $basicPassword);
461
    }
462
    /**
463
     * Get basic proxy host option value
464
     * @return string
465 822
     */
466
    public function getProxyHost()
467 822
    {
468
        return $this->getOptionValue(self::PROXY_HOST);
469
    }
470
    /**
471
     * Set current proxy host option value
472
     * @throws \InvalidArgumentException
473
     * @param string $proxyHost
474
     * @return GeneratorOptions
475 42
     */
476
    public function setProxyHost($proxyHost)
477 42
    {
478
        return $this->setOptionValue(self::PROXY_HOST, $proxyHost);
479
    }
480
    /**
481
     * Get basic proxy port option value
482
     * @return string
483 822
     */
484
    public function getProxyPort()
485 822
    {
486
        return $this->getOptionValue(self::PROXY_PORT);
487
    }
488
    /**
489
     * Set current proxy port option value
490
     * @throws \InvalidArgumentException
491
     * @param string $proxyPort
492
     * @return GeneratorOptions
493 42
     */
494
    public function setProxyPort($proxyPort)
495 42
    {
496
        return $this->setOptionValue(self::PROXY_PORT, $proxyPort);
497
    }
498
    /**
499
     * Get basic proxy login option value
500
     * @return string
501 822
     */
502
    public function getProxyLogin()
503 822
    {
504
        return $this->getOptionValue(self::PROXY_LOGIN);
505
    }
506
    /**
507
     * Set current proxy login option value
508
     * @throws \InvalidArgumentException
509
     * @param string $proxyLogin
510
     * @return GeneratorOptions
511 42
     */
512
    public function setProxyLogin($proxyLogin)
513 42
    {
514
        return $this->setOptionValue(self::PROXY_LOGIN, $proxyLogin);
515
    }
516
    /**
517
     * Get basic proxy password option value
518
     * @return string
519 810
     */
520
    public function getProxyPassword()
521 810
    {
522
        return $this->getOptionValue(self::PROXY_PASSWORD);
523
    }
524
    /**
525
     * Set current proxy password option value
526
     * @throws \InvalidArgumentException
527
     * @param string $proxyPassword
528
     * @return GeneratorOptions
529 48
     */
530
    public function setProxyPassword($proxyPassword)
531 48
    {
532
        return $this->setOptionValue(self::PROXY_PASSWORD, $proxyPassword);
533
    }
534
    /**
535
     * Get basic soap options option value
536
     * @return array
537 72
     */
538
    public function getSoapOptions()
539 72
    {
540
        return $this->getOptionValue(self::SOAP_OPTIONS);
541
    }
542
    /**
543
     * Set current soap options option value
544
     * @throws \InvalidArgumentException
545
     * @param array $soapOptions
546
     * @return GeneratorOptions
547 108
     */
548
    public function setSoapOptions(array $soapOptions)
549 108
    {
550
        return $this->setOptionValue(self::SOAP_OPTIONS, $soapOptions);
551
    }
552
    /**
553
     * Get composer name option value
554
     * @return string
555 54
     */
556
    public function getComposerName()
557 54
    {
558
        return $this->getOptionValue(self::COMPOSER_NAME);
559
    }
560
    /**
561
     * Set current composer name option value
562
     * @throws \InvalidArgumentException
563
     * @param string $composerName
564
     * @return GeneratorOptions
565 102
     */
566
    public function setComposerName($composerName)
567
    {
568
        return $this->setOptionValue(self::COMPOSER_NAME, $composerName);
569
    }
570 102
    /**
571 102
     * Get composer settings option value
572 42
     * @return array
573 42
     */
574 42
    public function getComposerSettings()
575 42
    {
576 42
        return $this->getOptionValue(self::COMPOSER_SETTINGS);
577 6
    }
578
    /**
579 102
     * Set current composer settings option value
580 102
     * @throws \InvalidArgumentException
581
     * @param array $composerSettings
582
     * @return GeneratorOptions
583
     */
584
    public function setComposerSettings(array $composerSettings = array())
585
    {
586
        /**
587
         * If array is type array("config.value:true","require:libray/src",)
588 42
         */
589
        $settings = array();
590 42
        foreach ($composerSettings as $index => $value) {
591 42
            if (is_numeric($index) && strpos($value, ':') > 0) {
592 42
                $path = implode('', array_slice(explode(':', $value), 0, 1));
593 42
                $val = implode(':', array_slice(explode(':', $value), 1));
594 42
                self::dotNotationToArray($path, $val, $settings);
595 42
            } else {
596
                $settings[$index] = $value;
597
            }
598
        }
599
        return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings);
600 426
    }
601
    /**
602 426
     * turns my.key.path to array('my' => array('key' => array('path' => $value)))
603
     * @param string $string
604
     * @param mixed $value
605
     * @param array $array
606
     */
607
    protected static function dotNotationToArray($string, $value, array &$array)
608
    {
609
        $keys = explode('.', $string);
610 42
        foreach ($keys as $key) {
611
            $array = &$array[$key];
612 42
        }
613
        $array = ($value === 'true' || $value === 'false') ? $value === 'true' : $value;
614
    }
615
    /**
616
     * Get structs folder option value
617
     * @return string
618 90
     */
619
    public function getStructsFolder()
620 90
    {
621
        return $this->getOptionValue(self::STRUCTS_FOLDER);
622
    }
623
    /**
624
     * Set current structs folder option value
625
     * @throws \InvalidArgumentException
626
     * @param string $structsFolder
627
     * @return GeneratorOptions
628 42
     */
629
    public function setStructsFolder($structsFolder)
630 42
    {
631
        return $this->setOptionValue(self::STRUCTS_FOLDER, $structsFolder);
632
    }
633
    /**
634
     * Get arrays folder option value
635
     * @return string
636 168
     */
637
    public function getArraysFolder()
638 168
    {
639
        return $this->getOptionValue(self::ARRAYS_FOLDER);
640
    }
641
    /**
642
     * Set current arrays folder option value
643
     * @throws \InvalidArgumentException
644
     * @param string $arraysFolder
645
     * @return GeneratorOptions
646 42
     */
647
    public function setArraysFolder($arraysFolder)
648 42
    {
649
        return $this->setOptionValue(self::ARRAYS_FOLDER, $arraysFolder);
650
    }
651
    /**
652
     * Get enums folder option value
653
     * @return string
654 810
     */
655
    public function getEnumsFolder()
656 810
    {
657
        return $this->getOptionValue(self::ENUMS_FOLDER);
658
    }
659
    /**
660
     * Set current enums folder option value
661
     * @throws \InvalidArgumentException
662
     * @param string $enumsFolder
663
     * @return GeneratorOptions
664 42
     */
665
    public function setEnumsFolder($enumsFolder)
666 42
    {
667
        return $this->setOptionValue(self::ENUMS_FOLDER, $enumsFolder);
668
    }
669
    /**
670
     * Get services folder option value
671 42
     * @return string
672
     */
673 42
    public function getServicesFolder()
674 42
    {
675 42
        return $this->getOptionValue(self::SERVICES_FOLDER);
676 42
    }
677 42
    /**
678
     * Set current services folder option value
679
     * @throws \InvalidArgumentException
680
     * @param string $servicesFolder
681
     * @return GeneratorOptions
682
     */
683
    public function setServicesFolder($servicesFolder)
684
    {
685
        return $this->setOptionValue(self::SERVICES_FOLDER, $servicesFolder);
686
    }
687
    /**
688
     * @return string[]
689
     */
690
    public function toArray()
691
    {
692
        $options = array();
693
        foreach (array_keys($this->options) as $name) {
694
            $options[$name] = $this->getOptionValue($name);
695
        }
696
        return $options;
697
    }
698
}
699