File indexing completed on 2025-01-19 05:21:27

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 callback 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_Callback
0033 {
0034     /**
0035      * @var string Class name for class method callback
0036      */
0037     protected $_class;
0038 
0039     /**
0040      * @var string Function name for function callback
0041      */
0042     protected $_function;
0043 
0044     /**
0045      * @var string Method name for class method callback
0046      */
0047     protected $_method;
0048 
0049     /**
0050      * @var string Callback type
0051      */
0052     protected $_type;
0053 
0054     /**
0055      * @var array Valid callback types
0056      */
0057     protected $_types = array('function', 'static', 'instance');
0058 
0059     /**
0060      * Constructor
0061      *
0062      * @param  null|array $options
0063      * @return void
0064      */
0065     public function __construct($options = null)
0066     {
0067         if ((null !== $options) && is_array($options))  {
0068             $this->setOptions($options);
0069         }
0070     }
0071 
0072     /**
0073      * Set object state from array of options
0074      *
0075      * @param  array $options
0076      * @return Zend_Server_Method_Callback
0077      */
0078     public function setOptions(array $options)
0079     {
0080         foreach ($options as $key => $value) {
0081             $method = 'set' . ucfirst($key);
0082             if (method_exists($this, $method)) {
0083                 $this->$method($value);
0084             }
0085         }
0086         return $this;
0087     }
0088 
0089     /**
0090      * Set callback class
0091      *
0092      * @param  string $class
0093      * @return Zend_Server_Method_Callback
0094      */
0095     public function setClass($class)
0096     {
0097         if (is_object($class)) {
0098             $class = get_class($class);
0099         }
0100         $this->_class = $class;
0101         return $this;
0102     }
0103 
0104     /**
0105      * Get callback class
0106      *
0107      * @return string|null
0108      */
0109     public function getClass()
0110     {
0111         return $this->_class;
0112     }
0113 
0114     /**
0115      * Set callback function
0116      *
0117      * @param  string $function
0118      * @return Zend_Server_Method_Callback
0119      */
0120     public function setFunction($function)
0121     {
0122         $this->_function = (string) $function;
0123         $this->setType('function');
0124         return $this;
0125     }
0126 
0127     /**
0128      * Get callback function
0129      *
0130      * @return null|string
0131      */
0132     public function getFunction()
0133     {
0134         return $this->_function;
0135     }
0136 
0137     /**
0138      * Set callback class method
0139      *
0140      * @param  string $method
0141      * @return Zend_Server_Method_Callback
0142      */
0143     public function setMethod($method)
0144     {
0145         $this->_method = $method;
0146         return $this;
0147     }
0148 
0149     /**
0150      * Get callback class  method
0151      *
0152      * @return null|string
0153      */
0154     public function getMethod()
0155     {
0156         return $this->_method;
0157     }
0158 
0159     /**
0160      * Set callback type
0161      *
0162      * @param  string $type
0163      * @return Zend_Server_Method_Callback
0164      * @throws Zend_Server_Exception
0165      */
0166     public function setType($type)
0167     {
0168         if (!in_array($type, $this->_types)) {
0169             // require_once 'Zend/Server/Exception.php';
0170             throw new Zend_Server_Exception('Invalid method callback type  passed to ' . __CLASS__ . '::' . __METHOD__);
0171         }
0172         $this->_type = $type;
0173         return $this;
0174     }
0175 
0176     /**
0177      * Get callback type
0178      *
0179      * @return string
0180      */
0181     public function getType()
0182     {
0183         return $this->_type;
0184     }
0185 
0186     /**
0187      * Cast callback to array
0188      *
0189      * @return array
0190      */
0191     public function toArray()
0192     {
0193         $type = $this->getType();
0194         $array = array(
0195             'type' => $type,
0196         );
0197         if ('function' == $type) {
0198             $array['function'] = $this->getFunction();
0199         } else {
0200             $array['class']  = $this->getClass();
0201             $array['method'] = $this->getMethod();
0202         }
0203         return $array;
0204     }
0205 }