Completed
Pull Request — master (#2)
by Štefan
03:07
created

SingleValueObjectTrait::sameValueAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
nc 1
cc 1
eloc 3
nop 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
    public function __construct($value)
18
    {
19
        $this->validation($value);
20
        $this->value = $value;
21
    }
22
23
    /**
24
     * @return mixed
25
     */
26
    public function getValue()
27
    {
28
        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
    public function sameValueAs(SingleValueObjectInterface $object)
38
    {
39
        $areSame = ($this->value === $object->getValue());
40
        return $areSame;
41
    }
42
43
    /**
44
     * @param $string
45
     * @throws InvalidArgumentException
46
     */
47
    protected function throwInvalidArgumentMustBe($string)
48
    {
49
        throw new InvalidArgumentException((new ReflectionClass($this))->getShortName()." must be ".$string);
50
    }
51
52
    /**
53
     * @param $value
54
     * @return void
55
     */
56
    abstract protected function validation($value);
57
}
58