Completed
Push — master ( 5f180f...861647 )
by arto
02:09
created

Arguments   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 23
c 5
b 0
f 1
lcom 1
cbo 2
dl 0
loc 227
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getArguments() 0 4 1
A getFlags() 0 9 1
A getList() 0 17 2
A getLists() 0 9 1
A getValues() 0 9 1
A hasArguments() 0 4 1
A hasFlag() 0 4 1
A hasFlags() 0 4 1
A hasList() 0 4 1
A hasLists() 0 4 1
A hasValues() 0 4 1
A parseArguments() 0 12 2
A convertToArray() 0 4 1
A convertToString() 0 4 1
A __toString() 0 4 1
A parse() 0 6 1
A bindValuesFromGenerator() 0 8 1
A convertCollectionToArrayIfNeeded() 0 8 2
1
<?php
2
/**
3
 * @author: stev leibelt <[email protected]>
4
 * @since: 2015-04-16
5
 */
6
7
namespace Net\Bazzline\Component\Cli\Arguments;
8
9
class Arguments
10
{
11
    /** @var array */
12
    private $arguments;
13
14
    /** @var Collection */
15
    private $flags;
16
17
    /** @var Collection */
18
    private $lists;
19
20
    /** @var Parser */
21
    private $parser;
22
23
    /** @var Collection */
24
    private $values;
25
26
    /**
27
     * @param null|array $argv
28
     * @param boolean $removeFirstArgument
29
     */
30
    public function __construct($argv = null, $removeFirstArgument = true)
31
    {
32
        $this->parser = new Parser();
33
34
        if (is_array($argv)) {
35
            $this->parseArguments($argv, $removeFirstArgument);
36
        } else {
37
            $this->bindValuesFromGenerator();
38
        }
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getArguments()
45
    {
46
        return $this->arguments;
47
    }
48
49
    /**
50
     * @param bool $convertCollectionToArray
51
     * @return array|Collection
52
     */
53
    public function getFlags($convertCollectionToArray = true)
54
    {
55
        return (
56
            $this->convertCollectionToArrayIfNeeded(
57
                $this->flags,
58
                $convertCollectionToArray
59
            )
60
        );
61
    }
62
63
    /**
64
     * @param string $name
65
     * @param bool $convertCollectionToArray
66
     * @return null|Collection
67
     */
68
    public function getList($name, $convertCollectionToArray = true)
69
    {
70
        $list = $this->lists->offsetGet($name);
71
72
        if ($list instanceof Collection) {
73
            $return = (
74
                $this->convertCollectionToArrayIfNeeded(
75
                    $list,
76
                    $convertCollectionToArray
77
                )
78
            );
79
        } else {
80
            $return = null;
81
        }
82
83
        return $return;
84
    }
85
86
    /**
87
     * @param bool $convertCollectionToArray
88
     * @return array|Collection
89
     */
90
    public function getLists($convertCollectionToArray = true)
91
    {
92
        return (
93
            $this->convertCollectionToArrayIfNeeded(
94
                $this->lists,
95
                $convertCollectionToArray
96
            )
97
        );
98
    }
99
100
    /**
101
     * @param bool $convertCollectionToArray
102
     * @return array|Collection
103
     */
104
    public function getValues($convertCollectionToArray = true)
105
    {
106
        return (
107
            $this->convertCollectionToArrayIfNeeded(
108
                $this->values,
109
                $convertCollectionToArray
110
            )
111
        );
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function hasArguments()
118
    {
119
        return (!empty($this->arguments));
120
    }
121
122
    /**
123
     * @param string $name
124
     * @return bool
125
     */
126
    public function hasFlag($name)
127
    {
128
        return $this->flags->containsValue($name);
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function hasFlags()
135
    {
136
        return (!$this->flags->isEmpty());
137
    }
138
139
    /**
140
     * @param string $name
141
     * @return bool
142
     */
143
    public function hasList($name)
144
    {
145
        return $this->lists->containsKey($name);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function hasLists()
152
    {
153
        return (!$this->lists->isEmpty());
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function hasValues()
160
    {
161
        return (!$this->values->isEmpty());
162
    }
163
164
    /**
165
     * @param array $argv
166
     * @param boolean $removeFirstArgument
167
     * @return $this
168
     */
169
    public function parseArguments(array $argv, $removeFirstArgument = true)
170
    {
171
        if ($removeFirstArgument) {
172
            array_shift($argv);
173
        }
174
175
        $this->arguments = $argv;
176
        $this->parse($this->arguments);
177
        $this->bindValuesFromGenerator();
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function convertToArray()
186
    {
187
        return $this->getArguments();
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function convertToString()
194
    {
195
        return implode(' ', $this->convertToArray());
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function __toString()
202
    {
203
        return $this->convertToString();
204
    }
205
206
    private function parse(array $arguments)
207
    {
208
        $parser = $this->parser;
209
210
        $parser->parse($arguments);
211
    }
212
213
    private function bindValuesFromGenerator()
214
    {
215
        $parser = $this->parser;
216
217
        $this->flags    = $parser->getFlags();
218
        $this->lists    = $parser->getLists();
219
        $this->values   = $parser->getValues();
220
    }
221
222
    /**
223
     * @param Collection $collection
224
     * @param bool $isNeeded
225
     * @return array|Collection
226
     */
227
    private function convertCollectionToArrayIfNeeded(Collection $collection, $isNeeded)
228
    {
229
        return (
230
            $isNeeded
231
                ? $collection->convertToArray()
232
                : $collection
233
        );
234
    }
235
}