1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Glen Campbell <glen.campbell@rackspace.com>
8: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
9: */
10:
11: namespace OpenCloud\DNS\Resource;
12:
13: use OpenCloud\Common\Lang;
14: use OpenCloud\Common\Exceptions;
15: use OpenCloud\Common\Http\Message\Formatter;
16:
17: /**
18: * PTR records are used for reverse DNS
19: *
20: * The PtrRecord object is nearly identical with the Record object. However,
21: * the PtrRecord is a child of the service, and not a child of a Domain.
22: */
23: class PtrRecord extends Record
24: {
25:
26: public $server;
27:
28: protected static $json_name = false;
29: protected static $json_collection_name = 'records';
30: protected static $url_resource = 'rdns';
31:
32: private $link_rel;
33: private $link_href;
34:
35: /**
36: * constructur ensures that the record type is PTR
37: */
38: public function __construct($parent, $info = null)
39: {
40: $this->type = 'PTR';
41: parent::__construct($parent, $info);
42: if ($this->type != 'PTR') {
43: throw new Exceptions\RecordTypeError(sprintf(
44: Lang::translate('Invalid record type [%s], must be PTR'),
45: $this->type
46: ));
47: }
48: }
49:
50: /**
51: * DNS PTR Create() method requires a server
52: *
53: * Generally called as `Create(array('server'=>$server))`
54: */
55: public function create($params = array())
56: {
57: $this->populate($params);
58: $this->link_rel = $this->server->getService()->name();
59: $this->link_href = $this->server->url();
60: return parent::create();
61: }
62:
63: /**
64: * DNS PTR Update() method requires a server
65: */
66: public function update($params = array())
67: {
68: $this->populate($params);
69: $this->link_rel = $this->server->getService()->Name();
70: $this->link_href = $this->server->Url();
71: return parent::update();
72: }
73:
74: /**
75: * DNS PTR Delete() method requires a server
76: *
77: * Note that delete will remove ALL PTR records associated with the device
78: * unless you pass in the parameter ip={ip address}
79: *
80: */
81: public function delete()
82: {
83: $this->link_rel = $this->server->getService()->Name();
84: $this->link_href = $this->server->Url();
85:
86: $params = array('href' => $this->link_href);
87: if (!empty($this->data)) {
88: $params['ip'] = $this->data;
89: }
90:
91: $url = clone $this->getUrl();
92: $url->addPath('rdns')
93: ->addPath($this->link_rel)
94: ->setQuery($params);
95:
96: $response = $this->getClient()->delete($url)->send();
97: return new AsyncResponse($this->getService(), Formatter::decode($response));
98: }
99:
100: /**
101: * Specialized JSON for DNS PTR creates and updates
102: */
103: protected function createJson()
104: {
105: return (object) array(
106: 'recordsList' => parent::createJson(),
107: 'link' => array(
108: 'href' => $this->link_href,
109: 'rel' => $this->link_rel
110: )
111: );
112: }
113:
114: /**
115: * The Update() JSON requires a record ID
116: */
117: protected function updateJson($params = array())
118: {
119: $this->populate($params);
120: $object = $this->createJson();
121: $object->recordsList->records[0]->id = $this->id;
122: return $object;
123: }
124:
125: }
126: