Skip to content

Commit 00e828a

Browse files
committed
added AbstractSerializableValue base subclass
1 parent 92e3313 commit 00e828a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace mle86\Value;
3+
4+
5+
/**
6+
* This extension of AbstractValue provides easy serializability
7+
* for the Value objects. It implements the PHP 5.4 JsonSerializable interface.
8+
*
9+
* @author Maximilian Eul
10+
*/
11+
abstract class AbstractSerializableValue
12+
extends AbstractValue
13+
implements \JsonSerializable
14+
{
15+
16+
/**
17+
* Returns the wrapped value -- like value(), but with an explicit
18+
* string typecast. This allows string concatenation of Value objects.
19+
* (The typecast is necessary to prevent type mismatch errors
20+
* for number-wrapping classes.)
21+
*
22+
* @return string
23+
*/
24+
public function __toString () {
25+
return (string)$this->value();
26+
}
27+
28+
29+
/**
30+
* Returns the wrapped value -- like value().
31+
* This enables json_encode() to encode the Value object.
32+
*
33+
* @return mixed
34+
*/
35+
public function jsonSerialize () {
36+
return $this->value();
37+
}
38+
39+
}
40+

0 commit comments

Comments
 (0)