Completed
Push — master ( 1e240b...42513d )
by Joe
02:04
created

Manager::trailing()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\CLImate\Argument;
4
5
use League\CLImate\CLImate;
6
7
class Manager
8
{
9
    /**
10
     * An array of arguments passed to the program.
11
     *
12
     * @var Argument[] $arguments
13
     */
14
    protected $arguments = [];
15
16
    /**
17
     * A program's description.
18
     *
19
     * @var string $description
20
     */
21
    protected $description;
22
23
    /**
24
     * Filter class to find various types of arguments
25
     *
26
     * @var League\CLImate\Argument\Filter $filter
27
     */
28
    protected $filter;
29
30
    /**
31
     * Summary builder class
32
     *
33
     * @var \League\CLImate\Argument\Summary $summary
34
     */
35
    protected $summary;
36
37
    /**
38
     * Argument parser class
39
     *
40
     * @var \League\CLImate\Argument\Parser $parser
41
     */
42
    protected $parser;
43
44 924
    public function __construct()
45
    {
46 924
        $this->filter  = new Filter();
47 924
        $this->summary = new Summary();
48 924
        $this->parser  = new Parser();
49 924
    }
50
51
    /**
52
     * Add an argument.
53
     *
54
     * @throws \Exception if $argument isn't an array or Argument object.
55
     * @param Argument|string|array $argument
56
     * @param $options
57
     */
58 20
    public function add($argument, array $options = [])
59
    {
60 20
        if (is_array($argument)) {
61 16
            $this->addMany($argument);
62 16
            return;
63
        }
64
65 20
        if (is_string($argument)) {
66 16
            $argument = Argument::createFromArray($argument, $options);
67 16
        }
68
69 20
        if (!($argument instanceof Argument)) {
70 4
            throw new \Exception('Please provide an argument name or object.');
71
        }
72
73 16
        $this->arguments[$argument->name()] = $argument;
74 16
    }
75
76
    /**
77
     * Add multiple arguments to a CLImate script.
78
     *
79
     * @param array $arguments
80
     */
81 16
    protected function addMany(array $arguments = [])
82
    {
83 16
        foreach ($arguments as $name => $options) {
84 16
            $this->add($name, $options);
85 16
        }
86 16
    }
87
88
    /**
89
     * Determine if an argument exists.
90
     *
91
     * @param string $name
92
     * @return bool
93
     */
94 4
    public function exists($name)
95
    {
96 4
        return isset($this->arguments[$name]);
97
    }
98
99
    /**
100
     * Retrieve an argument's value.
101
     *
102
     * @param string $name
103
     * @return string|int|float|bool|null
104
     */
105 4
    public function get($name)
106
    {
107 4
        return isset($this->arguments[$name]) ? $this->arguments[$name]->value() : null;
108
    }
109
110
    /**
111
     * Retrieve all arguments.
112
     *
113
     * @return Argument[]
114
     */
115 12
    public function all()
116
    {
117 12
        return $this->arguments;
118
    }
119
120
    /**
121
     * Determine if an argument has been defined on the command line.
122
     *
123
     * This can be useful for making sure an argument is present on the command
124
     * line before parse()'ing them into argument objects.
125
     *
126
     * @param string $name
127
     * @param array $argv
128
     *
129
     * @return bool
130
     */
131 4
    public function defined($name, array $argv = null)
132
    {
133
        // The argument isn't defined if it's not defined by the calling code.
134 4
        if (!$this->exists($name)) {
135 4
            return false;
136
        }
137
138 4
        $argument = $this->arguments[$name];
139 4
        $command_arguments = $this->parser->arguments($argv);
140
141 4
        foreach ($command_arguments as $command_argument) {
142 4
            if ($this->isArgument($argument, $command_argument)) {
143 4
                return true;
144
            }
145 4
        }
146
147 4
        return false;
148
    }
149
150
    /**
151
     * Check if the defined argument matches the command argument.
152
     *
153
     * @param Argument $argument
154
     * @param string $command_argument
155
     *
156
     * @return bool
157
     */
158 4
    protected function isArgument($argument, $command_argument)
159
    {
160
        $possibilities = [
161 4
            $argument->prefix()     => "-{$argument->prefix()}",
162 4
            $argument->longPrefix() => "--{$argument->longPrefix()}",
163 4
        ];
164
165 4
        foreach ($possibilities as $key => $search) {
166 4
            if ($key && strpos($command_argument, $search) === 0) {
167 4
                return true;
168
            }
169 4
        }
170
171 4
        return false;
172
    }
173
174
    /**
175
     * Retrieve all arguments as key/value pairs.
176
     *
177
     * @return array
178
     */
179 4
    public function toArray()
180
    {
181 4
        $return = [];
182
183 4
        foreach ($this->all() as $name => $argument) {
184 4
            $return[$name] = $argument->value();
185 4
        }
186
187 4
        return $return;
188
    }
189
190
    /**
191
     * Set a program's description.
192
     *
193
     * @param string $description
194
     */
195 4
    public function description($description)
196
    {
197 4
        $this->description = trim($description);
198 4
    }
199
200
    /**
201
     * Output a script's usage statement.
202
     *
203
     * @param CLImate $climate
204
     * @param array $argv
205
     */
206 4
    public function usage(CLImate $climate, array $argv = null)
207
    {
208 4
        $this->summary
209 4
                ->setClimate($climate)
210 4
                ->setDescription($this->description)
211 4
                ->setCommand($this->parser->command($argv))
212 4
                ->setFilter($this->filter, $this->all())
213 4
                ->output();
214 4
    }
215
216
    /**
217
     * Parse command line arguments into CLImate arguments.
218
     *
219
     * @throws \Exception if required arguments aren't defined.
220
     * @param array $argv
221
     */
222 8
    public function parse(array $argv = null)
223
    {
224 8
        $this->parser->setFilter($this->filter, $this->all());
225
226 8
        $this->parser->parse($argv);
227 4
    }
228
229
    /**
230
     * Get the trailing arguments
231
     *
232
     * @return string|null
233
     */
234
    public function trailing()
235
    {
236
        return $this->parser->trailing();
237
    }
238
}
239