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

SignatureParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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
use Symfony\Component\Console\Input\InputArgument;
8
9
class SignatureParser
10
{
11
    /**
12
     * The command to build.
13
     *
14
     * @var Command
15
     */
16
    protected $command;
17
18
    /**
19
     * Construct.
20
     *
21
     * @param Command $command
22
     */
23
    public function __construct(Command $command)
24
    {
25
        $this->command = $command;
26
    }
27
28
    /**
29
     * Parse the command signature.
30
     *
31
     * @param  string $signature
32
     */
33
    public function parse($signature)
34
    {
35
        $this->setName($signature);
36
37
        $argumentsOptions = $this->extractArgumentsOptions($signature);
38
39
        foreach ($argumentsOptions as $value) {
40
            if (substr($value, 0, 2) !== '--') {
41
                $parser = new ArgumentParser($this->command);
42
            } else {
43
                $parser = new OptionParser($this->command);
44
45
                $value = trim($value, '--');
46
            }
47
48
            $parser->handle($value);
49
        }
50
    }
51
52
    /**
53
     * Set the command name.
54
     *
55
     * @param string $signature
56
     */
57
    protected function setName($signature)
58
    {
59
        $this->command->setName(preg_split('/\s+/', $signature)[0]);
60
    }
61
62
    /**
63
     * Extract arguments and options from signature.
64
     *
65
     * @param  string $signature
66
     *
67
     * @return array
68
     */
69
    protected function extractArgumentsOptions($signature)
70
    {
71
        preg_match_all('/{(.*?)}/', $signature, $argumentsOption);
72
73
        return array_map(function ($item) {
74
            return trim($item, '{}');
75
        }, $argumentsOption[1]);
76
    }
77
}
78