Completed
Push — dev ( 122982...5e45c8 )
by Zach
02:18
created

OptionParser::parseValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class OptionParser extends Parser
9
{
10
    /**
11
     * Option name.
12
     *
13
     * @var string
14
     */
15
    protected $name = '';
16
17
    /**
18
     * Option shortcut.
19
     *
20
     * @var null|string
21
     */
22
    protected $shortcut = null;
23
24
    /**
25
     * Array of option modes to apply.
26
     *
27
     * @var array
28
     */
29
    protected $modeArray = [];
30
31
    /**
32
     * Option description.
33
     *
34
     * @var null|string
35
     */
36
    protected $description = null;
37
38
    /**
39
     * Default option value.
40
     *
41
     * @var null|string
42
     */
43
    protected $default = null;
44
45
    /**
46
     * Parse the given option string.
47
     *
48
     * @param string $option
49
     */
50
    public function handle($option)
51
    {
52
        list($option, $this->description) = $this->parseDescription($option);
53
54
        $option = $this->parseArray($option, 'VALUE_IS_ARRAY');
55
56
        if (strpos($option, '=') !== false) {
57
            $option = $this->parseValue($option);
58
        }
59
60
        $this->modeArray = empty($this->modeArray) ? ['VALUE_NONE'] : $this->modeArray;
61
62
        if (strpos($option, '|') !== false) {
63
            list($this->shortcut, $option) = explode('|', $option);
64
        }
65
66
        $this->command->addOption(
67
            $option,
68
            $this->shortcut,
69
            $this->calculateMode($this->modeArray, InputOption::class),
70
            $this->description,
71
            $this->default
72
        );
73
    }
74
75
    /**
76
     * Parse option value and value default.
77
     *
78
     * @param string $option
79
     *
80
     * @return string
81
     */
82
    protected function parseValue($option)
83
    {
84
        if (substr($option, -1) === '=') {
85
            $option = str_replace('=', '', $option);
86
87
            $this->modeArray[] = 'VALUE_REQUIRED';
88
        } else {
89
            list($option, $this->default) = explode('=', $option);
90
91
            $this->modeArray[] = 'VALUE_OPTIONAL';
92
        }
93
94
        return $option;
95
    }
96
}
97