File indexing completed on 2024-06-16 05:30:23

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_Server
0017  * @subpackage Method
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  * Method prototype metadata
0025  *
0026  * @category   Zend
0027  * @package    Zend_Server
0028  * @subpackage Method
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Server_Method_Prototype
0033 {
0034     /**
0035      * @var string Return type
0036      */
0037     protected $_returnType = 'void';
0038 
0039     /**
0040      * @var array Map parameter names to parameter index
0041      */
0042     protected $_parameterNameMap = array();
0043 
0044     /**
0045      * @var array Method parameters
0046      */
0047     protected $_parameters = array();
0048 
0049     /**
0050      * Constructor
0051      *
0052      * @param  null|array $options
0053      * @return void
0054      */
0055     public function __construct($options = null)
0056     {
0057         if ((null !== $options) && is_array($options)) {
0058             $this->setOptions($options);
0059         }
0060     }
0061 
0062     /**
0063      * Set return value
0064      *
0065      * @param  string $returnType
0066      * @return Zend_Server_Method_Prototype
0067      */
0068     public function setReturnType($returnType)
0069     {
0070         $this->_returnType = $returnType;
0071         return $this;
0072     }
0073 
0074     /**
0075      * Retrieve return type
0076      *
0077      * @return string
0078      */
0079     public function getReturnType()
0080     {
0081         return $this->_returnType;
0082     }
0083 
0084     /**
0085      * Add a parameter
0086      *
0087      * @param  string $parameter
0088      * @return Zend_Server_Method_Prototype
0089      */
0090     public function addParameter($parameter)
0091     {
0092         if ($parameter instanceof Zend_Server_Method_Parameter) {
0093             $this->_parameters[] = $parameter;
0094             if (null !== ($name = $parameter->getName())) {
0095                 $this->_parameterNameMap[$name] = count($this->_parameters) - 1;
0096             }
0097         } else {
0098             // require_once 'Zend/Server/Method/Parameter.php';
0099             $parameter = new Zend_Server_Method_Parameter(array(
0100                 'type' => (string) $parameter,
0101             ));
0102             $this->_parameters[] = $parameter;
0103         }
0104         return $this;
0105     }
0106 
0107     /**
0108      * Add parameters
0109      *
0110      * @param  array $parameter
0111      * @return Zend_Server_Method_Prototype
0112      */
0113     public function addParameters(array $parameters)
0114     {
0115         foreach ($parameters as $parameter) {
0116             $this->addParameter($parameter);
0117         }
0118         return $this;
0119     }
0120 
0121     /**
0122      * Set parameters
0123      *
0124      * @param  array $parameters
0125      * @return Zend_Server_Method_Prototype
0126      */
0127     public function setParameters(array $parameters)
0128     {
0129         $this->_parameters       = array();
0130         $this->_parameterNameMap = array();
0131         $this->addParameters($parameters);
0132         return $this;
0133     }
0134 
0135     /**
0136      * Retrieve parameters as list of types
0137      *
0138      * @return array
0139      */
0140     public function getParameters()
0141     {
0142         $types = array();
0143         foreach ($this->_parameters as $parameter) {
0144             $types[] = $parameter->getType();
0145         }
0146         return $types;
0147     }
0148 
0149     /**
0150      * Get parameter objects
0151      *
0152      * @return array
0153      */
0154     public function getParameterObjects()
0155     {
0156         return $this->_parameters;
0157     }
0158 
0159     /**
0160      * Retrieve a single parameter by name or index
0161      *
0162      * @param  string|int $index
0163      * @return null|Zend_Server_Method_Parameter
0164      */
0165     public function getParameter($index)
0166     {
0167         if (!is_string($index) && !is_numeric($index)) {
0168             return null;
0169         }
0170         if (array_key_exists($index, $this->_parameterNameMap)) {
0171             $index = $this->_parameterNameMap[$index];
0172         }
0173         if (array_key_exists($index, $this->_parameters)) {
0174             return $this->_parameters[$index];
0175         }
0176         return null;
0177     }
0178 
0179     /**
0180      * Set object state from array
0181      *
0182      * @param  array $options
0183      * @return Zend_Server_Method_Prototype
0184      */
0185     public function setOptions(array $options)
0186     {
0187         foreach ($options as $key => $value) {
0188             $method = 'set' . ucfirst($key);
0189             if (method_exists($this, $method)) {
0190                 $this->$method($value);
0191             }
0192         }
0193         return $this;
0194     }
0195 
0196     /**
0197      * Serialize to array
0198      *
0199      * @return array
0200      */
0201     public function toArray()
0202     {
0203         return array(
0204             'returnType' => $this->getReturnType(),
0205             'parameters' => $this->getParameters(),
0206         );
0207     }
0208 }