<?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) 2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */


/**
 * Zend_Service_Delicious is a concrete implementation of the del.icio.us web service
 *
 * @category   Zend
 * @package    Zend_Service
 * @subpackage Delicious
 * @copyright  Copyright (c) 2006 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 array $content Post content
     * @return Zend_Service_Delicious_Post
     */
    public function __construct (Zend_Service_Delicious $service, $values)
    {
        $this->_service = $service;

        if ($values instanceof DOMElement) {
            $values = self::_parsePostNode($values);
        } else if (!is_array($values)) {
            throw new Zend_Service_Exception("Second argument must be array with at least 2 keys ('url' and 'title')");
        }

        if(!isset($values['url']) || !isset($values['title'])) {
            throw new Zend_Service_Exception("Second argument must be array with at least 2 keys ('url' and 'title')");
        }
        if(isset($values['date']) && ! $values['date'] instanceof Zend_Date) {
            throw new Zend_Service_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($tags)
    {
        $this->_tags = (array) $tags;

        return $this;
    }
    /**
     * Add a tag
     *
     * @param string $tag
     * @return Zend_Service_Delicious_Post
     */
    public function addTag($tag)
    {
        $this->_tags[] = (string) $tag;

        return $this;
    }
    /**
     * Getter for date
     *
     * @return Zend_Date
     */
    public function getDate()
    {
        return $this->_date;
    }
    /**
     * Setter for date
     *
     * @param Zend_Date $date
     * @return Zend_Service_Delicious_Post
     */
    public function setDate(Zend_Date $date)
    {
        $this->_date = $date;

        return $this;
    }
    /**
     * Getter for others
     *
     * This property is only populated when posts are retrived
     * with getPosts() method 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
     * @parm bool $isShared
     * @return Zend_Service_Delicious_Post
     */
    public function setShared($isShared)
    {
        $this->_shared = (bool) $isShared;

        return $this;
    }
    /**
     * Deletes post
     */
    public function delete()
    {
        return $this->_service->deletePost($this->_url);
    }
    /**
     * Saves post
     */
    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
     * @return array
     */
    protected static function _parsePostNode(DOMElement $node) {
        return array(
            'url'   => $node->getAttribute('href'),
            'title' => $node->getAttribute('description'),
            'notes' => $node->getAttribute('extended'),
            'others'=> $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')
        );
    }
}