File indexing completed on 2024-12-22 05:37:07

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tag
0017  * @subpackage Item
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Tag_Taggable
0025  */
0026 // require_once 'Zend/Tag/Taggable.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Tag
0031  * @uses       Zend_Tag_Taggable
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Tag_Item implements Zend_Tag_Taggable
0036 {
0037     /**
0038      * Title of the tag
0039      *
0040      * @var string
0041      */
0042     protected $_title = null;
0043 
0044     /**
0045      * Weight of the tag
0046      *
0047      * @var float
0048      */
0049     protected $_weight = null;
0050 
0051     /**
0052      * Custom parameters
0053      *
0054      * @var string
0055      */
0056     protected $_params = array();
0057 
0058     /**
0059      * Option keys to skip when calling setOptions()
0060      *
0061      * @var array
0062      */
0063     protected $_skipOptions = array(
0064         'options',
0065         'param'
0066     );
0067 
0068     /**
0069      * Create a new tag according to the options
0070      *
0071      * @param  array|Zend_Config $options
0072      * @throws Zend_Tag_Exception When invalid options are provided
0073      * @throws Zend_Tag_Exception When title was not set
0074      * @throws Zend_Tag_Exception When weight was not set
0075      * @return void
0076      */
0077     public function __construct($options)
0078     {
0079         if ($options instanceof Zend_Config) {
0080             $options = $options->toArray();
0081         }
0082 
0083         if (!is_array($options)) {
0084             // require_once 'Zend/Tag/Exception.php';
0085             throw new Zend_Tag_Exception('Invalid options provided to constructor');
0086         }
0087 
0088         $this->setOptions($options);
0089 
0090         if ($this->_title === null) {
0091             // require_once 'Zend/Tag/Exception.php';
0092             throw new Zend_Tag_Exception('Title was not set');
0093         }
0094 
0095         if ($this->_weight === null) {
0096             // require_once 'Zend/Tag/Exception.php';
0097             throw new Zend_Tag_Exception('Weight was not set');
0098         }
0099     }
0100 
0101     /**
0102      * Set options of the tag
0103      *
0104      * @param  array $options
0105      * @return Zend_Tag_Item
0106      */
0107     public function setOptions(array $options)
0108     {
0109         foreach ($options as $key => $value) {
0110             if (in_array(strtolower($key), $this->_skipOptions)) {
0111                 continue;
0112             }
0113 
0114             $method = 'set' . $key;
0115             if (method_exists($this, $method)) {
0116                 $this->$method($value);
0117             }
0118         }
0119 
0120         return $this;
0121     }
0122 
0123     /**
0124      * Defined by Zend_Tag_Taggable
0125      *
0126      * @return string
0127      */
0128     public function getTitle()
0129     {
0130         return $this->_title;
0131     }
0132 
0133     /**
0134      * Set the title
0135      *
0136      * @param  string $title
0137      * @throws Zend_Tag_Exception When title is no string
0138      * @return Zend_Tag_Item
0139      */
0140     public function setTitle($title)
0141     {
0142         if (!is_string($title)) {
0143             // require_once 'Zend/Tag/Exception.php';
0144             throw new Zend_Tag_Exception('Title must be a string');
0145         }
0146 
0147         $this->_title = (string) $title;
0148         return $this;
0149     }
0150 
0151     /**
0152      * Defined by Zend_Tag_Taggable
0153      *
0154      * @return float
0155      */
0156     public function getWeight()
0157     {
0158         return $this->_weight;
0159     }
0160 
0161     /**
0162      * Set the weight
0163      *
0164      * @param  float $weight
0165      * @throws Zend_Tag_Exception When weight is not numeric
0166      * @return Zend_Tag_Item
0167      */
0168     public function setWeight($weight)
0169     {
0170         if (!is_numeric($weight)) {
0171             // require_once 'Zend/Tag/Exception.php';
0172             throw new Zend_Tag_Exception('Weight must be numeric');
0173         }
0174 
0175         $this->_weight = (float) $weight;
0176         return $this;
0177     }
0178 
0179     /**
0180      * Set multiple params at once
0181      *
0182      * @param  array $params
0183      * @return Zend_Tag_Item
0184      */
0185     public function setParams(array $params)
0186     {
0187         foreach ($params as $name => $value) {
0188             $this->setParam($name, $value);
0189         }
0190 
0191         return $this;
0192     }
0193 
0194     /**
0195      * Defined by Zend_Tag_Taggable
0196      *
0197      * @param  string $name
0198      * @param  mixed  $value
0199      * @return Zend_Tag_Item
0200      */
0201     public function setParam($name, $value)
0202     {
0203         $this->_params[$name] = $value;
0204         return $this;
0205     }
0206 
0207     /**
0208      * Defined by Zend_Tag_Taggable
0209      *
0210      * @param  string $name
0211      * @return mixed
0212      */
0213     public function getParam($name)
0214     {
0215         if (isset($this->_params[$name])) {
0216             return $this->_params[$name];
0217         }
0218         return null;
0219     }
0220 }