Argument::getAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Artisanize\Input;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
7
class Argument extends Input
8
{
9
    /**
10
     * Default arguement value.
11
     *
12
     * @var null|string
13
     */
14
    protected $default = null;
15
16
    /**
17
     * Parse the set argument string.
18
     *
19
     * @return $this
20
     */
21
    public function parse()
22
    {
23
        return $this->setDescription()
24
            ->setArray('IS_ARRAY')
25
            ->setOptional()
26
            ->setDefault()
27
            ->setRequired()
28
            ->calculateMode(InputArgument::class)
29
            ->setName();
30
    }
31
32
    /**
33
     * Set optional argument mode.
34
     *
35
     * @return $this
36
     */
37
    protected function setOptional()
38
    {
39
        if (substr($this->input, -1) === '?') {
40
            $this->modeArray[] = 'OPTIONAL';
41
42
            $this->input = str_replace('?', '', $this->input);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Set default option value.
50
     *
51
     * @return $this
52
     */
53
    protected function setDefault()
54
    {
55
        if (strpos($this->input, '=')) {
56
            list($this->input, $this->default) = explode('=', $this->input);
57
58
            $this->modeArray[] = 'OPTIONAL';
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set required mode if optional not already set.
66
     *
67
     * @return $this
68
     */
69
    protected function setRequired()
70
    {
71
        if (!in_array('OPTIONAL', $this->modeArray)) {
72
            $this->modeArray[] = 'REQUIRED';
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * Get the argument attributes.
80
     *
81
     * @return array
82
     */
83
    public function getAttributes()
84
    {
85
        return [
86
            'name'        => $this->name,
87
            'mode'        => $this->mode,
88
            'description' => $this->description,
89
            'default'     => $this->default,
90
        ];
91
    }
92
}
93