BasicAttribute::getValue()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bpost\BpostApiClient\Common;
4
5
use Bpost\BpostApiClient\Exception\BpostLogicException;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidPatternException;
8
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
9
10
abstract class BasicAttribute
11
{
12
    /** @var mixed */
13
    private $value;
14
15
    /** @var string */
16
    private $key;
17
18
    /**
19
     * BasicAttribute constructor.
20
     * @param mixed  $value
21
     * @param string $key
22
     */
23 13
    public function __construct($value, $key = '')
24
    {
25 13
        $this->value = $value;
26 13
        $this->setKey($key);
27 13
        $this->validate();
28 13
    }
29
30
    /**
31
     * @return mixed
32
     */
33 13
    public function getValue()
34
    {
35 13
        return $this->value;
36
    }
37
38
    /**
39
     * @param string $key
40
     */
41 13
    private function setKey($key)
42
    {
43 13
        $this->key = (string)($key ?: $this->getDefaultKey());
44 13
    }
45
46
    /**
47
     * @return string
48
     */
49 8
    public function getKey()
50
    {
51 8
        return $this->key;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function __toString()
58
    {
59 1
        return (string)$this->getValue();
60
    }
61
62
    /**
63
     * Prefix $tagName with the $prefix, if needed
64
     * @param string $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param string $tagName
66
     * @return string
67
     */
68
    public function getPrefixedTagName($tagName, $prefix = null)
69
    {
70
        if (empty($prefix)) {
71
            return $tagName;
72
        }
73
        return $prefix . ':' . $tagName;
74
    }
75
76
    /**
77
     * @param int $length
78
     * @throws BpostInvalidLengthException
79
     */
80 8
    public function validateLength($length)
81
    {
82 8 View Code Duplication
        if (mb_strlen($this->getValue()) > $length) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83 4
            throw new BpostInvalidLengthException($this->getKey(), mb_strlen($this->getValue()), $length);
84
        }
85 8
    }
86
87
    /**
88
     * @param array $allowedValues
89
     * @throws BpostInvalidValueException
90
     */
91 5
    public function validateChoice(array $allowedValues)
92
    {
93 5
        if (!in_array($this->getValue(), $allowedValues)) {
94 2
            throw new BpostInvalidValueException($this->getKey(), $this->getValue(), $allowedValues);
95
        }
96 5
    }
97
98
    /**
99
     * @param string $regexPattern
100
     * @throws BpostInvalidPatternException
101
     */
102 5
    public function validatePattern($regexPattern)
103
    {
104 5
        if (!preg_match("/^$regexPattern\$/", $this->getValue())) {
105 2
            throw new BpostInvalidPatternException($this->getKey(), $this->getValue(), $regexPattern);
106
        }
107 5
    }
108
109
    /**
110
     * @return string
111
     */
112
    protected abstract function getDefaultKey();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
113
114
    /**
115
     * @throws BpostLogicException
116
     */
117
    public abstract function validate();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
118
119
}
120