XmlAttribute::setPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Dgame\Soap\Attribute;
4
5
use Dgame\Soap\Visitor\AttributeVisitorInterface;
6
7
/**
8
 * Class XmlAttribute
9
 * @package Dgame\Soap\Attribute
10
 */
11
class XmlAttribute extends Attribute
12
{
13
    /**
14
     * @var string
15
     */
16
    private $prefix;
17
18
    /**
19
     * XmlAttribute constructor.
20
     *
21
     * @param string      $name
22
     * @param string|null $value
23
     * @param string|null $prefix
24
     */
25
    public function __construct(string $name, string $value = null, string $prefix = null)
26 12
    {
27
        parent::__construct($name, $value);
28 12
29
        $this->setPrefix($prefix ?? '');
30 12
    }
31 12
32
    /**
33 12
     * @return string
34
     */
35
    final public function getPrefixedName(): string
36
    {
37
        if ($this->hasPrefix()) {
38 4
            return sprintf('%s:%s', $this->prefix, $this->getName());
39
        }
40 4
41
        return $this->getName();
42
    }
43
44
    /**
45
     * @param string $prefix
46 4
     */
47
    final public function setPrefix(string $prefix)
48 4
    {
49
        $prefix = trim($prefix);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $prefix. This often makes code more readable.
Loading history...
50
        if (strlen($prefix) !== 0) {
51
            $this->prefix = $prefix;
52
        }
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    final public function hasPrefix(): bool
59
    {
60
        return $this->prefix !== null;
61
    }
62 4
63
    /**
64 4
     * @return string
65 4
     */
66
    final public function getPrefix(): string
67
    {
68 1
        return $this->prefix;
69
    }
70
71
    /**
72
     * @param AttributeVisitorInterface $visitor
73
     */
74 9
    public function accept(AttributeVisitorInterface $visitor)
75
    {
76 9
        $visitor->visitXmlAttribute($this);
77
    }
78
}