AbstractValue::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the tmilos/value package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\Value;
13
14
abstract class AbstractValue implements Value
15
{
16
    /** @var int|string */
17
    private $value;
18
19
    /**
20
     * ScalarValue constructor.
21
     *
22
     * @param int|string $value
23
     *
24
     * @throws \UnexpectedValueException
25
     */
26
    public function __construct($value)
27
    {
28
        if (false === static::isValid($value)) {
29
            throw new \UnexpectedValueException(sprintf('Value "%s" is not valid value of class %s', $value, get_called_class()));
30
        }
31
        $this->value = $value;
32
    }
33
34
    /**
35
     * @return int|string
36
     */
37
    public function getValue()
38
    {
39
        return $this->value;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getTitle()
46
    {
47
        return (string) $this->value;
48
    }
49
50
    /**
51
     * @param mixed $other
52
     *
53
     * @return bool
54
     */
55
    public function equals($other)
56
    {
57
        $value = $other instanceof Value ? $other->getValue() : $other;
58
59
        return $this->getValue() === $value;
60
    }
61
62
    /**
63
     * @param mixed $other
64
     *
65
     * @return bool
66
     */
67
    public function notEquals($other)
68
    {
69
        return false === $this->equals($other);
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function __toString()
76
    {
77
        return $this->getTitle();
78
    }
79
}
80