Builder::setPrefix()   A
last analyzed

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
namespace Filesystem;
3
4
/**
5
 * Builder class.
6
 *
7
 * @package Filesystem
8
 */
9
class Builder
10
{
11
12
    /**
13
     * Command prefix.
14
     *
15
     * @var string
16
     */
17
    public $prefix;
18
	
19
    /**
20
     * Command options.
21
     *
22
     * @var string
23
     */
24
    public $options;
25
	
26
    /**
27
     * Command arguments.
28
     *
29
     * @var string
30
     */
31
    public $arguments;
32
	
33
    /**
34
     * Returns $command as string.
35
     *
36
     * @return string $command command line string
37
     */
38
    public function __toString()
39
    {
40
        $command  = str_pad($this->prefix, strlen($this->prefix) + 1, ' ', STR_PAD_RIGHT);
41
        $command .= !empty($this->options) ? str_pad($this->options, strlen($this->options) + 1, ' ', STR_PAD_RIGHT) : '';
42
        $command .= !empty($this->arguments) ? str_pad($this->arguments, strlen($this->arguments) + 1, ' ', STR_PAD_RIGHT) : '';
43
        return $command;
44
    }
45
46
    /**
47
     * Set command line prefix.
48
     *
49
     * @param  string $prefix command line prefix
50
     * @return object class object
51
     */
52
    final public function setPrefix($prefix)
53
    {
54
        $this->prefix = $prefix;
55
        return $this;
56
    }
57
58
    /**
59
     * Set command line options
60
     *
61
     * @param  array  $opts command line options
62
     * @return object class object
63
     */
64
    final public function setOptions(array $opts)
65
    {
66
        $options = [];
67
        foreach ($opts as $key => $value)
68
        {
69
            if (preg_match('/^-.*$/', $key, $matches))
70
            {
71
                $options[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value;
72
            }
73
            if (preg_match('/^-.*$/', $value, $matches))
74
            {
75
                $options[] = $value;
76
            }
77
        }
78
        $this->options = implode(' ', array_filter($options, 'strlen'));
79
        return $this;
80
    }
81
82
    /**
83
     * Set command line arguments
84
     *
85
     * @param  array  $args command line arguments
86
     * @return object class object
87
     */
88
    final public function setArguments(array $args)
89
    {
90
        $arguments = [];
91
        foreach ($args as $key => $value)
92
        {
93
            if (preg_match('/^-.*$/', $key, $matches))
94
            {
95
                $arguments[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value;
96
            } else
97
            {
98
                $arguments[] = is_array($value) ? implode(' ', $value) : $value;
99
            }
100
        }
101
        $this->arguments = implode(' ', array_filter($arguments, 'strlen'));
102
        return $this;
103
    }
104
}
105