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

Argument::fromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 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 process argument class
12
 *
13
 * @package GravityMedia\Ghostscript\Process
14
 */
15
class Argument
16
{
17
    /**
18
     * The name
19
     *
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * The value
26
     *
27
     * @var mixed
28
     */
29
    protected $value;
30
31
    /**
32
     * Create process argument object
33
     *
34
     * @param string $name
35
     * @param mixed  $value
36
     */
37 18
    public function __construct($name, $value = null)
38
    {
39 18
        $this->name = $name;
40 18
        $this->value = $value;
41 18
    }
42
43
    /**
44
     * Create process argument object from string
45
     *
46
     * @param string $argument
47
     *
48
     * @return $this
49
     */
50 6
    public static function fromString($argument)
51
    {
52 6
        $argument = explode('=', $argument, 2);
53 6
        $instance = new static(array_shift($argument));
54
55 6
        if (count($argument) > 0) {
56 3
            $instance->setValue(array_shift($argument));
57 3
        }
58
59 6
        return $instance;
60
    }
61
62
    /**
63
     * Return process argument as string
64
     *
65
     * @return string
66
     */
67 6
    public function toString()
68
    {
69 6
        if (!$this->hasValue()) {
70 3
            return $this->getName();
71
        }
72
73 3
        return sprintf('%s=%s', $this->getName(), $this->getValue());
74
    }
75
76
    /**
77
     * Get name
78
     *
79
     * @return string
80
     */
81 18
    public function getName()
82
    {
83 18
        return $this->name;
84
    }
85
86
    /**
87
     * Whether the argument has a value
88
     *
89
     * @return bool
90
     */
91 6
    public function hasValue()
92
    {
93 6
        return null !== $this->value;
94
    }
95
96
    /**
97
     * Get value
98
     *
99
     * @return mixed
100
     */
101 15
    public function getValue()
102
    {
103 15
        return $this->value;
104
    }
105
106
    /**
107
     * Set value
108
     *
109
     * @param mixed $value
110
     *
111
     * @return $this
112
     */
113 3
    public function setValue($value)
114
    {
115 3
        $this->value = $value;
116
117 3
        return $this;
118
    }
119
}
120