Completed
Push — develop ( 06b47b...d99803 )
by Mikaël
30:22
created

GeneratorOptions::setOptionValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

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