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: * Class DataObject
11: *
12: * @package Worldline\Acquiring\Sdk\Domain
13: */
14: abstract class DataObject
15: {
16: /**
17: * @return string
18: */
19: public function toJson()
20: {
21: return json_encode($this->toObject());
22: }
23:
24: /**
25: * @param string $value
26: * @return $this
27: * @throws UnexpectedValueException
28: */
29: public function fromJson($value)
30: {
31: $object = JSONUtil::decode($value);
32: return $this->fromObject($object);
33: }
34:
35: /**
36: * @return object
37: */
38: public function toObject()
39: {
40: return new stdClass();
41: }
42:
43: /**
44: * @param object $object
45: * @return $this
46: * @throws UnexpectedValueException
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: