PageRenderTime 104ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Feed/Entry/Atom.php

https://github.com/sluther/portsensor
PHP | 231 lines | 111 code | 22 blank | 98 comment | 24 complexity | 2d26fcb0268baaf85299800b27a7bb79 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: EntryAtom.php 3941 2007-03-14 21:36:13Z darby $
  20. */
  21. /**
  22. * @see Zend_Feed_Entry_Abstract
  23. */
  24. require_once 'Zend/Feed/Entry/Abstract.php';
  25. /**
  26. * Concrete class for working with Atom entries.
  27. *
  28. * @category Zend
  29. * @package Zend_Feed
  30. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Feed_Entry_Atom extends Zend_Feed_Entry_Abstract
  34. {
  35. /**
  36. * Root XML element for Atom entries.
  37. *
  38. * @var string
  39. */
  40. protected $_rootElement = 'entry';
  41. /**
  42. * Root namespace for Atom entries.
  43. *
  44. * @var string
  45. */
  46. protected $_rootNamespace = 'atom';
  47. /**
  48. * Delete an atom entry.
  49. *
  50. * Delete tries to delete this entry from its feed. If the entry
  51. * does not contain a link rel="edit", we throw an error (either
  52. * the entry does not yet exist or this is not an editable
  53. * feed). If we have a link rel="edit", we do the empty-body
  54. * HTTP DELETE to that URI and check for a response of 2xx.
  55. * Usually the response would be 204 No Content, but the Atom
  56. * Publishing Protocol permits it to be 200 OK.
  57. *
  58. * @throws Zend_Feed_Exception
  59. * @return void
  60. */
  61. public function delete()
  62. {
  63. // Look for link rel="edit" in the entry object.
  64. $deleteUri = $this->link('edit');
  65. if (!$deleteUri) {
  66. throw new Zend_Feed_Exception('Cannot delete entry; no link rel="edit" is present.');
  67. }
  68. // DELETE
  69. $client = Zend_Feed::getHttpClient();
  70. do {
  71. $client->setUri($deleteUri);
  72. if (Zend_Feed::getHttpMethodOverride()) {
  73. $client->setHeader('X-HTTP-Method-Override', 'DELETE');
  74. $response = $client->request('POST');
  75. } else {
  76. $response = $client->request('DELETE');
  77. }
  78. $httpStatus = $response->getStatus();
  79. switch ((int) $httpStatus / 100) {
  80. // Success
  81. case 2:
  82. return true;
  83. // Redirect
  84. case 3:
  85. $deleteUri = $response->getHeader('Location');
  86. continue;
  87. // Error
  88. default:
  89. throw new Zend_Feed_Exception("Expected response code 2xx, got $httpStatus");
  90. }
  91. } while (true);
  92. }
  93. /**
  94. * Save a new or updated Atom entry.
  95. *
  96. * Save is used to either create new entries or to save changes to
  97. * existing ones. If we have a link rel="edit", we are changing
  98. * an existing entry. In this case we re-serialize the entry and
  99. * PUT it to the edit URI, checking for a 200 OK result.
  100. *
  101. * For posting new entries, you must specify the $postUri
  102. * parameter to save() to tell the object where to post itself.
  103. * We use $postUri and POST the serialized entry there, checking
  104. * for a 201 Created response. If the insert is successful, we
  105. * then parse the response from the POST to get any values that
  106. * the server has generated: an id, an updated time, and its new
  107. * link rel="edit".
  108. *
  109. * @param string $postUri Location to POST for creating new entries.
  110. * @throws Zend_Feed_Exception
  111. * @return void
  112. */
  113. public function save($postUri = null)
  114. {
  115. if ($this->id()) {
  116. // If id is set, look for link rel="edit" in the
  117. // entry object and PUT.
  118. $editUri = $this->link('edit');
  119. if (!$editUri) {
  120. throw new Zend_Feed_Exception('Cannot edit entry; no link rel="edit" is present.');
  121. }
  122. $client = Zend_Feed::getHttpClient();
  123. $client->setUri($editUri);
  124. if (Zend_Feed::getHttpMethodOverride()) {
  125. $client->setHeaders(array('X-HTTP-Method-Override: PUT',
  126. 'Content-Type: application/atom+xml'));
  127. $client->setRawData($this->saveXML());
  128. $response = $client->request('POST');
  129. } else {
  130. $client->setHeaders('Content-Type', 'application/atom+xml');
  131. $client->setRawData($this->saveXML());
  132. $response = $client->request('PUT');
  133. }
  134. if ($response->getStatus() !== 200) {
  135. throw new Zend_Feed_Exception('Expected response code 200, got ' . $response->getStatus());
  136. }
  137. } else {
  138. if ($postUri === null) {
  139. throw new Zend_Feed_Exception('PostURI must be specified to save new entries.');
  140. }
  141. $client = Zend_Feed::getHttpClient();
  142. $client->setUri($postUri);
  143. $client->setRawData($this->saveXML());
  144. $response = $client->request('POST');
  145. if ($response->getStatus() !== 201) {
  146. throw new Zend_Feed_Exception('Expected response code 201, got '
  147. . $response->getStatus());
  148. }
  149. }
  150. // Update internal properties using $client->responseBody;
  151. @ini_set('track_errors', 1);
  152. $newEntry = @DOMDocument::loadXML($response->getBody());
  153. @ini_restore('track_errors');
  154. if (!$newEntry) {
  155. throw new Zend_Feed_Exception('XML cannot be parsed: ' . $php_errormsg);
  156. }
  157. $newEntry = $newEntry->getElementsByTagName($this->_rootElement)->item(0);
  158. if (!$newEntry) {
  159. throw new Zend_Feed_Exception('No root <feed> element found in server response:'
  160. . "\n\n" . $client->responseBody);
  161. }
  162. if ($this->_element->parentNode) {
  163. $oldElement = $this->_element;
  164. $this->_element = $oldElement->ownerDocument->importNode($newEntry, true);
  165. $oldElement->parentNode->replaceChild($this->_element, $oldElement);
  166. } else {
  167. $this->_element = $newEntry;
  168. }
  169. }
  170. /**
  171. * Easy access to <link> tags keyed by "rel" attributes.
  172. *
  173. * If $elt->link() is called with no arguments, we will attempt to
  174. * return the value of the <link> tag(s) like all other
  175. * method-syntax attribute access. If an argument is passed to
  176. * link(), however, then we will return the "href" value of the
  177. * first <link> tag that has a "rel" attribute matching $rel:
  178. *
  179. * $elt->link(): returns the value of the link tag.
  180. * $elt->link('self'): returns the href from the first <link rel="self"> in the entry.
  181. *
  182. * @param string $rel The "rel" attribute to look for.
  183. * @return mixed
  184. */
  185. public function link($rel = null)
  186. {
  187. if ($rel === null) {
  188. return parent::__call('link', null);
  189. }
  190. // index link tags by their "rel" attribute.
  191. $links = parent::__get('link');
  192. if (!is_array($links)) {
  193. if ($links instanceof Zend_Feed_Element) {
  194. $links = array($links);
  195. } else {
  196. return $links;
  197. }
  198. }
  199. foreach ($links as $link) {
  200. if (empty($link['rel'])) {
  201. continue;
  202. }
  203. if ($rel == $link['rel']) {
  204. return $link['href'];
  205. }
  206. }
  207. return null;
  208. }
  209. }