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

SingleValueObjectTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 45
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 4 1
A sameValueAs() 0 5 1
A throwInvalidArgumentMustBe() 0 4 1
validation() 0 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