PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Server/Method/Prototype.php

http://zoop.googlecode.com/
PHP | 208 lines | 94 code | 14 blank | 100 comment | 10 complexity | 8d9e2ff61ac902bd2123d16b23541454 MD5 | raw file
  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 * @subpackage Method
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Prototype.php 20096 2010-01-06 02:05:09Z bkarwin $
 21 */
 22
 23/**
 24 * Method prototype metadata
 25 *
 26 * @category   Zend
 27 * @package    Zend_Server
 28 * @subpackage Method
 29 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 30 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 31 */
 32class Zend_Server_Method_Prototype
 33{
 34    /**
 35     * @var string Return type
 36     */
 37    protected $_returnType = 'void';
 38
 39    /**
 40     * @var array Map parameter names to parameter index
 41     */
 42    protected $_parameterNameMap = array();
 43
 44    /**
 45     * @var array Method parameters
 46     */
 47    protected $_parameters = array();
 48
 49    /**
 50     * Constructor
 51     *
 52     * @param  null|array $options
 53     * @return void
 54     */
 55    public function __construct($options = null)
 56    {
 57        if ((null !== $options) && is_array($options)) {
 58            $this->setOptions($options);
 59        }
 60    }
 61
 62    /**
 63     * Set return value
 64     *
 65     * @param  string $returnType
 66     * @return Zend_Server_Method_Prototype
 67     */
 68    public function setReturnType($returnType)
 69    {
 70        $this->_returnType = $returnType;
 71        return $this;
 72    }
 73
 74    /**
 75     * Retrieve return type
 76     *
 77     * @return string
 78     */
 79    public function getReturnType()
 80    {
 81        return $this->_returnType;
 82    }
 83
 84    /**
 85     * Add a parameter
 86     *
 87     * @param  string $parameter
 88     * @return Zend_Server_Method_Prototype
 89     */
 90    public function addParameter($parameter)
 91    {
 92        if ($parameter instanceof Zend_Server_Method_Parameter) {
 93            $this->_parameters[] = $parameter;
 94            if (null !== ($name = $parameter->getName())) {
 95                $this->_parameterNameMap[$name] = count($this->_parameters) - 1;
 96            }
 97        } else {
 98            require_once 'Zend/Server/Method/Parameter.php';
 99            $parameter = new Zend_Server_Method_Parameter(array(
100                'type' => (string) $parameter,
101            ));
102            $this->_parameters[] = $parameter;
103        }
104        return $this;
105    }
106
107    /**
108     * Add parameters
109     *
110     * @param  array $parameter
111     * @return Zend_Server_Method_Prototype
112     */
113    public function addParameters(array $parameters)
114    {
115        foreach ($parameters as $parameter) {
116            $this->addParameter($parameter);
117        }
118        return $this;
119    }
120
121    /**
122     * Set parameters
123     *
124     * @param  array $parameters
125     * @return Zend_Server_Method_Prototype
126     */
127    public function setParameters(array $parameters)
128    {
129        $this->_parameters       = array();
130        $this->_parameterNameMap = array();
131        $this->addParameters($parameters);
132        return $this;
133    }
134
135    /**
136     * Retrieve parameters as list of types
137     *
138     * @return array
139     */
140    public function getParameters()
141    {
142        $types = array();
143        foreach ($this->_parameters as $parameter) {
144            $types[] = $parameter->getType();
145        }
146        return $types;
147    }
148
149    /**
150     * Get parameter objects
151     *
152     * @return array
153     */
154    public function getParameterObjects()
155    {
156        return $this->_parameters;
157    }
158
159    /**
160     * Retrieve a single parameter by name or index
161     *
162     * @param  string|int $index
163     * @return null|Zend_Server_Method_Parameter
164     */
165    public function getParameter($index)
166    {
167        if (!is_string($index) && !is_numeric($index)) {
168            return null;
169        }
170        if (array_key_exists($index, $this->_parameterNameMap)) {
171            $index = $this->_parameterNameMap[$index];
172        }
173        if (array_key_exists($index, $this->_parameters)) {
174            return $this->_parameters[$index];
175        }
176        return null;
177    }
178
179    /**
180     * Set object state from array
181     *
182     * @param  array $options
183     * @return Zend_Server_Method_Prototype
184     */
185    public function setOptions(array $options)
186    {
187        foreach ($options as $key => $value) {
188            $method = 'set' . ucfirst($key);
189            if (method_exists($this, $method)) {
190                $this->$method($value);
191            }
192        }
193        return $this;
194    }
195
196    /**
197     * Serialize to array
198     *
199     * @return array
200     */
201    public function toArray()
202    {
203        return array(
204            'returnType' => $this->getReturnType(),
205            'parameters' => $this->getParameters(),
206        );
207    }
208}