|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scriptotek\GoogleBooks; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Model implements \IteratorAggregate |
|
6
|
|
|
{ |
|
7
|
|
|
protected $client; |
|
8
|
|
|
private $data; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct(GoogleBooks $client, \stdClass $data) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->client = $client; |
|
13
|
|
|
$this->data = $data; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Returns true if the model is created from a search result response |
|
18
|
|
|
* (and thus do not contain all the data of the full record). |
|
19
|
|
|
* |
|
20
|
|
|
* @return bool |
|
21
|
|
|
*/ |
|
22
|
|
|
public function isSearchResult() |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->has('searchInfo'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Expand a search result response object to a full record. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function expandToFullRecord() |
|
31
|
|
|
{ |
|
32
|
|
|
$url = $this->client->removeBaseUrl($this->selfLink); |
|
|
|
|
|
|
33
|
|
|
$this->data = $this->client->getItem($url); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Special method that allows the object to be iterated over, for example |
|
38
|
|
|
* with a foreach statement. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getIterator() |
|
41
|
|
|
{ |
|
42
|
|
|
return new \ArrayIterator($this->data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get an item from an array using "dot" notation. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $key |
|
49
|
|
|
* @param mixed $default |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$current = $this->data; |
|
55
|
|
|
foreach (explode('.', $key) as $segment) { |
|
56
|
|
|
if (!isset($current->{$segment})) { |
|
57
|
|
|
return $default; |
|
58
|
|
|
} |
|
59
|
|
|
$current = $current->{$segment}; |
|
60
|
|
|
} |
|
61
|
|
|
return $current; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check if an item or items exist in an array using "dot" notation. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
public function has($key) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$current = $this->data; |
|
73
|
|
|
foreach (explode('.', $key) as $segment) { |
|
74
|
|
|
if (!isset($current->{$segment})) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
$current = $current->{$segment}; |
|
78
|
|
|
} |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get a string representation of the object |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __toString() |
|
88
|
|
|
{ |
|
89
|
|
|
return json_encode($this); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Provide object-like access to the data. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __get($key) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->get($key); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.