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