1 | <?php |
||
15 | abstract class LazyResource extends Model |
||
16 | { |
||
17 | /** |
||
18 | * This class is a ghost object that lazy loads the full record only when needed. |
||
19 | * If $initialized is false, it means we haven't yet loaded the full record. |
||
20 | * We can still have incomplete data from a search response. |
||
21 | */ |
||
22 | protected $initialized = false; |
||
23 | |||
24 | /** |
||
25 | * @var array Query string parameters |
||
26 | */ |
||
27 | protected $params = []; |
||
28 | |||
29 | /** |
||
30 | * Set request query string parameters. |
||
31 | * @param $params array |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function setParams($params) |
||
39 | |||
40 | /** |
||
41 | * Get the request query string parameters. |
||
42 | * @return array |
||
43 | */ |
||
44 | public function getParams() |
||
48 | |||
49 | /** |
||
50 | * Check if we have the full representation of our data object. |
||
51 | * |
||
52 | * @param \stdClass $data |
||
53 | * @return boolean |
||
54 | */ |
||
55 | abstract protected function isInitialized($data); |
||
56 | |||
57 | /** |
||
58 | * Load data onto this object. Chainable method. |
||
59 | * |
||
60 | * @param \stdClass $data |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function init($data = null) |
||
85 | |||
86 | /** |
||
87 | * Get and return the model data. |
||
88 | * @return object |
||
89 | */ |
||
90 | protected function fetchData() |
||
94 | |||
95 | /** |
||
96 | * Called when data is available on the object. |
||
97 | * The resource classes can use this method to process the data. |
||
98 | * |
||
99 | * @param mixed $data |
||
100 | */ |
||
101 | protected function onData($data) |
||
104 | |||
105 | /** |
||
106 | * Get the raw data object. |
||
107 | */ |
||
108 | public function getData() |
||
112 | |||
113 | /** |
||
114 | * Check if the object exists. |
||
115 | */ |
||
116 | public function exists() |
||
125 | |||
126 | /** |
||
127 | * Generate the base URL for this resource. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | abstract protected function urlBase(); |
||
132 | |||
133 | /** |
||
134 | * Build a relative URL for a resource. |
||
135 | * |
||
136 | * @param string $path |
||
137 | * @param array $query |
||
138 | * @return string |
||
139 | */ |
||
140 | protected function url($path = '', $query = []) |
||
152 | } |
||
153 |