1: <?php
2: /**
3: * Copyright 2012-2014 Rackspace US, Inc.
4: *
5: * Licensed under the Apache License, Version 2.0 (the "License");
6: * you may not use this file except in compliance with the License.
7: * You may obtain a copy of the License at
8: *
9: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: namespace OpenCloud\DNS\Resource;
19:
20: use OpenCloud\Common\Http\Message\Formatter;
21:
22: /**
23: * The Domain class represents a single domain
24: *
25: * Note that the `Subdomain` class is defined in this same file because of
26: * mutual dependencies.
27: */
28: class Domain extends AbstractResource
29: {
30: public $id;
31: public $accountId;
32: public $ttl;
33: public $updated;
34: public $emailAddress;
35: public $created;
36: public $name;
37: public $comment;
38:
39: protected static $json_name = false;
40: protected static $json_collection_name = 'domains';
41: protected static $url_resource = 'domains';
42:
43: protected $createKeys = array(
44: 'name',
45: 'emailAddress',
46: 'ttl',
47: 'comment'
48: );
49:
50: protected $updateKeys = array(
51: 'emailAddress',
52: 'ttl',
53: 'comment'
54: );
55:
56: private $records = array();
57: private $subdomains = array();
58:
59: /**
60: * returns a Record object
61: *
62: * Note that this method is available at the DNS level, but only for
63: * PTR records.
64: *
65: * @return Record
66: */
67: public function record($info = null)
68: {
69: $resource = new Record($this->getService());
70: $resource->setParent($this)->populate($info);
71:
72: return $resource;
73: }
74:
75: /**
76: * returns a Collection of Record objects
77: *
78: * @param array $filter query-string parameters
79: * @return OpenCloud\DNS\Collection\DnsIterator
80: */
81: public function recordList($filter = array())
82: {
83: $url = $this->getUrl(Record::resourceName(), $filter);
84:
85: return $this->getParent()->collection(
86: 'OpenCloud\DNS\Resource\Record', $url, $this
87: );
88: }
89:
90: /**
91: * returns a Subdomain object (child of current domain)
92: *
93: */
94: public function subdomain($info = array())
95: {
96: $resource = new Subdomain($this->getService());
97: $resource->setParent($this)->populate($info);
98:
99: return $resource;
100: }
101:
102: /**
103: * returns a Collection of subdomains
104: *
105: * The subdomains are all `DNS:Domain` objects that are children of
106: * the current domain.
107: *
108: * @param array $filter key/value pairs for query string parameters
109: * return \OpenCloud\Collection
110: * @return OpenCloud\DNS\Collection\DnsIterator
111: */
112: public function subdomainList($filter = array())
113: {
114: $url = $this->getUrl(Subdomain::resourceName(), $filter);
115:
116: return $this->getParent()->collection(
117: 'OpenCloud\DNS\Resource\Subdomain', $url, $this
118: );
119: }
120:
121: /**
122: * Adds a new record to the list (for multiple record creation)
123: *
124: * @api
125: * @param Record $rec the record to add
126: * @return integer the number of records
127: */
128: public function addRecord(Record $record)
129: {
130: $this->records[] = $record;
131:
132: return count($this->records);
133: }
134:
135: /**
136: * adds a new subdomain (for multiple subdomain creation)
137: *
138: * @api
139: * @param Subdomain $subd the subdomain to add
140: * @return integer the number of subdomains
141: */
142: public function addSubdomain(Subdomain $subdomain)
143: {
144: $this->subdomains[] = $subdomain;
145:
146: return count($this->subdomains);
147: }
148:
149: /**
150: * returns changes since a specified date/time
151: *
152: * @param string $since the date or time
153: * @return DNS\Changes
154: */
155: public function changes($since = null)
156: {
157: $url = clone $this->getUrl();
158: $url->addPath('changes');
159:
160: if ($since) {
161: $url->setQuery(array('since' => $since));
162: }
163:
164: $response = $this->getService()
165: ->getClient()
166: ->get($url)
167: ->send();
168:
169: return Formatter::decode($response);
170: }
171:
172: /**
173: * exports the domain
174: *
175: * @return AsyncResponse
176: */
177: public function export()
178: {
179: return $this->getService()->asyncRequest($this->getUrl('export'));
180: }
181:
182: /**
183: * clones the domain to the specified target domain
184: *
185: * @param string $newdomain the new domain to create from this domain
186: * @param boolean $sub to clone subdomains as well
187: * @param boolean $comments Replace occurrences of the reference domain
188: * name with the new domain name in comments
189: * @param boolean $email Replace occurrences of the reference domain
190: * name with the new domain name in email addresses on the cloned
191: * (new) domain.
192: * @param boolean $records Replace occurrences of the reference domain
193: * name with the new domain name in data fields (of records) on the
194: * cloned (new) domain. Does not affect NS records.
195: * @return AsyncResponse
196: */
197: public function cloneDomain(
198: $newDomainName,
199: $subdomains = true,
200: $comments = true,
201: $email = true,
202: $records = true
203: ) {
204: $url = $this->getUrl();
205: $url->addPath('clone');
206: $url->setQuery(array(
207: 'cloneName' => $newDomainName,
208: 'cloneSubdomains' => $subdomains,
209: 'modifyComment' => $comments,
210: 'modifyEmailAddress' => $email,
211: 'modifyRecordData' => $records
212: ));
213:
214: return $this->getService()->asyncRequest($url, 'POST');
215: }
216:
217: /**
218: * handles creation of multiple records at Create()
219: *
220: * @return \stdClass
221: */
222: protected function createJson()
223: {
224: $object = parent::createJson();
225:
226: // add records, if any
227: if (count($this->records)) {
228: $recordsObject = (object) array('records' => array());
229:
230: foreach ($this->records as $record) {
231: $recordObject = new \stdClass;
232: foreach ($record->getCreateKeys() as $key) {
233: if (isset($record->$key)) {
234: $recordObject->$key = $record->$key;
235: }
236: }
237: $recordsObject->records[] = $recordObject;
238: }
239: $object->domains[0]->recordsList = $recordsObject;
240: }
241:
242: // add subdomains, if any
243: if (count($this->subdomains)) {
244: $subdomainsObject = (object) array('domains' => array());
245:
246: foreach ($this->subdomains as $subdomain) {
247: $subdomainObject = new \stdClass;
248: foreach ($subdomain->getCreateKeys() as $key) {
249: if (isset($subdomain->$key)) {
250: $subdomainObject->$key = $subdomain->$key;
251: }
252: }
253: $subdomainsObject->domains[] = $subdomainObject;
254: }
255: $object->domains[0]->subdomains = $subdomainsObject;
256: }
257:
258: return $object;
259: }
260: }
261: