SingleValueObjectTrait::validation()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
/**
3
 * @author: [email protected]
4
 * @license MIT
5
 */
6
7
namespace Webcore\Validation;
8
9
10
use InvalidArgumentException;
11
use ReflectionClass;
12
13
trait SingleValueObjectTrait
14
{
15
    private $value;
16
17 21
    public function __construct($value)
18
    {
19 21
        $this->validation($value);
20 21
        $this->value = $value;
21 21
    }
22
23
    /**
24
     * @return mixed
25
     */
26 9
    public function getValue()
27
    {
28 9
        return $this->value;
29
    }
30
31
    /**
32
     * Compare two SingleValueObject and tells whether they can be considered equal
33
     *
34
     * @param SingleValueObjectInterface $object
35
     * @return bool
36
     */
37 9
    public function sameValueAs(SingleValueObjectInterface $object)
38
    {
39 9
        $areSame = ($this->value === $object->getValue());
40
41 9
        return $areSame;
42
    }
43
44
    /**
45
     * @param string $string
46
     * @throws InvalidArgumentException
47
     */
48 3
    protected function throwInvalidArgumentMustBe($string)
49
    {
50 3
        throw new InvalidArgumentException((new ReflectionClass($this))->getShortName()." must be ".$string);
51
    }
52
53
    /**
54
     * @param mixed $value
55
     * @return null
56
     */
57
    abstract protected function validation($value);
58
}
59