1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects;
|
4
|
|
|
|
5
|
|
|
use InvalidArgumentException;
|
6
|
|
|
use BestServedCold\PhalueObjects\Contract\ValueObject as ValueObjectInterface;
|
7
|
|
|
|
8
|
|
|
/**
|
9
|
|
|
* Class ValueObject
|
10
|
|
|
*
|
11
|
|
|
* @package BestServedCold\PhalueObjects
|
12
|
|
|
* @author Adam Lewis <[email protected]>
|
13
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited
|
14
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License
|
15
|
|
|
* @link http://bestservedcold.com
|
16
|
|
|
* @since 0.0.1-alpha
|
17
|
|
|
* @version 0.0.2-alpha
|
18
|
|
|
*/
|
19
|
|
|
class ValueObject implements ValueObjectInterface
|
20
|
|
|
{
|
21
|
|
|
/**
|
22
|
|
|
* @var mixed
|
23
|
|
|
*/
|
24
|
|
|
protected $value;
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* @var string
|
28
|
|
|
*/
|
29
|
|
|
protected $type;
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* Class Constructor
|
33
|
|
|
*/
|
34
|
139 |
|
public function __construct($value)
|
35
|
|
|
{
|
36
|
139 |
|
$this->value = $value;
|
37
|
139 |
|
$this->type = gettype($value);
|
38
|
139 |
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* @param $field
|
42
|
|
|
* @param $value
|
43
|
|
|
* @throws \RuntimeException
|
44
|
|
|
* @return void
|
|
|
|
|
45
|
|
|
*/
|
46
|
1 |
|
public function __set($field, $value)
|
47
|
|
|
{
|
48
|
1 |
|
throw new \RuntimeException(
|
49
|
|
|
"You cannot set a value of a Value Object, that's the whole point!"
|
50
|
1 |
|
);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* @return string
|
55
|
|
|
*/
|
56
|
32 |
|
public function __toString()
|
57
|
|
|
{
|
58
|
32 |
|
return (string) $this->getValue();
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* @return string
|
63
|
|
|
*/
|
64
|
11 |
|
public function getType()
|
65
|
|
|
{
|
66
|
11 |
|
return $this->type;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* @return string
|
72
|
|
|
*/
|
73
|
1 |
|
public function hash()
|
74
|
|
|
{
|
75
|
1 |
|
return spl_object_hash($this);
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* @param $object
|
80
|
|
|
* @return mixed
|
|
|
|
|
81
|
|
|
*/
|
82
|
1 |
|
public function cloneObject($object)
|
83
|
|
|
{
|
84
|
1 |
|
return clone($object);
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* @param $string
|
89
|
|
|
* @return static
|
90
|
|
|
*/
|
91
|
2 |
|
public static function fromString($string)
|
92
|
|
|
{
|
93
|
2 |
|
return new static((string) $string);
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* @return mixed
|
98
|
|
|
*/
|
99
|
108 |
|
public function getValue()
|
100
|
|
|
{
|
101
|
108 |
|
return $this->value;
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
/**
|
105
|
|
|
* @param ValueObject $object
|
106
|
|
|
* @return bool
|
107
|
|
|
*/
|
108
|
2 |
|
public function equals(ValueObject $object)
|
|
|
|
|
109
|
|
|
{
|
110
|
2 |
|
return $this->value === $object->value;
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* @param ValueObject $object
|
115
|
|
|
* @return ValueObject
|
|
|
|
|
116
|
|
|
*/
|
117
|
2 |
|
public function diff(ValueObject $object)
|
118
|
|
|
{
|
119
|
2 |
|
switch ($this->getType()) {
|
120
|
2 |
|
case 'boolean':
|
121
|
2 |
|
case 'double':
|
122
|
2 |
|
case 'integer':
|
123
|
1 |
|
return new static($this->getValue() - $object->getValue());
|
124
|
2 |
|
case 'array':
|
125
|
2 |
|
return new static(
|
126
|
2 |
|
$object->getType() === 'array'
|
127
|
2 |
|
? array_diff_assoc($this->getValue(), $object->getValue())
|
128
|
2 |
|
: $this->getValue()
|
129
|
2 |
|
);
|
130
|
1 |
|
case 'string':
|
131
|
1 |
|
return new static(
|
132
|
1 |
|
str_replace($object->getValue(), '', $this->getValue())
|
133
|
1 |
|
);
|
134
|
1 |
|
case 'NULL':
|
135
|
1 |
|
return new static($object->getValue());
|
136
|
1 |
|
case 'object':
|
137
|
1 |
|
case 'resource':
|
138
|
1 |
|
return null;
|
139
|
1 |
|
case 'unknown type':
|
140
|
1 |
|
default:
|
141
|
1 |
|
throw new InvalidArgumentException('Cannot diff type [' . $this->type . '].');
|
142
|
1 |
|
}
|
143
|
|
|
}
|
144
|
|
|
|
145
|
9 |
|
public function count()
|
146
|
|
|
{
|
147
|
9 |
|
switch ($this->getType()) {
|
148
|
9 |
|
case 'boolean':
|
149
|
9 |
|
case 'double':
|
150
|
9 |
|
case 'integer':
|
151
|
3 |
|
return $this->getValue();
|
152
|
7 |
|
case 'array':
|
153
|
7 |
|
case 'NULL':
|
154
|
7 |
|
return count($this->getValue());
|
155
|
1 |
|
case 'object':
|
156
|
1 |
|
case 'resource':
|
157
|
1 |
|
return null;
|
158
|
1 |
|
case 'string':
|
159
|
1 |
|
return strlen($this->getValue());
|
160
|
1 |
|
case 'unknown type':
|
161
|
1 |
|
default:
|
162
|
1 |
|
throw new InvalidArgumentException('Cannot count type [' . $this->type . '].');
|
163
|
1 |
|
}
|
164
|
|
|
}
|
165
|
|
|
}
|
166
|
|
|
|
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.