Completed
Pull Request — master (#2)
by Štefan
02:24
created

SingleValueObjectTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10

5 Methods

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