web/lib/Zend/Server/Definition.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Server
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Definition.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * Server methods metadata
       
    24  *
       
    25  * @todo       Implement iterator
       
    26  * @category   Zend
       
    27  * @package    Zend_Server
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  */
       
    31 class Zend_Server_Definition implements Countable, Iterator
       
    32 {
       
    33     /**
       
    34      * @var array Array of Zend_Server_Method_Definition objects
       
    35      */
       
    36     protected $_methods = array();
       
    37 
       
    38     /**
       
    39      * @var bool Whether or not overwriting existing methods is allowed
       
    40      */
       
    41     protected $_overwriteExistingMethods = false;
       
    42 
       
    43     /**
       
    44      * Constructor
       
    45      *
       
    46      * @param  null|array $methods
       
    47      * @return void
       
    48      */
       
    49     public function __construct($methods = null)
       
    50     {
       
    51         if (is_array($methods)) {
       
    52             $this->setMethods($methods);
       
    53         }
       
    54     }
       
    55 
       
    56     /**
       
    57      * Set flag indicating whether or not overwriting existing methods is allowed
       
    58      *
       
    59      * @param mixed $flag
       
    60      * @return void
       
    61      */
       
    62     public function setOverwriteExistingMethods($flag)
       
    63     {
       
    64         $this->_overwriteExistingMethods = (bool) $flag;
       
    65         return $this;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Add method to definition
       
    70      *
       
    71      * @param  array|Zend_Server_Method_Definition $method
       
    72      * @param  null|string $name
       
    73      * @return Zend_Server_Definition
       
    74      * @throws Zend_Server_Exception if duplicate or invalid method provided
       
    75      */
       
    76     public function addMethod($method, $name = null)
       
    77     {
       
    78         if (is_array($method)) {
       
    79             require_once 'Zend/Server/Method/Definition.php';
       
    80             $method = new Zend_Server_Method_Definition($method);
       
    81         } elseif (!$method instanceof Zend_Server_Method_Definition) {
       
    82             require_once 'Zend/Server/Exception.php';
       
    83             throw new Zend_Server_Exception('Invalid method provided');
       
    84         }
       
    85 
       
    86         if (is_numeric($name)) {
       
    87             $name = null;
       
    88         }
       
    89         if (null !== $name) {
       
    90             $method->setName($name);
       
    91         } else {
       
    92             $name = $method->getName();
       
    93         }
       
    94         if (null === $name) {
       
    95             require_once 'Zend/Server/Exception.php';
       
    96             throw new Zend_Server_Exception('No method name provided');
       
    97         }
       
    98 
       
    99         if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) {
       
   100             require_once 'Zend/Server/Exception.php';
       
   101             throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name));
       
   102         }
       
   103         $this->_methods[$name] = $method;
       
   104         return $this;
       
   105     }
       
   106 
       
   107     /**
       
   108      * Add multiple methods
       
   109      *
       
   110      * @param  array $methods Array of Zend_Server_Method_Definition objects or arrays
       
   111      * @return Zend_Server_Definition
       
   112      */
       
   113     public function addMethods(array $methods)
       
   114     {
       
   115         foreach ($methods as $key => $method) {
       
   116             $this->addMethod($method, $key);
       
   117         }
       
   118         return $this;
       
   119     }
       
   120 
       
   121     /**
       
   122      * Set all methods at once (overwrite)
       
   123      *
       
   124      * @param  array $methods Array of Zend_Server_Method_Definition objects or arrays
       
   125      * @return Zend_Server_Definition
       
   126      */
       
   127     public function setMethods(array $methods)
       
   128     {
       
   129         $this->clearMethods();
       
   130         $this->addMethods($methods);
       
   131         return $this;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Does the definition have the given method?
       
   136      *
       
   137      * @param  string $method
       
   138      * @return bool
       
   139      */
       
   140     public function hasMethod($method)
       
   141     {
       
   142         return array_key_exists($method, $this->_methods);
       
   143     }
       
   144 
       
   145     /**
       
   146      * Get a given method definition
       
   147      *
       
   148      * @param  string $method
       
   149      * @return null|Zend_Server_Method_Definition
       
   150      */
       
   151     public function getMethod($method)
       
   152     {
       
   153         if ($this->hasMethod($method)) {
       
   154             return $this->_methods[$method];
       
   155         }
       
   156         return false;
       
   157     }
       
   158 
       
   159     /**
       
   160      * Get all method definitions
       
   161      *
       
   162      * @return array Array of Zend_Server_Method_Definition objects
       
   163      */
       
   164     public function getMethods()
       
   165     {
       
   166         return $this->_methods;
       
   167     }
       
   168 
       
   169     /**
       
   170      * Remove a method definition
       
   171      *
       
   172      * @param  string $method
       
   173      * @return Zend_Server_Definition
       
   174      */
       
   175     public function removeMethod($method)
       
   176     {
       
   177         if ($this->hasMethod($method)) {
       
   178             unset($this->_methods[$method]);
       
   179         }
       
   180         return $this;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Clear all method definitions
       
   185      *
       
   186      * @return Zend_Server_Definition
       
   187      */
       
   188     public function clearMethods()
       
   189     {
       
   190         $this->_methods = array();
       
   191         return $this;
       
   192     }
       
   193 
       
   194     /**
       
   195      * Cast definition to an array
       
   196      *
       
   197      * @return array
       
   198      */
       
   199     public function toArray()
       
   200     {
       
   201         $methods = array();
       
   202         foreach ($this->getMethods() as $key => $method) {
       
   203             $methods[$key] = $method->toArray();
       
   204         }
       
   205         return $methods;
       
   206     }
       
   207 
       
   208     /**
       
   209      * Countable: count of methods
       
   210      *
       
   211      * @return int
       
   212      */
       
   213     public function count()
       
   214     {
       
   215         return count($this->_methods);
       
   216     }
       
   217 
       
   218     /**
       
   219      * Iterator: current item
       
   220      *
       
   221      * @return mixed
       
   222      */
       
   223     public function current()
       
   224     {
       
   225         return current($this->_methods);
       
   226     }
       
   227 
       
   228     /**
       
   229      * Iterator: current item key
       
   230      *
       
   231      * @return int|string
       
   232      */
       
   233     public function key()
       
   234     {
       
   235         return key($this->_methods);
       
   236     }
       
   237 
       
   238     /**
       
   239      * Iterator: advance to next method
       
   240      *
       
   241      * @return void
       
   242      */
       
   243     public function next()
       
   244     {
       
   245         return next($this->_methods);
       
   246     }
       
   247 
       
   248     /**
       
   249      * Iterator: return to first method
       
   250      *
       
   251      * @return void
       
   252      */
       
   253     public function rewind()
       
   254     {
       
   255         return reset($this->_methods);
       
   256     }
       
   257 
       
   258     /**
       
   259      * Iterator: is the current index valid?
       
   260      *
       
   261      * @return bool
       
   262      */
       
   263     public function valid()
       
   264     {
       
   265         return (bool) $this->current();
       
   266     }
       
   267 }