1 <?php
 2 namespace Slack;
 3 
 4 /**
 5  * A serializable model object that stores its data in a hash.
 6  */
 7 abstract class DataObject implements \JsonSerializable
 8 {
 9     /**
10      * @var array The object's data cache.
11      */
12     public $data = [];
13 
14     /**
15      * Creates a data object from an array of data.
16      *
17      * @param array $data The array containing object data.
18      *
19      * @return self A new object instance.
20      */
21     public static function fromData(array $data)
22     {
23         $reflection = new \ReflectionClass(static::class);
24         $instance = $reflection->newInstanceWithoutConstructor();
25         $instance->data = $data;
26         $instance->jsonUnserialize($data);
27         return $instance;
28     }
29 
30     /**
31      * Returns scalar data to be serialized.
32      *
33      * @return array
34      */
35     public function jsonSerialize()
36     {
37         return $this->data;
38     }
39 
40     /**
41      * Re-initializes the object when unserialized or created from a data array.
42      *
43      * @param array $data The object data.
44      */
45     public function jsonUnserialize(array $data)
46     {
47     }
48 }
49