File indexing completed on 2025-01-19 05:20:59

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_CodeGenerator
0017  * @subpackage PHP
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  * @see Zend_CodeGenerator_Php_Abstract
0025  */
0026 // require_once 'Zend/CodeGenerator/Php/Abstract.php';
0027 
0028 /**
0029  * @see Zend_CodeGenerator_Php_ParameterDefaultValue
0030  */
0031 // require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_CodeGenerator
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
0040 {
0041     /**
0042      * @var string
0043      */
0044     protected $_type = null;
0045 
0046     /**
0047      * @var string
0048      */
0049     protected $_name = null;
0050 
0051     /**
0052      * @var string
0053      */
0054     protected $_defaultValue = null;
0055 
0056     /**
0057      * @var int
0058      */
0059     protected $_position = null;
0060 
0061     /**
0062      * @var bool
0063      */
0064     protected $_passedByReference = false;
0065 
0066     /**
0067      * fromReflection()
0068      *
0069      * @param Zend_Reflection_Parameter $reflectionParameter
0070      * @return Zend_CodeGenerator_Php_Parameter
0071      */
0072     public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
0073     {
0074         $param = new Zend_CodeGenerator_Php_Parameter();
0075         $param->setName($reflectionParameter->getName());
0076 
0077         if($reflectionParameter->isArray()) {
0078             $param->setType('array');
0079         } else {
0080             $typeClass = $reflectionParameter->getClass();
0081             if($typeClass !== null) {
0082                 $param->setType($typeClass->getName());
0083             }
0084         }
0085 
0086         $param->setPosition($reflectionParameter->getPosition());
0087 
0088         if($reflectionParameter->isOptional()) {
0089             $param->setDefaultValue($reflectionParameter->getDefaultValue());
0090         }
0091         $param->setPassedByReference($reflectionParameter->isPassedByReference());
0092 
0093         return $param;
0094     }
0095 
0096     /**
0097      * setType()
0098      *
0099      * @param string $type
0100      * @return Zend_CodeGenerator_Php_Parameter
0101      */
0102     public function setType($type)
0103     {
0104         $this->_type = $type;
0105         return $this;
0106     }
0107 
0108     /**
0109      * getType()
0110      *
0111      * @return string
0112      */
0113     public function getType()
0114     {
0115         return $this->_type;
0116     }
0117 
0118     /**
0119      * setName()
0120      *
0121      * @param string $name
0122      * @return Zend_CodeGenerator_Php_Parameter
0123      */
0124     public function setName($name)
0125     {
0126         $this->_name = $name;
0127         return $this;
0128     }
0129 
0130     /**
0131      * getName()
0132      *
0133      * @return string
0134      */
0135     public function getName()
0136     {
0137         return $this->_name;
0138     }
0139 
0140     /**
0141      * Set the default value of the parameter.
0142      *
0143      * Certain variables are difficult to expres
0144      *
0145      * @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
0146      * @return Zend_CodeGenerator_Php_Parameter
0147      */
0148     public function setDefaultValue($defaultValue)
0149     {
0150         if($defaultValue === null) {
0151             $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
0152         } else if(is_array($defaultValue)) {
0153             $defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
0154             $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
0155         } else if(is_bool($defaultValue)) {
0156             if($defaultValue == true) {
0157                 $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
0158             } else {
0159                 $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
0160             }
0161         } else {
0162             $this->_defaultValue = $defaultValue;
0163         }
0164         return $this;
0165     }
0166 
0167     /**
0168      * getDefaultValue()
0169      *
0170      * @return string
0171      */
0172     public function getDefaultValue()
0173     {
0174         return $this->_defaultValue;
0175     }
0176 
0177     /**
0178      * setPosition()
0179      *
0180      * @param int $position
0181      * @return Zend_CodeGenerator_Php_Parameter
0182      */
0183     public function setPosition($position)
0184     {
0185         $this->_position = $position;
0186         return $this;
0187     }
0188 
0189     /**
0190      * getPosition()
0191      *
0192      * @return int
0193      */
0194     public function getPosition()
0195     {
0196         return $this->_position;
0197     }
0198 
0199     /**
0200      * @return bool
0201      */
0202     public function getPassedByReference()
0203     {
0204         return $this->_passedByReference;
0205     }
0206 
0207     /**
0208      * @param bool $passedByReference
0209      * @return Zend_CodeGenerator_Php_Parameter
0210      */
0211     public function setPassedByReference($passedByReference)
0212     {
0213         $this->_passedByReference = $passedByReference;
0214         return $this;
0215     }
0216 
0217     /**
0218      * generate()
0219      *
0220      * @return string
0221      */
0222     public function generate()
0223     {
0224         $output = '';
0225 
0226         if ($this->_type) {
0227             $output .= $this->_type . ' ';
0228         }
0229 
0230         if($this->_passedByReference === true) {
0231             $output .= '&';
0232         }
0233 
0234         $output .= '$' . $this->_name;
0235 
0236         if ($this->_defaultValue !== null) {
0237             $output .= ' = ';
0238             if (is_string($this->_defaultValue)) {
0239                 $output .= '\'' . $this->_defaultValue . '\'';
0240             } else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_Parameter_DefaultValue) {
0241                 $output .= (string)$this->_defaultValue;
0242             } else {
0243                 $output .= $this->_defaultValue;
0244             }
0245         }
0246 
0247         return $output;
0248     }
0249 
0250 }