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