PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ZendFramework/library/Zend/Service/Delicious/Post.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 292 lines | 107 code | 35 blank | 150 comment | 8 complexity | 2fcc7f69701139e8b5f6bddef2988a09 MD5 | raw file
  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_Service
  17. * @subpackage Delicious
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Post.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Date
  24. */
  25. require_once 'Zend/Date.php';
  26. /**
  27. * @see Zend_Service_Delicious_SimplePost
  28. */
  29. require_once 'Zend/Service/Delicious/SimplePost.php';
  30. /**
  31. * Zend_Service_Delicious_Post represents a post of a user that can be edited
  32. *
  33. * @category Zend
  34. * @package Zend_Service
  35. * @subpackage Delicious
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Service_Delicious_Post extends Zend_Service_Delicious_SimplePost
  40. {
  41. /**
  42. * Service that has downloaded the post
  43. *
  44. * @var Zend_Service_Delicious
  45. */
  46. protected $_service;
  47. /**
  48. * @var int Number of people that have the same post
  49. */
  50. protected $_others;
  51. /**
  52. * @var Zend_Date Post date
  53. */
  54. protected $_date;
  55. /**
  56. * @var bool Post share
  57. */
  58. protected $_shared = true;
  59. /**
  60. * @var string Post hash
  61. */
  62. protected $_hash;
  63. /**
  64. * Constructs a new del.icio.us post
  65. *
  66. * @param Zend_Service_Delicious $service Service that has downloaded the post
  67. * @param DOMElement|array $values Post content
  68. * @throws Zend_Service_Delicious_Exception
  69. * @return void
  70. */
  71. public function __construct(Zend_Service_Delicious $service, $values)
  72. {
  73. $this->_service = $service;
  74. if ($values instanceof DOMElement) {
  75. $values = self::_parsePostNode($values);
  76. }
  77. if (!is_array($values) || !isset($values['url']) || !isset($values['title'])) {
  78. /**
  79. * @see Zend_Service_Delicious_Exception
  80. */
  81. require_once 'Zend/Service/Delicious/Exception.php';
  82. throw new Zend_Service_Delicious_Exception("Second argument must be array with at least 2 keys ('url' and"
  83. . " 'title')");
  84. }
  85. if (isset($values['date']) && ! $values['date'] instanceof Zend_Date) {
  86. /**
  87. * @see Zend_Service_Delicious_Exception
  88. */
  89. require_once 'Zend/Service/Delicious/Exception.php';
  90. throw new Zend_Service_Delicious_Exception("Date has to be an instance of Zend_Date");
  91. }
  92. foreach (array('url', 'title', 'notes', 'others', 'tags', 'date', 'shared', 'hash') as $key) {
  93. if (isset($values[$key])) {
  94. $this->{"_$key"} = $values[$key];
  95. }
  96. }
  97. }
  98. /**
  99. * Setter for title
  100. *
  101. * @param string $newTitle
  102. * @return Zend_Service_Delicious_Post
  103. */
  104. public function setTitle($newTitle)
  105. {
  106. $this->_title = (string) $newTitle;
  107. return $this;
  108. }
  109. /**
  110. * Setter for notes
  111. *
  112. * @param string $newNotes
  113. * @return Zend_Service_Delicious_Post
  114. */
  115. public function setNotes($newNotes)
  116. {
  117. $this->_notes = (string) $newNotes;
  118. return $this;
  119. }
  120. /**
  121. * Setter for tags
  122. *
  123. * @param array $tags
  124. * @return Zend_Service_Delicious_Post
  125. */
  126. public function setTags(array $tags)
  127. {
  128. $this->_tags = $tags;
  129. return $this;
  130. }
  131. /**
  132. * Add a tag
  133. *
  134. * @param string $tag
  135. * @return Zend_Service_Delicious_Post
  136. */
  137. public function addTag($tag)
  138. {
  139. $this->_tags[] = (string) $tag;
  140. return $this;
  141. }
  142. /**
  143. * Remove a tag
  144. *
  145. * @param string $tag
  146. * @return Zend_Service_Delicious_Post
  147. */
  148. public function removeTag($tag)
  149. {
  150. $this->_tags = array_diff($this->_tags, array((string) $tag));
  151. return $this;
  152. }
  153. /**
  154. * Getter for date
  155. *
  156. * @return Zend_Date
  157. */
  158. public function getDate()
  159. {
  160. return $this->_date;
  161. }
  162. /**
  163. * Getter for others
  164. *
  165. * This property is only populated when posts are retrieved
  166. * with getPosts() method. The getAllPosts() and getRecentPosts()
  167. * methods will not populate this property.
  168. *
  169. * @return int
  170. */
  171. public function getOthers()
  172. {
  173. return $this->_others;
  174. }
  175. /**
  176. * Getter for hash
  177. *
  178. * @return string
  179. */
  180. public function getHash()
  181. {
  182. return $this->_hash;
  183. }
  184. /**
  185. * Getter for shared
  186. *
  187. * @return bool
  188. */
  189. public function getShared()
  190. {
  191. return $this->_shared;
  192. }
  193. /**
  194. * Setter for shared
  195. *
  196. * @param bool $isShared
  197. * @return Zend_Service_Delicious_Post
  198. */
  199. public function setShared($isShared)
  200. {
  201. $this->_shared = (bool) $isShared;
  202. return $this;
  203. }
  204. /**
  205. * Deletes post
  206. *
  207. * @return Zend_Service_Delicious
  208. */
  209. public function delete()
  210. {
  211. return $this->_service->deletePost($this->_url);
  212. }
  213. /**
  214. * Saves post
  215. *
  216. * @return DOMDocument
  217. */
  218. public function save()
  219. {
  220. $parms = array(
  221. 'url' => $this->_url,
  222. 'description'=> $this->_title,
  223. 'extended' => $this->_notes,
  224. 'shared' => ($this->_shared ? 'yes' : 'no'),
  225. 'tags' => implode(' ', (array) $this->_tags),
  226. 'replace' => 'yes'
  227. );
  228. /*
  229. if ($this->_date instanceof Zend_Date) {
  230. $parms['dt'] = $this->_date->get('Y-m-d\TH:i:s\Z');
  231. }
  232. */
  233. return $this->_service->makeRequest(Zend_Service_Delicious::PATH_POSTS_ADD, $parms);
  234. }
  235. /**
  236. * Extracts content from the DOM element of a post
  237. *
  238. * @param DOMElement $node
  239. * @return array
  240. */
  241. protected static function _parsePostNode(DOMElement $node)
  242. {
  243. return array(
  244. 'url' => $node->getAttribute('href'),
  245. 'title' => $node->getAttribute('description'),
  246. 'notes' => $node->getAttribute('extended'),
  247. 'others' => (int) $node->getAttribute('others'),
  248. 'tags' => explode(' ', $node->getAttribute('tag')),
  249. /**
  250. * @todo replace strtotime() with Zend_Date equivalent
  251. */
  252. 'date' => new Zend_Date(strtotime($node->getAttribute('time'))),
  253. 'shared' => ($node->getAttribute('shared') == 'no' ? false : true),
  254. 'hash' => $node->getAttribute('hash')
  255. );
  256. }
  257. }