Completed
Push — master ( 126b63...733cbc )
by Randy
04:39
created

XmlAttribute::getPrefixedName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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