PageRenderTime 51ms CodeModel.GetById 23ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 1ms

/library/Zend/Server/Method/Definition.php

https://bitbucket.org/hamidrezas/melobit
PHP | 293 lines | 127 code | 24 blank | 142 comment | 7 complexity | 5a2bf334617866d61436339f28b35f67 MD5 | raw file
Possible License(s): AGPL-1.0
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Definition.php 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23/**
 24 * Method definition metadata
 25 *
 26 * @category   Zend
 27 * @package    Zend_Server
 28 * @subpackage Method
 29 * @copyright  Copyright (c) 2005-2012 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_Definition
 33{
 34    /**
 35     * @var Zend_Server_Method_Callback
 36     */
 37    protected $_callback;
 38
 39    /**
 40     * @var array
 41     */
 42    protected $_invokeArguments = array();
 43
 44    /**
 45     * @var string
 46     */
 47    protected $_methodHelp = '';
 48
 49    /**
 50     * @var string
 51     */
 52    protected $_name;
 53
 54    /**
 55     * @var null|object
 56     */
 57    protected $_object;
 58
 59    /**
 60     * @var array Array of Zend_Server_Method_Prototype objects
 61     */
 62    protected $_prototypes = array();
 63
 64    /**
 65     * Constructor
 66     *
 67     * @param  null|array $options
 68     * @return void
 69     */
 70    public function __construct($options = null)
 71    {
 72        if ((null !== $options) && is_array($options)) {
 73            $this->setOptions($options);
 74        }
 75    }
 76
 77    /**
 78     * Set object state from options
 79     *
 80     * @param  array $options
 81     * @return Zend_Server_Method_Definition
 82     */
 83    public function setOptions(array $options)
 84    {
 85        foreach ($options as $key => $value) {
 86            $method = 'set' . ucfirst($key);
 87            if (method_exists($this, $method)) {
 88                $this->$method($value);
 89            }
 90        }
 91        return $this;
 92    }
 93
 94    /**
 95     * Set method name
 96     *
 97     * @param  string $name
 98     * @return Zend_Server_Method_Definition
 99     */
100    public function setName($name)
101    {
102        $this->_name = (string) $name;
103        return $this;
104    }
105
106    /**
107     * Get method name
108     *
109     * @return string
110     */
111    public function getName()
112    {
113        return $this->_name;
114    }
115
116    /**
117     * Set method callback
118     *
119     * @param  array|Zend_Server_Method_Callback $callback
120     * @return Zend_Server_Method_Definition
121     */
122    public function setCallback($callback)
123    {
124        if (is_array($callback)) {
125            require_once 'Zend/Server/Method/Callback.php';
126            $callback = new Zend_Server_Method_Callback($callback);
127        } elseif (!$callback instanceof Zend_Server_Method_Callback) {
128            require_once 'Zend/Server/Exception.php';
129            throw new Zend_Server_Exception('Invalid method callback provided');
130        }
131        $this->_callback = $callback;
132        return $this;
133    }
134
135    /**
136     * Get method callback
137     *
138     * @return Zend_Server_Method_Callback
139     */
140    public function getCallback()
141    {
142        return $this->_callback;
143    }
144
145    /**
146     * Add prototype to method definition
147     *
148     * @param  array|Zend_Server_Method_Prototype $prototype
149     * @return Zend_Server_Method_Definition
150     */
151    public function addPrototype($prototype)
152    {
153        if (is_array($prototype)) {
154            require_once 'Zend/Server/Method/Prototype.php';
155            $prototype = new Zend_Server_Method_Prototype($prototype);
156        } elseif (!$prototype instanceof Zend_Server_Method_Prototype) {
157            require_once 'Zend/Server/Exception.php';
158            throw new Zend_Server_Exception('Invalid method prototype provided');
159        }
160        $this->_prototypes[] = $prototype;
161        return $this;
162    }
163
164    /**
165     * Add multiple prototypes at once
166     *
167     * @param  array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
168     * @return Zend_Server_Method_Definition
169     */
170    public function addPrototypes(array $prototypes)
171    {
172        foreach ($prototypes as $prototype) {
173            $this->addPrototype($prototype);
174        }
175        return $this;
176    }
177
178    /**
179     * Set all prototypes at once (overwrites)
180     *
181     * @param  array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
182     * @return Zend_Server_Method_Definition
183     */
184    public function setPrototypes(array $prototypes)
185    {
186        $this->_prototypes = array();
187        $this->addPrototypes($prototypes);
188        return $this;
189    }
190
191    /**
192     * Get all prototypes
193     *
194     * @return array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
195     */
196    public function getPrototypes()
197    {
198        return $this->_prototypes;
199    }
200
201    /**
202     * Set method help
203     *
204     * @param  string $methodHelp
205     * @return Zend_Server_Method_Definition
206     */
207    public function setMethodHelp($methodHelp)
208    {
209        $this->_methodHelp = (string) $methodHelp;
210        return $this;
211    }
212
213    /**
214     * Get method help
215     *
216     * @return string
217     */
218    public function getMethodHelp()
219    {
220        return $this->_methodHelp;
221    }
222
223    /**
224     * Set object to use with method calls
225     *
226     * @param  object $object
227     * @return Zend_Server_Method_Definition
228     */
229    public function setObject($object)
230    {
231        if (!is_object($object) && (null !== $object)) {
232            require_once 'Zend/Server/Exception.php';
233            throw new Zend_Server_Exception('Invalid object passed to ' . __CLASS__ . '::' . __METHOD__);
234        }
235        $this->_object = $object;
236        return $this;
237    }
238
239    /**
240     * Get object to use with method calls
241     *
242     * @return null|object
243     */
244    public function getObject()
245    {
246        return $this->_object;
247    }
248
249    /**
250     * Set invoke arguments
251     *
252     * @param  array $invokeArguments
253     * @return Zend_Server_Method_Definition
254     */
255    public function setInvokeArguments(array $invokeArguments)
256    {
257        $this->_invokeArguments = $invokeArguments;
258        return $this;
259    }
260
261    /**
262     * Retrieve invoke arguments
263     *
264     * @return array
265     */
266    public function getInvokeArguments()
267    {
268        return $this->_invokeArguments;
269    }
270
271    /**
272     * Serialize to array
273     *
274     * @return array
275     */
276    public function toArray()
277    {
278        $prototypes = $this->getPrototypes();
279        $signatures = array();
280        foreach ($prototypes as $prototype) {
281            $signatures[] = $prototype->toArray();
282        }
283
284        return array(
285            'name'            => $this->getName(),
286            'callback'        => $this->getCallback()->toArray(),
287            'prototypes'      => $signatures,
288            'methodHelp'      => $this->getMethodHelp(),
289            'invokeArguments' => $this->getInvokeArguments(),
290            'object'          => $this->getObject(),
291        );
292    }
293}