|
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 |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws BpostLogicException |
|
116
|
|
|
*/ |
|
117
|
|
|
public abstract function validate(); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for
@paramannotations 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.