3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Server\Method
;
13 * Method prototype metadata
18 * @var string Return type
20 protected $returnType = 'void';
23 * @var array Map parameter names to parameter index
25 protected $parameterNameMap = array();
28 * @var array Method parameters
30 protected $parameters = array();
35 * @param null|array $options
37 public function __construct($options = null)
39 if ((null !== $options) && is_array($options)) {
40 $this->setOptions($options);
47 * @param string $returnType
48 * @return \Zend\Server\Method\Prototype
50 public function setReturnType($returnType)
52 $this->returnType
= $returnType;
57 * Retrieve return type
61 public function getReturnType()
63 return $this->returnType
;
69 * @param string $parameter
70 * @return \Zend\Server\Method\Prototype
72 public function addParameter($parameter)
74 if ($parameter instanceof Parameter
) {
75 $this->parameters
[] = $parameter;
76 if (null !== ($name = $parameter->getName())) {
77 $this->parameterNameMap
[$name] = count($this->parameters
) - 1;
80 $parameter = new Parameter(array(
81 'type' => (string) $parameter,
83 $this->parameters
[] = $parameter;
91 * @param array $parameters
92 * @return \Zend\Server\Method\Prototype
94 public function addParameters(array $parameters)
96 foreach ($parameters as $parameter) {
97 $this->addParameter($parameter);
105 * @param array $parameters
106 * @return \Zend\Server\Method\Prototype
108 public function setParameters(array $parameters)
110 $this->parameters
= array();
111 $this->parameterNameMap
= array();
112 $this->addParameters($parameters);
117 * Retrieve parameters as list of types
121 public function getParameters()
124 foreach ($this->parameters
as $parameter) {
125 $types[] = $parameter->getType();
131 * Get parameter objects
135 public function getParameterObjects()
137 return $this->parameters
;
141 * Retrieve a single parameter by name or index
143 * @param string|int $index
144 * @return null|\Zend\Server\Method\Parameter
146 public function getParameter($index)
148 if (!is_string($index) && !is_numeric($index)) {
151 if (array_key_exists($index, $this->parameterNameMap
)) {
152 $index = $this->parameterNameMap
[$index];
154 if (array_key_exists($index, $this->parameters
)) {
155 return $this->parameters
[$index];
161 * Set object state from array
163 * @param array $options
164 * @return \Zend\Server\Method\Prototype
166 public function setOptions(array $options)
168 foreach ($options as $key => $value) {
169 $method = 'set' . ucfirst($key);
170 if (method_exists($this, $method)) {
171 $this->$method($value);
182 public function toArray()
185 'returnType' => $this->getReturnType(),
186 'parameters' => $this->getParameters(),