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
|
|
|
/** |
11
|
|
|
* Class ValidatedValue |
12
|
|
|
*/ |
13
|
|
|
abstract class ValidatedValue |
14
|
|
|
{ |
15
|
|
|
/** @var mixed */ |
16
|
|
|
private $value; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ValidatedValue constructor. |
20
|
|
|
* @param mixed $value |
21
|
|
|
*/ |
22
|
6 |
|
public function __construct($value) |
23
|
|
|
{ |
24
|
6 |
|
$this->setValue($value); |
25
|
6 |
|
$this->validate(); |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
6 |
|
public function getValue() |
32
|
|
|
{ |
33
|
6 |
|
return $this->value; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $value |
38
|
|
|
*/ |
39
|
6 |
|
public function setValue($value) |
40
|
|
|
{ |
41
|
6 |
|
$this->value = $value; |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
1 |
|
public function __toString() |
48
|
|
|
{ |
49
|
1 |
|
return (string)$this->getValue(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $length |
54
|
|
|
* @throws BpostInvalidLengthException |
55
|
|
|
*/ |
56
|
1 |
|
public function validateLength($length) |
57
|
|
|
{ |
58
|
1 |
View Code Duplication |
if (mb_strlen($this->getValue()) > $length) { |
|
|
|
|
59
|
1 |
|
throw new BpostInvalidLengthException('', mb_strlen($this->getValue()), $length); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $allowedValues |
65
|
|
|
* @throws BpostInvalidValueException |
66
|
|
|
*/ |
67
|
3 |
|
public function validateChoice(array $allowedValues) |
68
|
|
|
{ |
69
|
3 |
|
if (!in_array($this->getValue(), $allowedValues)) { |
70
|
2 |
|
throw new BpostInvalidValueException('', $this->getValue(), $allowedValues); |
71
|
|
|
} |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $regexPattern |
76
|
|
|
* @throws BpostInvalidPatternException |
77
|
|
|
*/ |
78
|
1 |
|
public function validatePattern($regexPattern) |
79
|
|
|
{ |
80
|
1 |
|
if (!preg_match("/^$regexPattern\$/", $this->getValue())) { |
81
|
1 |
|
throw new BpostInvalidPatternException('', $this->getValue(), $regexPattern); |
82
|
|
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws BpostLogicException |
87
|
|
|
*/ |
88
|
|
|
public abstract function validate(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
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.