|
1
|
|
|
<?php |
|
2
|
|
|
namespace Perry\Representation; |
|
3
|
|
|
|
|
4
|
|
|
use Perry\Response; |
|
5
|
|
|
use Perry\Setup; |
|
6
|
|
|
|
|
7
|
|
|
class Base |
|
8
|
|
|
{ |
|
9
|
|
|
protected $genericMembers = array(); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @param null|array|object|string $inputData |
|
13
|
|
|
* @throws \Exception |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct($inputData) |
|
16
|
|
|
{ |
|
17
|
|
|
$inputData = $this->cleanInputData($inputData); |
|
18
|
|
|
|
|
19
|
|
|
foreach ($inputData as $key => $value) { |
|
20
|
|
|
$method = 'set'.ucfirst($key); |
|
21
|
|
|
// if there is a setter method for this call the setter |
|
22
|
|
|
if (method_exists($this, $method)) { |
|
23
|
|
|
$this->{$method}($value); |
|
24
|
|
|
} else { |
|
25
|
|
|
$this->genericMembers[$key] = $value; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* clean input data |
|
32
|
|
|
* |
|
33
|
|
|
* @param array|object|null|string $inputData |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
* @returns array |
|
36
|
|
|
*/ |
|
37
|
|
|
private function cleanInputData($inputData) |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
switch (true) { |
|
41
|
|
|
case $inputData instanceof Response: |
|
42
|
|
|
$inputData = json_decode((string) $inputData); |
|
43
|
|
|
break; |
|
44
|
|
|
case is_null($inputData): |
|
45
|
|
|
throw new \Exception("got NULL in Base Construtor"); |
|
46
|
|
|
case is_string($inputData): |
|
47
|
|
|
$inputData = json_decode($inputData); |
|
48
|
|
|
break; |
|
49
|
|
|
case is_object($inputData): |
|
50
|
|
|
$inputData = get_object_vars($inputData); |
|
51
|
|
|
break; |
|
52
|
|
|
default: |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!is_array($inputData) && !is_object($inputData)) { |
|
56
|
|
|
throw new \Exception("inputData is not an array, and therefor can't be traversed"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $inputData; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @return \Perry\Representation\Base|string|integer|float|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __get($key) |
|
67
|
|
|
{ |
|
68
|
|
|
if (isset($this->genericMembers[$key])) { |
|
69
|
|
|
return $this->genericMembers[$key]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $method |
|
77
|
|
|
* @param array $args |
|
78
|
|
|
* @return \Perry\Representation\Base |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __call($method, $args) |
|
82
|
|
|
{ |
|
83
|
|
|
if (isset($this->{$method}) && $this->{$method} instanceof Interfaces\CanRefer) { |
|
84
|
|
|
/** |
|
85
|
|
|
* @var Interfaces\CanRefer $reference |
|
86
|
|
|
*/ |
|
87
|
|
|
$reference = $this->{$method}; |
|
88
|
|
|
|
|
89
|
|
|
return $reference->call($args); |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new \Exception("$method does not exist with this object"); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Magic isset method, to ensure keys are found |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __isset($key) |
|
101
|
|
|
{ |
|
102
|
|
|
return isset($this->genericMembers[$key]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $url |
|
107
|
|
|
* @param string $representation |
|
108
|
|
|
* @throws \Exception |
|
109
|
|
|
* @return Response |
|
110
|
|
|
*/ |
|
111
|
|
|
protected static function doGetRequest($url, $representation) |
|
112
|
|
|
{ |
|
113
|
|
|
// use configured fetcher |
|
114
|
|
|
return Setup::getInstance()->fetcher->doGetRequest($url, $representation); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|