Domain.php
6.59 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Glen Campbell <glen.campbell@rackspace.com>
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
*/
namespace OpenCloud\DNS\Resource;
use OpenCloud\Common\Http\Message\Formatter;
/**
* The Domain class represents a single domain
*
* Note that the `Subdomain` class is defined in this same file because of
* mutual dependencies.
*/
class Domain extends Object
{
public $id;
public $accountId;
public $ttl;
public $updated;
public $emailAddress;
public $created;
public $name;
public $comment;
protected static $json_name = FALSE;
protected static $json_collection_name = 'domains';
protected static $url_resource = 'domains';
protected $createKeys = array(
'name',
'emailAddress',
'ttl',
'comment'
);
protected $updateKeys = array(
'emailAddress',
'ttl',
'comment'
);
private $records = array();
private $subdomains = array();
/**
* returns a Record object
*
* Note that this method is available at the DNS level, but only for
* PTR records.
*
* @return Record
*/
public function record($info = null)
{
$resource = new Record($this->getService());
$resource->setParent($this)->populate($info);
return $resource;
}
/**
* returns a Collection of Record objects
*
* @param array $filter query-string parameters
* @return \OpenCloud\Collection
*/
public function recordList($filter = array())
{
$url = $this->getUrl(Record::resourceName(), $filter);
return $this->getParent()->collection(
'OpenCloud\DNS\Resource\Record', $url, $this
);
}
/**
* returns a Subdomain object (child of current domain)
*
*/
public function subdomain($info = array())
{
$resource = new Subdomain($this->getService());
$resource->setParent($this)->populate($info);
return $resource;
}
/**
* returns a Collection of subdomains
*
* The subdomains are all `DNS:Domain` objects that are children of
* the current domain.
*
* @param array $filter key/value pairs for query string parameters
* return \OpenCloud\Collection
*/
public function subdomainList($filter = array())
{
$url = $this->getUrl(Subdomain::resourceName(), $filter);
return $this->getParent()->collection(
'OpenCloud\DNS\Resource\Subdomain', $url, $this
);
}
/**
* Adds a new record to the list (for multiple record creation)
*
* @api
* @param Record $rec the record to add
* @return integer the number of records
*/
public function addRecord(Record $record)
{
$this->records[] = $record;
return count($this->records);
}
/**
* adds a new subdomain (for multiple subdomain creation)
*
* @api
* @param Subdomain $subd the subdomain to add
* @return integer the number of subdomains
*/
public function addSubdomain(Subdomain $subdomain)
{
$this->subdomains[] = $subdomain;
return count($this->subdomains);
}
/**
* returns changes since a specified date/time
*
* @param string $since the date or time
* @return DNS\Changes
*/
public function changes($since = null)
{
$url = $this->url('changes', isset($since) ? array('since' => $since) : array());
$response = $this->getService()
->getClient()
->get($url)
->send();
return Formatter::decode($response);
}
/**
* exports the domain
*
* @return AsyncResponse
*/
public function export()
{
return $this->getService()->asyncRequest($this->url('export'));
}
/**
* clones the domain to the specified target domain
*
* @param string $newdomain the new domain to create from this domain
* @param boolean $sub to clone subdomains as well
* @param boolean $comments Replace occurrences of the reference domain
* name with the new domain name in comments
* @param boolean $email Replace occurrences of the reference domain
* name with the new domain name in email addresses on the cloned
* (new) domain.
* @param boolean $records Replace occurrences of the reference domain
* name with the new domain name in data fields (of records) on the
* cloned (new) domain. Does not affect NS records.
* @return AsyncResponse
*/
public function cloneDomain(
$newdomain,
$sub = true,
$comments = true,
$email = true,
$records = true
) {
$url = $this->url('clone', array(
'cloneName' => $newdomain,
'cloneSubdomains' => $sub,
'modifyComment' => $comments,
'modifyEmailAddress' => $email,
'modifyRecordData' => $records
));
return $this->getService()->asyncRequest($url, 'POST');
}
/**
* handles creation of multiple records at Create()
*
* @api
* @return \stdClass
*/
protected function createJson()
{
$object = parent::createJson();
// add records, if any
if (count($this->records)) {
$recordsObject = (object) array('records' => array());
foreach ($this->records as $record) {
$recordObject = new \stdClass;
foreach($record->getCreateKeys() as $key) {
if (isset($record->$key)) {
$recordObject->$key = $record->$key;
}
}
$recordsObject->records[] = $recordObject;
}
$object->domains[0]->recordsList = $recordsObject;
}
// add subdomains, if any
if (count($this->subdomains)) {
$subdomainsObject = (object) array('domains' => array());
foreach($this->subdomains as $subdomain) {
$subdomainObject = new \stdClass;
foreach($subdomain->getCreateKeys() as $key) {
if (isset($subdomain->$key)) {
$subdomainObject->$key = $subdomain->$key;
}
}
$subdomainsObject->domains[] = $subdomainObject;
}
$object->domains[0]->subdomains = $subdomainsObject;
}
return $object;
}
}