1: | <?php |
2: | namespace Worldline\Acquiring\Sdk\Domain; |
3: | |
4: | use Exception; |
5: | use stdClass; |
6: | use UnexpectedValueException; |
7: | use Worldline\Acquiring\Sdk\JSON\JSONUtil; |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | abstract class DataObject |
15: | { |
16: | |
17: | |
18: | |
19: | public function toJson() |
20: | { |
21: | return json_encode($this->toObject()); |
22: | } |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | public function fromJson($value) |
30: | { |
31: | $object = JSONUtil::decode($value); |
32: | return $this->fromObject($object); |
33: | } |
34: | |
35: | |
36: | |
37: | |
38: | public function toObject() |
39: | { |
40: | return new stdClass(); |
41: | } |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | public function fromObject($object) |
49: | { |
50: | if (!is_object($object)) { |
51: | throw new UnexpectedValueException('Expected object, got ' . gettype($object)); |
52: | } |
53: | return $this; |
54: | } |
55: | |
56: | public function __set($name, $value) |
57: | { |
58: | throw new Exception('Cannot add new property ' . $name . ' to instances of class ' . get_class($this)); |
59: | } |
60: | } |
61: | |