ValidatedValue::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
/**
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) {
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...
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();
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...
89
90
}
91