FeedEntryParent.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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_Gdata
  17. * @subpackage App
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Gdata_App_Extension_Element
  23. */
  24. require_once 'Zend/Gdata/App/Extension/Element.php';
  25. /**
  26. * @see Zend_Gdata_App_Extension_Author
  27. */
  28. require_once 'Zend/Gdata/App/Extension/Author.php';
  29. /**
  30. * @see Zend_Gdata_App_Extension_Category
  31. */
  32. require_once 'Zend/Gdata/App/Extension/Category.php';
  33. /**
  34. * @see Zend_Gdata_App_Extension_Contributor
  35. */
  36. require_once 'Zend/Gdata/App/Extension/Contributor.php';
  37. /**
  38. * @see Zend_Gdata_App_Extension_Id
  39. */
  40. require_once 'Zend/Gdata/App/Extension/Id.php';
  41. /**
  42. * @see Zend_Gdata_App_Extension_Link
  43. */
  44. require_once 'Zend/Gdata/App/Extension/Link.php';
  45. /**
  46. * @see Zend_Gdata_App_Extension_Rights
  47. */
  48. require_once 'Zend/Gdata/App/Extension/Rights.php';
  49. /**
  50. * @see Zend_Gdata_App_Extension_Title
  51. */
  52. require_once 'Zend/Gdata/App/Extension/Title.php';
  53. /**
  54. * @see Zend_Gdata_App_Extension_Updated
  55. */
  56. require_once 'Zend/Gdata/App/Extension/Updated.php';
  57. /**
  58. * Zend_Version
  59. */
  60. require_once 'Zend/Version.php';
  61. /**
  62. * Abstract class for common functionality in entries and feeds
  63. *
  64. * @category Zend
  65. * @package Zend_Gdata
  66. * @subpackage App
  67. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  68. * @license http://framework.zend.com/license/new-bsd New BSD License
  69. */
  70. abstract class Zend_Gdata_App_FeedEntryParent extends Zend_Gdata_App_Base
  71. {
  72. /**
  73. * Service instance used to make network requests.
  74. *
  75. * @see setService(), getService()
  76. */
  77. protected $_service = null;
  78. /**
  79. * The HTTP ETag associated with this entry. Used for optimistic
  80. * concurrency in protoco v2 or greater.
  81. *
  82. * @var string|null
  83. */
  84. protected $_etag = NULL;
  85. protected $_author = array();
  86. protected $_category = array();
  87. protected $_contributor = array();
  88. protected $_id = null;
  89. protected $_link = array();
  90. protected $_rights = null;
  91. protected $_title = null;
  92. protected $_updated = null;
  93. /**
  94. * Indicates the major protocol version that should be used.
  95. * At present, recognized values are either 1 or 2. However, any integer
  96. * value >= 1 is considered valid.
  97. *
  98. * @see setMajorProtocolVersion()
  99. * @see getMajorProtocolVersion()
  100. */
  101. protected $_majorProtocolVersion = 1;
  102. /**
  103. * Indicates the minor protocol version that should be used. Can be set
  104. * to either an integer >= 0, or NULL if no minor version should be sent
  105. * to the server.
  106. *
  107. * @see setMinorProtocolVersion()
  108. * @see getMinorProtocolVersion()
  109. */
  110. protected $_minorProtocolVersion = null;
  111. /**
  112. * Constructs a Feed or Entry
  113. */
  114. public function __construct($element = null)
  115. {
  116. if (!($element instanceof DOMElement)) {
  117. if ($element) {
  118. $this->transferFromXML($element);
  119. }
  120. } else {
  121. $this->transferFromDOM($element);
  122. }
  123. }
  124. /**
  125. * Set the HTTP client instance
  126. *
  127. * Sets the HTTP client object to use for retrieving the feed.
  128. *
  129. * @deprecated Deprecated as of Zend Framework 1.7. Use
  130. * setService() instead.
  131. * @param Zend_Http_Client $httpClient
  132. * @return Zend_Gdata_App_Feed Provides a fluent interface
  133. */
  134. public function setHttpClient(Zend_Http_Client $httpClient)
  135. {
  136. if (!$this->_service) {
  137. $this->_service = new Zend_Gdata_App();
  138. }
  139. $this->_service->setHttpClient($httpClient);
  140. return $this;
  141. }
  142. /**
  143. * Gets the HTTP client object. If none is set, a new Zend_Http_Client
  144. * will be used.
  145. *
  146. * @deprecated Deprecated as of Zend Framework 1.7. Use
  147. * getService() instead.
  148. * @return Zend_Http_Client_Abstract
  149. */
  150. public function getHttpClient()
  151. {
  152. if (!$this->_service) {
  153. $this->_service = new Zend_Gdata_App();
  154. }
  155. $client = $this->_service->getHttpClient();
  156. return $client;
  157. }
  158. /**
  159. * Set the active service instance for this object. This will be used to
  160. * perform network requests, such as when calling save() and delete().
  161. *
  162. * @param Zend_Gdata_App $instance The new service instance.
  163. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface.
  164. */
  165. public function setService($instance)
  166. {
  167. $this->_service = $instance;
  168. return $this;
  169. }
  170. /**
  171. * Get the active service instance for this object. This will be used to
  172. * perform network requests, such as when calling save() and delete().
  173. *
  174. * @return Zend_Gdata_App|null The current service instance, or null if
  175. * not set.
  176. */
  177. public function getService()
  178. {
  179. return $this->_service;
  180. }
  181. public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
  182. {
  183. $element = parent::getDOM($doc, $majorVersion, $minorVersion);
  184. foreach ($this->_author as $author) {
  185. $element->appendChild($author->getDOM($element->ownerDocument));
  186. }
  187. foreach ($this->_category as $category) {
  188. $element->appendChild($category->getDOM($element->ownerDocument));
  189. }
  190. foreach ($this->_contributor as $contributor) {
  191. $element->appendChild($contributor->getDOM($element->ownerDocument));
  192. }
  193. if ($this->_id != null) {
  194. $element->appendChild($this->_id->getDOM($element->ownerDocument));
  195. }
  196. foreach ($this->_link as $link) {
  197. $element->appendChild($link->getDOM($element->ownerDocument));
  198. }
  199. if ($this->_rights != null) {
  200. $element->appendChild($this->_rights->getDOM($element->ownerDocument));
  201. }
  202. if ($this->_title != null) {
  203. $element->appendChild($this->_title->getDOM($element->ownerDocument));
  204. }
  205. if ($this->_updated != null) {
  206. $element->appendChild($this->_updated->getDOM($element->ownerDocument));
  207. }
  208. return $element;
  209. }
  210. protected function takeChildFromDOM($child)
  211. {
  212. $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
  213. switch ($absoluteNodeName) {
  214. case $this->lookupNamespace('atom') . ':' . 'author':
  215. $author = new Zend_Gdata_App_Extension_Author();
  216. $author->transferFromDOM($child);
  217. $this->_author[] = $author;
  218. break;
  219. case $this->lookupNamespace('atom') . ':' . 'category':
  220. $category = new Zend_Gdata_App_Extension_Category();
  221. $category->transferFromDOM($child);
  222. $this->_category[] = $category;
  223. break;
  224. case $this->lookupNamespace('atom') . ':' . 'contributor':
  225. $contributor = new Zend_Gdata_App_Extension_Contributor();
  226. $contributor->transferFromDOM($child);
  227. $this->_contributor[] = $contributor;
  228. break;
  229. case $this->lookupNamespace('atom') . ':' . 'id':
  230. $id = new Zend_Gdata_App_Extension_Id();
  231. $id->transferFromDOM($child);
  232. $this->_id = $id;
  233. break;
  234. case $this->lookupNamespace('atom') . ':' . 'link':
  235. $link = new Zend_Gdata_App_Extension_Link();
  236. $link->transferFromDOM($child);
  237. $this->_link[] = $link;
  238. break;
  239. case $this->lookupNamespace('atom') . ':' . 'rights':
  240. $rights = new Zend_Gdata_App_Extension_Rights();
  241. $rights->transferFromDOM($child);
  242. $this->_rights = $rights;
  243. break;
  244. case $this->lookupNamespace('atom') . ':' . 'title':
  245. $title = new Zend_Gdata_App_Extension_Title();
  246. $title->transferFromDOM($child);
  247. $this->_title = $title;
  248. break;
  249. case $this->lookupNamespace('atom') . ':' . 'updated':
  250. $updated = new Zend_Gdata_App_Extension_Updated();
  251. $updated->transferFromDOM($child);
  252. $this->_updated = $updated;
  253. break;
  254. default:
  255. parent::takeChildFromDOM($child);
  256. break;
  257. }
  258. }
  259. /**
  260. * @return Zend_Gdata_App_Extension_Author
  261. */
  262. public function getAuthor()
  263. {
  264. return $this->_author;
  265. }
  266. /**
  267. * Sets the list of the authors of this feed/entry. In an atom feed, each
  268. * author is represented by an atom:author element
  269. *
  270. * @param array $value
  271. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  272. */
  273. public function setAuthor($value)
  274. {
  275. $this->_author = $value;
  276. return $this;
  277. }
  278. /**
  279. * Returns the array of categories that classify this feed/entry. Each
  280. * category is represented in an atom feed by an atom:category element.
  281. *
  282. * @return array Array of Zend_Gdata_App_Extension_Category
  283. */
  284. public function getCategory()
  285. {
  286. return $this->_category;
  287. }
  288. /**
  289. * Sets the array of categories that classify this feed/entry. Each
  290. * category is represented in an atom feed by an atom:category element.
  291. *
  292. * @param array $value Array of Zend_Gdata_App_Extension_Category
  293. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  294. */
  295. public function setCategory($value)
  296. {
  297. $this->_category = $value;
  298. return $this;
  299. }
  300. /**
  301. * Returns the array of contributors to this feed/entry. Each contributor
  302. * is represented in an atom feed by an atom:contributor XML element
  303. *
  304. * @return array An array of Zend_Gdata_App_Extension_Contributor
  305. */
  306. public function getContributor()
  307. {
  308. return $this->_contributor;
  309. }
  310. /**
  311. * Sets the array of contributors to this feed/entry. Each contributor
  312. * is represented in an atom feed by an atom:contributor XML element
  313. *
  314. * @param array $value
  315. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  316. */
  317. public function setContributor($value)
  318. {
  319. $this->_contributor = $value;
  320. return $this;
  321. }
  322. /**
  323. * @return Zend_Gdata_App_Extension_Id
  324. */
  325. public function getId()
  326. {
  327. return $this->_id;
  328. }
  329. /**
  330. * @param Zend_Gdata_App_Extension_Id $value
  331. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  332. */
  333. public function setId($value)
  334. {
  335. $this->_id = $value;
  336. return $this;
  337. }
  338. /**
  339. * Given a particular 'rel' value, this method returns a matching
  340. * Zend_Gdata_App_Extension_Link element. If the 'rel' value
  341. * is not provided, the full array of Zend_Gdata_App_Extension_Link
  342. * elements is returned. In an atom feed, each link is represented
  343. * by an atom:link element. The 'rel' value passed to this function
  344. * is the atom:link/@rel attribute. Example rel values include 'self',
  345. * 'edit', and 'alternate'.
  346. *
  347. * @param string $rel The rel value of the link to be found. If null,
  348. * the array of Zend_Gdata_App_Extension_link elements is returned
  349. * @return mixed Either a single Zend_Gdata_App_Extension_link element,
  350. * an array of the same or null is returned depending on the rel value
  351. * supplied as the argument to this function
  352. */
  353. public function getLink($rel = null)
  354. {
  355. if ($rel == null) {
  356. return $this->_link;
  357. } else {
  358. foreach ($this->_link as $link) {
  359. if ($link->rel == $rel) {
  360. return $link;
  361. }
  362. }
  363. return null;
  364. }
  365. }
  366. /**
  367. * Returns the Zend_Gdata_App_Extension_Link element which represents
  368. * the URL used to edit this resource. This link is in the atom feed/entry
  369. * as an atom:link with a rel attribute value of 'edit'.
  370. *
  371. * @return Zend_Gdata_App_Extension_Link The link, or null if not found
  372. */
  373. public function getEditLink()
  374. {
  375. return $this->getLink('edit');
  376. }
  377. /**
  378. * Returns the Zend_Gdata_App_Extension_Link element which represents
  379. * the URL used to retrieve the next chunk of results when paging through
  380. * a feed. This link is in the atom feed as an atom:link with a
  381. * rel attribute value of 'next'.
  382. *
  383. * @return Zend_Gdata_App_Extension_Link The link, or null if not found
  384. */
  385. public function getNextLink()
  386. {
  387. return $this->getLink('next');
  388. }
  389. /**
  390. * Returns the Zend_Gdata_App_Extension_Link element which represents
  391. * the URL used to retrieve the previous chunk of results when paging
  392. * through a feed. This link is in the atom feed as an atom:link with a
  393. * rel attribute value of 'previous'.
  394. *
  395. * @return Zend_Gdata_App_Extension_Link The link, or null if not found
  396. */
  397. public function getPreviousLink()
  398. {
  399. return $this->getLink('previous');
  400. }
  401. /**
  402. * @return Zend_Gdata_App_Extension_Link
  403. */
  404. public function getLicenseLink()
  405. {
  406. return $this->getLink('license');
  407. }
  408. /**
  409. * Returns the Zend_Gdata_App_Extension_Link element which represents
  410. * the URL used to retrieve the entry or feed represented by this object
  411. * This link is in the atom feed/entry as an atom:link with a
  412. * rel attribute value of 'self'.
  413. *
  414. * @return Zend_Gdata_App_Extension_Link The link, or null if not found
  415. */
  416. public function getSelfLink()
  417. {
  418. return $this->getLink('self');
  419. }
  420. /**
  421. * Returns the Zend_Gdata_App_Extension_Link element which represents
  422. * the URL for an alternate view of the data represented by this feed or
  423. * entry. This alternate view is commonly a user-facing webpage, blog
  424. * post, etc. The MIME type for the data at the URL is available from the
  425. * returned Zend_Gdata_App_Extension_Link element.
  426. * This link is in the atom feed/entry as an atom:link with a
  427. * rel attribute value of 'self'.
  428. *
  429. * @return Zend_Gdata_App_Extension_Link The link, or null if not found
  430. */
  431. public function getAlternateLink()
  432. {
  433. return $this->getLink('alternate');
  434. }
  435. /**
  436. * @param array $value The array of Zend_Gdata_App_Extension_Link elements
  437. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  438. */
  439. public function setLink($value)
  440. {
  441. $this->_link = $value;
  442. return $this;
  443. }
  444. /**
  445. * @return Zend_Gdata_AppExtension_Rights
  446. */
  447. public function getRights()
  448. {
  449. return $this->_rights;
  450. }
  451. /**
  452. * @param Zend_Gdata_App_Extension_Rights $value
  453. * @return Zend_Gdata_App_FeedEntryParent Provides a fluent interface
  454. */
  455. public function setRights($value)
  456. {
  457. $this->_rights = $value;
  458. return $this;
  459. }
  460. /**
  461. * Returns the title of this feed or entry. The title is an extremely
  462. * short textual representation of this resource and is found as
  463. * an atom:title element in a feed or entry
  464. *
  465. * @return Zend_Gdata_App_Extension_Title
  466. */
  467. public function getTitle()
  468. {
  469. return $this->_title;
  470. }
  471. /**
  472. * Returns a string representation of the title of this feed or entry.
  473. * The title is an extremely short textual representation of this
  474. * resource and is found as an atom:title element in a feed or entry
  475. *
  476. * @return string
  477. */
  478. public function getTitleValue()
  479. {
  480. if (($titleObj = $this->getTitle()) != null) {
  481. return $titleObj->getText();
  482. } else {
  483. return null;
  484. }
  485. }
  486. /**
  487. * Returns the title of this feed or entry. The title is an extremely
  488. * short textual representation of this resource and is found as
  489. * an atom:title element in a feed or entry
  490. *
  491. * @param Zend_Gdata_App_Extension_Title $value
  492. * @return Zend_Gdata_App_Feed_Entry_Parent Provides a fluent interface
  493. */
  494. public function setTitle($value)
  495. {
  496. $this->_title = $value;
  497. return $this;
  498. }
  499. /**
  500. * @return Zend_Gdata_App_Extension_Updated
  501. */
  502. public function getUpdated()
  503. {
  504. return $this->_updated;
  505. }
  506. /**
  507. * @param Zend_Gdata_App_Extension_Updated $value
  508. * @return Zend_Gdata_App_Feed_Entry_Parent Provides a fluent interface
  509. */
  510. public function setUpdated($value)
  511. {
  512. $this->_updated = $value;
  513. return $this;
  514. }
  515. /**
  516. * Set the Etag for the current entry to $value. Setting $value to null
  517. * unsets the Etag.
  518. *
  519. * @param string|null $value
  520. * @return Zend_Gdata_App_Entry Provides a fluent interface
  521. */
  522. public function setEtag($value) {
  523. $this->_etag = $value;
  524. return $this;
  525. }
  526. /**
  527. * Return the Etag for the current entry, or null if not set.
  528. *
  529. * @return string|null
  530. */
  531. public function getEtag() {
  532. return $this->_etag;
  533. }
  534. /**
  535. * Set the major protocol version that should be used. Values < 1
  536. * (excluding NULL) will cause a Zend_Gdata_App_InvalidArgumentException
  537. * to be thrown.
  538. *
  539. * @see _majorProtocolVersion
  540. * @param (int|NULL) $value The major protocol version to use.
  541. * @throws Zend_Gdata_App_InvalidArgumentException
  542. */
  543. public function setMajorProtocolVersion($value)
  544. {
  545. if (!($value >= 1) && ($value !== null)) {
  546. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  547. throw new Zend_Gdata_App_InvalidArgumentException(
  548. 'Major protocol version must be >= 1');
  549. }
  550. $this->_majorProtocolVersion = $value;
  551. }
  552. /**
  553. * Get the major protocol version that is in use.
  554. *
  555. * @see _majorProtocolVersion
  556. * @return (int|NULL) The major protocol version in use.
  557. */
  558. public function getMajorProtocolVersion()
  559. {
  560. return $this->_majorProtocolVersion;
  561. }
  562. /**
  563. * Set the minor protocol version that should be used. If set to NULL, no
  564. * minor protocol version will be sent to the server. Values < 0 will
  565. * cause a Zend_Gdata_App_InvalidArgumentException to be thrown.
  566. *
  567. * @see _minorProtocolVersion
  568. * @param (int|NULL) $value The minor protocol version to use.
  569. * @throws Zend_Gdata_App_InvalidArgumentException
  570. */
  571. public function setMinorProtocolVersion($value)
  572. {
  573. if (!($value >= 0)) {
  574. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  575. throw new Zend_Gdata_App_InvalidArgumentException(
  576. 'Minor protocol version must be >= 0 or null');
  577. }
  578. $this->_minorProtocolVersion = $value;
  579. }
  580. /**
  581. * Get the minor protocol version that is in use.
  582. *
  583. * @see _minorProtocolVersion
  584. * @return (int|NULL) The major protocol version in use, or NULL if no
  585. * minor version is specified.
  586. */
  587. public function getMinorProtocolVersion()
  588. {
  589. return $this->_minorProtocolVersion;
  590. }
  591. /**
  592. * Get the full version of a namespace prefix
  593. *
  594. * Looks up a prefix (atom:, etc.) in the list of registered
  595. * namespaces and returns the full namespace URI if
  596. * available. Returns the prefix, unmodified, if it's not
  597. * registered.
  598. *
  599. * The current entry or feed's version will be used when performing the
  600. * namespace lookup unless overridden using $majorVersion and
  601. * $minorVersion. If the entry/fee has a null version, then the latest
  602. * protocol version will be used by default.
  603. *
  604. * @param string $prefix The namespace prefix to lookup.
  605. * @param integer $majorVersion The major protocol version in effect.
  606. * Defaults to null (auto-select).
  607. * @param integer $minorVersion The minor protocol version in effect.
  608. * Defaults to null (auto-select).
  609. * @return string
  610. */
  611. public function lookupNamespace($prefix,
  612. $majorVersion = null,
  613. $minorVersion = null)
  614. {
  615. // Auto-select current version
  616. if ($majorVersion === null) {
  617. $majorVersion = $this->getMajorProtocolVersion();
  618. }
  619. if ($minorVersion === null) {
  620. $minorVersion = $this->getMinorProtocolVersion();
  621. }
  622. // Perform lookup
  623. return parent::lookupNamespace($prefix, $majorVersion, $minorVersion);
  624. }
  625. }