Arguments   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 153
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 6 1
A getArgument() 0 10 2
A addArgument() 0 6 1
A hashName() 0 4 1
A setArgument() 0 10 1
A convertArgument() 0 12 3
A addArguments() 0 8 2
A setArguments() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Ghostscript package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ghostscript\Process;
9
10
/**
11
 * The arguments class.
12
 *
13
 * @package GravityMedia\Ghostscript\Process
14
 */
15
class Arguments
16
{
17
    /**
18
     * The argument objects.
19
     *
20
     * @var Argument[]
21
     */
22
    protected $arguments;
23
24
    /**
25
     * Create arguments object.
26
     */
27 18
    public function __construct()
28
    {
29 18
        $this->arguments = [];
30 18
    }
31
32
    /**
33
     * Return arguments as array of strings.
34
     *
35
     * @return string[]
36
     */
37
    public function toArray()
38
    {
39 14
        return array_values(array_map(function (Argument $argument) {
40 12
            return $argument->toString();
41 14
        }, $this->arguments));
42
    }
43
44
    /**
45
     * Convert argument to argument object.
46
     *
47
     * @param string|Argument $argument
48
     *
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return Argument
52
     */
53 16
    protected function convertArgument($argument)
54
    {
55 16
        if (is_string($argument)) {
56 14
            $argument = Argument::fromString($argument);
57 7
        }
58
59 16
        if (!$argument instanceof Argument) {
60 2
            throw new \InvalidArgumentException('Invalid argument');
61
        }
62
63 14
        return $argument;
64
    }
65
66
    /**
67
     * Get argument object.
68
     *
69
     * @param string $name
70
     *
71
     * @return Argument|null
72
     */
73 2
    public function getArgument($name)
74
    {
75 2
        $hash = $this->hashName($name);
76
77 2
        if (isset($this->arguments[$hash])) {
78 2
            return $this->arguments[$hash];
79
        }
80
81 2
        return null;
82
    }
83
84
    /**
85
     * Add argument.
86
     *
87
     * @param string|Argument $argument
88
     *
89
     * @throws \InvalidArgumentException
90
     *
91
     * @return $this
92
     */
93 6
    public function addArgument($argument)
94
    {
95 6
        $this->arguments[] = $this->convertArgument($argument);
96
97 6
        return $this;
98
    }
99
100
    /**
101
     * Add all arguments.
102
     *
103
     * @param array $arguments
104
     *
105
     * @throws \InvalidArgumentException
106
     *
107
     * @return $this
108
     */
109 6
    public function addArguments(array $arguments)
110
    {
111 6
        foreach ($arguments as $argument) {
112 6
            $this->addArgument($argument);
113 3
        }
114
115 6
        return $this;
116
    }
117
118
    /**
119
     * Hash the name.
120
     *
121
     * @param string $name
122
     *
123
     * @return string
124
     */
125 8
    protected function hashName($name)
126
    {
127 8
        return strtolower(ltrim($name, '-'));
128
    }
129
130
    /**
131
     * Set argument.
132
     *
133
     * @param string|Argument $argument
134
     *
135
     * @throws \InvalidArgumentException
136
     *
137
     * @return $this
138
     */
139 8
    public function setArgument($argument)
140
    {
141 8
        $argument = $this->convertArgument($argument);
142
143 8
        $hash = $this->hashName($argument->getName());
144
145 8
        $this->arguments[$hash] = $argument;
146
147 8
        return $this;
148
    }
149
150
    /**
151
     * Set all arguments.
152
     *
153
     * @param array $arguments
154
     *
155
     * @throws \InvalidArgumentException
156
     *
157
     * @return $this
158
     */
159 6
    public function setArguments(array $arguments)
160
    {
161 6
        foreach ($arguments as $argument) {
162 6
            $this->setArgument($argument);
163 3
        }
164
165 6
        return $this;
166
    }
167
}
168