Completed
Push — feature/issue-101 ( 426b61...bb6cdd )
by Mikaël
49:18
created

GeneratorOptions::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
4
5
class GeneratorOptions extends AbstractYamlReader implements \JsonSerializable
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
     */
57
    protected function __construct($filename)
58
    {
59
        $this->options = array();
60
        $this->parseOptions($filename);
61
    }
62
    /**
63
     * Parse options for generator
64
     * @param string $filename options's file to parse
65
     * @return GeneratorOptions
66
     */
67
    protected function parseOptions($filename)
68
    {
69
        $options = $this->loadYaml($filename);
70
        if (is_array($options)) {
71
            $this->options = $options;
72
        } 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
        }
75
        return $this;
76
    }
77
    /**
78
     * Returns the option value
79
     * @throws \InvalidArgumentException
80
     * @param string $optionName
81
     * @return mixed
82
     */
83
    public function getOptionValue($optionName)
84
    {
85
        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
        }
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
     */
96
    public function setOptionValue($optionName, $optionValue, array $values = array())
97
    {
98
        if (!isset($this->options[$optionName])) {
99
            $this->options[$optionName] = array(
100
                'value' => $optionValue,
101
                'values' => $values,
102
            );
103
        } 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
        } else {
106
            $this->options[$optionName]['value'] = $optionValue;
107
        }
108
        return $this;
109
    }
110
    /**
111
     * @throws \InvalidArgumentException
112
     * @param string $filename options's file to parse
113
     * @return GeneratorOptions
114
     */
115
    public static function instance($filename = null)
116
    {
117
        return parent::instance(empty($filename) ? self::getDefaultConfigurationPath() : $filename);
118
    }
119
    /**
120
     * @return string
121
     */
122
    public static function getDefaultConfigurationPath()
123
    {
124
        return __DIR__ . '/../resources/config/generator_options.yml';
125
    }
126
    /**
127
     * Get category option value
128
     * @return string
129
     */
130
    public function getCategory()
131
    {
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
     */
140
    public function setCategory($category)
141
    {
142
        return $this->setOptionValue(self::CATEGORY, $category);
143
    }
144
    /**
145
     * Get add comments option value
146
     * @return array
147
     */
148
    public function getAddComments()
149
    {
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
     */
158
    public function setAddComments(array $addComments = array())
159
    {
160
        /**
161
         * If array is type array("author:john Doe","Release:1",)
162
         */
163
        $comments = array();
164
        foreach ($addComments as $index => $value) {
165
            if (is_numeric($index) && strpos($value, ':') > 0) {
166
                list($tag, $val) = explode(':', $value);
167
                $comments[$tag] = $val;
168
            } else {
169
                $comments[$index] = $value;
170
            }
171
        }
172
        return $this->setOptionValue(self::ADD_COMMENTS, $comments);
173
    }
174
    /**
175
     * Get gather methods option value
176
     * @return string
177
     */
178
    public function getGatherMethods()
179
    {
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
     */
188
    public function setGatherMethods($gatherMethods)
189
    {
190
        return $this->setOptionValue(self::GATHER_METHODS, $gatherMethods);
191
    }
192
    /**
193
     * Get generate tutorial file option value
194
     * @return bool
195
     */
196
    public function getGenerateTutorialFile()
197
    {
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
     */
206
    public function setGenerateTutorialFile($generateTutorialFile)
207
    {
208
        return $this->setOptionValue(self::GENERATE_TUTORIAL_FILE, $generateTutorialFile);
209
    }
210
    /**
211
     * Get namespace option value
212
     * @return string
213
     */
214
    public function getNamespace()
215
    {
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
     */
224
    public function setNamespace($namespace)
225
    {
226
        return $this->setOptionValue(self::NAMESPACE_PREFIX, $namespace);
227
    }
228
    /**
229
     * Get generic constants name option value
230
     * @return bool
231
     */
232
    public function getGenericConstantsName()
233
    {
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
     */
242
    public function setGenericConstantsName($genericConstantsName)
243
    {
244
        return $this->setOptionValue(self::GENERIC_CONSTANTS_NAME, $genericConstantsName);
245
    }
246
    /**
247
     * Get standalone option value
248
     * @return bool
249
     */
250
    public function getStandalone()
251
    {
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
     */
260
    public function setStandalone($standalone)
261
    {
262
        return $this->setOptionValue(self::STANDALONE, $standalone);
263
    }
264
    /**
265
     * Get validation option value
266
     * @return bool
267
     */
268
    public function getValidation()
269
    {
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
     */
278
    public function setValidation($validation)
279
    {
280
        return $this->setOptionValue(self::VALIDATION, $validation);
281
    }
282
    /**
283
     * Get struct class option value
284
     * @return string
285
     */
286
    public function getStructClass()
287
    {
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
     */
296
    public function setStructClass($structClass)
297
    {
298
        return $this->setOptionValue(self::STRUCT_CLASS, $structClass);
299
    }
300
    /**
301
     * Get struct array class option value
302
     * @return string
303
     */
304
    public function getStructArrayClass()
305
    {
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
     */
314
    public function setStructArrayClass($structArrayClass)
315
    {
316
        return $this->setOptionValue(self::STRUCT_ARRAY_CLASS, $structArrayClass);
317
    }
318
    /**
319
     * Get struct array class option value
320
     * @return string
321
     */
322
    public function getSoapClientClass()
323
    {
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
     */
332
    public function setSoapClientClass($soapClientClass)
333
    {
334
        return $this->setOptionValue(self::SOAP_CLIENT_CLASS, $soapClientClass);
335
    }
336
    /**
337
     * Get origin option value
338
     * @return string
339
     */
340
    public function getOrigin()
341
    {
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
     */
350
    public function setOrigin($origin)
351
    {
352
        return $this->setOptionValue(self::ORIGIN, $origin);
353
    }
354
    /**
355
     * Get destination option value
356
     * @return string
357
     */
358
    public function getDestination()
359
    {
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
     */
368
    public function setDestination($destination)
369
    {
370
        return $this->setOptionValue(self::DESTINATION, $destination);
371
    }
372
    /**
373
     * Get src dirname option value
374
     * @return string
375
     */
376
    public function getSrcDirname()
377
    {
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
     */
386
    public function setSrcDirname($srcDirname)
387
    {
388
        return $this->setOptionValue(self::SRC_DIRNAME, $srcDirname);
389
    }
390
    /**
391
     * Get prefix option value
392
     * @return string
393
     */
394
    public function getPrefix()
395
    {
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
     */
404
    public function setPrefix($prefix)
405
    {
406
        return $this->setOptionValue(self::PREFIX, $prefix);
407
    }
408
    /**
409
     * Get suffix option value
410
     * @return string
411
     */
412
    public function getSuffix()
413
    {
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
     */
422
    public function setSuffix($suffix)
423
    {
424
        return $this->setOptionValue(self::SUFFIX, $suffix);
425
    }
426
    /**
427
     * Get basic login option value
428
     * @return string
429
     */
430
    public function getBasicLogin()
431
    {
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
     */
440
    public function setBasicLogin($basicLogin)
441
    {
442
        return $this->setOptionValue(self::BASIC_LOGIN, $basicLogin);
443
    }
444
    /**
445
     * Get basic password option value
446
     * @return string
447
     */
448
    public function getBasicPassword()
449
    {
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
     */
458
    public function setBasicPassword($basicPassword)
459
    {
460
        return $this->setOptionValue(self::BASIC_PASSWORD, $basicPassword);
461
    }
462
    /**
463
     * Get basic proxy host option value
464
     * @return string
465
     */
466
    public function getProxyHost()
467
    {
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
     */
476
    public function setProxyHost($proxyHost)
477
    {
478
        return $this->setOptionValue(self::PROXY_HOST, $proxyHost);
479
    }
480
    /**
481
     * Get basic proxy port option value
482
     * @return string
483
     */
484
    public function getProxyPort()
485
    {
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
     */
494
    public function setProxyPort($proxyPort)
495
    {
496
        return $this->setOptionValue(self::PROXY_PORT, $proxyPort);
497
    }
498
    /**
499
     * Get basic proxy login option value
500
     * @return string
501
     */
502
    public function getProxyLogin()
503
    {
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
     */
512
    public function setProxyLogin($proxyLogin)
513
    {
514
        return $this->setOptionValue(self::PROXY_LOGIN, $proxyLogin);
515
    }
516
    /**
517
     * Get basic proxy password option value
518
     * @return string
519
     */
520
    public function getProxyPassword()
521
    {
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
     */
530
    public function setProxyPassword($proxyPassword)
531
    {
532
        return $this->setOptionValue(self::PROXY_PASSWORD, $proxyPassword);
533
    }
534
    /**
535
     * Get basic soap options option value
536
     * @return array
537
     */
538
    public function getSoapOptions()
539
    {
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
     */
548
    public function setSoapOptions(array $soapOptions)
549
    {
550
        return $this->setOptionValue(self::SOAP_OPTIONS, $soapOptions);
551
    }
552
    /**
553
     * Get composer name option value
554
     * @return string
555
     */
556
    public function getComposerName()
557
    {
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
     */
566
    public function setComposerName($composerName)
567
    {
568
        return $this->setOptionValue(self::COMPOSER_NAME, $composerName);
569
    }
570
    /**
571
     * Get composer settings option value
572
     * @return array
573
     */
574
    public function getComposerSettings()
575
    {
576
        return $this->getOptionValue(self::COMPOSER_SETTINGS);
577
    }
578
    /**
579
     * Set current composer settings option value
580
     * @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
         */
589
        $settings = array();
590
        foreach ($composerSettings as $index => $value) {
591
            if (is_numeric($index) && strpos($value, ':') > 0) {
592
                $path = implode('', array_slice(explode(':', $value), 0, 1));
593
                $val = implode(':', array_slice(explode(':', $value), 1));
594
                self::dotNotationToArray($path, $val, $settings);
595
            } else {
596
                $settings[$index] = $value;
597
            }
598
        }
599
        return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings);
600
    }
601
    /**
602
     * 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
        foreach ($keys as $key) {
611
            $array = &$array[$key];
612
        }
613
        $array = ($value === 'true' || $value === 'false') ? $value === 'true' : $value;
614
    }
615
    /**
616
     * Get structs folder option value
617
     * @return string
618
     */
619
    public function getStructsFolder()
620
    {
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
     */
629
    public function setStructsFolder($structsFolder)
630
    {
631
        return $this->setOptionValue(self::STRUCTS_FOLDER, $structsFolder);
632
    }
633
    /**
634
     * Get arrays folder option value
635
     * @return string
636
     */
637
    public function getArraysFolder()
638
    {
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
     */
647
    public function setArraysFolder($arraysFolder)
648
    {
649
        return $this->setOptionValue(self::ARRAYS_FOLDER, $arraysFolder);
650
    }
651
    /**
652
     * Get enums folder option value
653
     * @return string
654
     */
655
    public function getEnumsFolder()
656
    {
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
     */
665
    public function setEnumsFolder($enumsFolder)
666
    {
667
        return $this->setOptionValue(self::ENUMS_FOLDER, $enumsFolder);
668
    }
669
    /**
670
     * Get services folder option value
671
     * @return string
672
     */
673
    public function getServicesFolder()
674
    {
675
        return $this->getOptionValue(self::SERVICES_FOLDER);
676
    }
677
    /**
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
    public function jsonSerialize()
699
    {
700
        return $this->toArray();
701
    }
702
}
703