Completed
Push — master ( ab5a7e...8890b1 )
by Adam
02:47
created

ValueObject::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects;
4
5
use phpDocumentor\Compiler\Pass\PackageTreeBuilder;
6
7
/**
8
 * Class ValueObject
9
 *
10
 * @package   BestServedCold\PhalueObjects
11
 * @author    Adam Lewis <[email protected]>
12
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
13
 * @license	  http://http://opensource.org/licenses/GPL-3.0 GPL License
14
 * @link	  http://bestservedcold.com
15
 * @since	  0.0.1-alpha
16
 * @version   0.0.2-alpha
17
 */
18
class ValueObject implements ValueObjectInterface
19
{
20
    /**
21
     * @var \ReflectionClass
22
     */
23
    protected $reflection;
24
25
    /**
26
     * @var mixed
27
     */
28
    protected $value;
29
30
    /**
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * Class Constructor
37
     */
38 109
    public function __construct($value)
39
    {
40 109
        $this->value      = $value;
41 109
        $this->type       = gettype($value);
42 109
        $this->reflection = new \ReflectionClass($this);
43 109
    }
44
45
    /**
46
     * @return string
47
     */
48 1
    public function getType()
49
    {
50 1
        $phpType = gettype($this->getValue());
51 1
        return $phpType === 'object' ? get_class($this->getValue()) : $phpType;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getShortName()
58
    {
59 1
        return $this->reflection->getShortName();
60
    }
61
62
    /**
63
     * @param  $field
64
     * @param  $value
65
     * @throws \RuntimeException
66
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
67
     */
68 1
    public function __set($field, $value)
69
    {
70 1
        throw new \RuntimeException(
71
            "You cannot set a value of a Value Object, that's the whole point!"
72 1
        );
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function hash()
79
    {
80 1
        return spl_object_hash($this);
81
    }
82
83
    /**
84
     * @param  $object
85
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
86
     */
87 1
    public function cloneObject($object)
88
    {
89 1
        return clone($object);
90
    }
91
92
    /**
93
     * @param $string
94
     * @return static
95
     */
96
    public static function fromString($string)
97
    {
98
        return new static((string) $string);
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104 85
    public function getValue()
105
    {
106 85
        return $this->value;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 30
    public function __toString()
113
    {
114 30
        return (string) $this->getValue();
115
    }
116
117
    /**
118
     * @param  ValueObject $object
119
     * @return bool
120
     */
121
    public function equals(ValueObject $object)
0 ignored issues
show
Coding Style introduced by
function equals() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
122
    {
123
        return $this->value === $object->value;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function isArray()
130
    {
131
        return is_array($this->getValue());
132
    }
133
134
    /**
135
     * @param  ValueObject $object
136
     * @return array|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ValueObject.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
137
     */
138
    public function diff(ValueObject $object)
139
    {
140
        switch ($this->type) {
141
            case 'boolean':
142
            case 'double':
143
            case 'integer':
144
                return new static($this->getValue() - $object->getValue());
145
            case 'array':
146
                return new static(
147
                    $object->getType() === 'array'
148
                        ? array_diff_assoc($this->getValue(), $object->getValue())
149
                        : $this->getValue()
150
                );
151
            case 'NULL':
152
                return new static($object->getValue());
153
            case 'object':
154
            case 'resource':
155
            case 'unknown type':
156
            default:
157
                throw new \InvalidArgumentException('Unknown type');
158
        }
159
    }
160
161
    public function count()
162
    {
163
        switch ($this->type) {
164
            case 'boolean':
165
            case 'double':
166
            case 'integer':
167
                return $this->getValue();
168
            case 'array':
169
                return count($this->getValue());
170
            case 'NULL':
171
                return count($this->getValue());
172
            case 'object':
173
            case 'resource':
174
                return null;
175
            case 'unknown type':
176
            default:
177
                throw new \InvalidArgumentException('Unknown type');
178
        }
179
    }
180
}
181