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-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
20 * @version    $Id$
21 */
22
23/**
24 * Method definition metadata
25 *
26 * @category   Zend
27 * @package    Zend_Server
28 * @subpackage Method
29 * @copyright  Copyright (c) 2005-2015 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            $callback = new Zend_Server_Method_Callback($callback);
126        } elseif (!$callback instanceof Zend_Server_Method_Callback) {
127            throw new Zend_Server_Exception('Invalid method callback provided');
128        }
129        $this->_callback = $callback;
130        return $this;
131    }
132
133    /**
134     * Get method callback
135     *
136     * @return Zend_Server_Method_Callback
137     */
138    public function getCallback()
139    {
140        return $this->_callback;
141    }
142
143    /**
144     * Add prototype to method definition
145     *
146     * @param  array|Zend_Server_Method_Prototype $prototype
147     * @return Zend_Server_Method_Definition
148     */
149    public function addPrototype($prototype)
150    {
151        if (is_array($prototype)) {
152            $prototype = new Zend_Server_Method_Prototype($prototype);
153        } elseif (!$prototype instanceof Zend_Server_Method_Prototype) {
154            throw new Zend_Server_Exception('Invalid method prototype provided');
155        }
156        $this->_prototypes[] = $prototype;
157        return $this;
158    }
159
160    /**
161     * Add multiple prototypes at once
162     *
163     * @param  array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
164     * @return Zend_Server_Method_Definition
165     */
166    public function addPrototypes(array $prototypes)
167    {
168        foreach ($prototypes as $prototype) {
169            $this->addPrototype($prototype);
170        }
171        return $this;
172    }
173
174    /**
175     * Set all prototypes at once (overwrites)
176     *
177     * @param  array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
178     * @return Zend_Server_Method_Definition
179     */
180    public function setPrototypes(array $prototypes)
181    {
182        $this->_prototypes = array();
183        $this->addPrototypes($prototypes);
184        return $this;
185    }
186
187    /**
188     * Get all prototypes
189     *
190     * @return array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
191     */
192    public function getPrototypes()
193    {
194        return $this->_prototypes;
195    }
196
197    /**
198     * Set method help
199     *
200     * @param  string $methodHelp
201     * @return Zend_Server_Method_Definition
202     */
203    public function setMethodHelp($methodHelp)
204    {
205        $this->_methodHelp = (string) $methodHelp;
206        return $this;
207    }
208
209    /**
210     * Get method help
211     *
212     * @return string
213     */
214    public function getMethodHelp()
215    {
216        return $this->_methodHelp;
217    }
218
219    /**
220     * Set object to use with method calls
221     *
222     * @param  object $object
223     * @return Zend_Server_Method_Definition
224     */
225    public function setObject($object)
226    {
227        if (!is_object($object) && (null !== $object)) {
228            throw new Zend_Server_Exception('Invalid object passed to ' . __CLASS__ . '::' . __METHOD__);
229        }
230        $this->_object = $object;
231        return $this;
232    }
233
234    /**
235     * Get object to use with method calls
236     *
237     * @return null|object
238     */
239    public function getObject()
240    {
241        return $this->_object;
242    }
243
244    /**
245     * Set invoke arguments
246     *
247     * @param  array $invokeArguments
248     * @return Zend_Server_Method_Definition
249     */
250    public function setInvokeArguments(array $invokeArguments)
251    {
252        $this->_invokeArguments = $invokeArguments;
253        return $this;
254    }
255
256    /**
257     * Retrieve invoke arguments
258     *
259     * @return array
260     */
261    public function getInvokeArguments()
262    {
263        return $this->_invokeArguments;
264    }
265
266    /**
267     * Serialize to array
268     *
269     * @return array
270     */
271    public function toArray()
272    {
273        $prototypes = $this->getPrototypes();
274        $signatures = array();
275        foreach ($prototypes as $prototype) {
276            $signatures[] = $prototype->toArray();
277        }
278
279        return array(
280            'name'            => $this->getName(),
281            'callback'        => $this->getCallback()->toArray(),
282            'prototypes'      => $signatures,
283            'methodHelp'      => $this->getMethodHelp(),
284            'invokeArguments' => $this->getInvokeArguments(),
285            'object'          => $this->getObject(),
286        );
287    }
288}
289