Completed
Push — master ( ea43a1...cefa14 )
by Daniel
05:36
created

Arguments::hashName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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