This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DomaintoolsAPIResponse.class.php
97 lines (80 loc) · 2.48 KB
/
DomaintoolsAPIResponse.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* This file is part of the domaintoolsAPI_php_wrapper package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
@example of call returning a DomaintoolsResponse Object :
// configure the request
$request = new DomaintoolsAPI();
$response = $request->from('whois')
->domain('domaintools.com')
->execute();
// send request to the response object
$response = new DomaintoolsAPIResponse($request);
// return responses
echo $response->whois->date;
echo $response->toXml();
*/
class DomaintoolsAPIResponse {
/**
* Request object for API
*/
protected $request;
/**
* Json string representing the response
*/
protected $json;
/**
* Json object representation
*/
protected $jsonObject;
/**
* Constructs the DomaintoolsAPIResponse object
* @param DomaintoolsAPI $request the request object
*/
public function __construct($request = null) {
if(!$request instanceof DomaintoolsAPI) {
throw new ServiceException(ServiceException::INVALID_REQUEST_OBJECT);
}
$this->mergeJson($request->getRawResponse());
$this->request = $request;
}
/**
* Magic get method to create an alias :
* $this->history <=> $this->jsonObject->response->history
* if $this->history already exists => return it
* elseif $this->jsonObject->response->history exists => return it
* else => return null
*/
public function __get($name) {
if(isset($this->$name)) {
return $this->$name;
}
elseif($this->jsonObject->response->$name) {
return $this->jsonObject->response->$name;
}
return null;
}
/**
* declare $this->json and $this->jsonObject only if json_decode worked
* otherwise we keep the old values
* @param string $json
*/
public function mergeJson($json) {
$object = json_decode($json);
if($object === null) {
throw new ServiceException(ServiceException::INVALID_JSON_STRING);
}
$this->json = $json;
$this->jsonObject = $object;
}
/**
* Getter of the request object (DomaintoolsAPI)
*/
public function getRequest() {
return $this->request;
}
}