web/lib/Zend/CodeGenerator/Php/Parameter.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_CodeGenerator
       
    17  * @subpackage PHP
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Parameter.php 21889 2010-04-16 18:40:50Z juokaz $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_CodeGenerator_Php_Abstract
       
    25  */
       
    26 require_once 'Zend/CodeGenerator/Php/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_CodeGenerator_Php_ParameterDefaultValue
       
    30  */
       
    31 require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_CodeGenerator
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
       
    40 {
       
    41     /**
       
    42      * @var string
       
    43      */
       
    44     protected $_type = null;
       
    45 
       
    46     /**
       
    47      * @var string
       
    48      */
       
    49     protected $_name = null;
       
    50 
       
    51     /**
       
    52      * @var string
       
    53      */
       
    54     protected $_defaultValue = null;
       
    55 
       
    56     /**
       
    57      * @var int
       
    58      */
       
    59     protected $_position = null;
       
    60 
       
    61     /**
       
    62      * @var bool
       
    63      */
       
    64     protected $_passedByReference = false;
       
    65 
       
    66     /**
       
    67      * fromReflection()
       
    68      *
       
    69      * @param Zend_Reflection_Parameter $reflectionParameter
       
    70      * @return Zend_CodeGenerator_Php_Parameter
       
    71      */
       
    72     public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
       
    73     {
       
    74         $param = new Zend_CodeGenerator_Php_Parameter();
       
    75         $param->setName($reflectionParameter->getName());
       
    76 
       
    77         if($reflectionParameter->isArray()) {
       
    78             $param->setType('array');
       
    79         } else {
       
    80             $typeClass = $reflectionParameter->getClass();
       
    81             if($typeClass !== null) {
       
    82                 $param->setType($typeClass->getName());
       
    83             }
       
    84         }
       
    85 
       
    86         $param->setPosition($reflectionParameter->getPosition());
       
    87 
       
    88         if($reflectionParameter->isOptional()) {
       
    89             $param->setDefaultValue($reflectionParameter->getDefaultValue());
       
    90         }
       
    91         $param->setPassedByReference($reflectionParameter->isPassedByReference());
       
    92 
       
    93         return $param;
       
    94     }
       
    95 
       
    96     /**
       
    97      * setType()
       
    98      *
       
    99      * @param string $type
       
   100      * @return Zend_CodeGenerator_Php_Parameter
       
   101      */
       
   102     public function setType($type)
       
   103     {
       
   104         $this->_type = $type;
       
   105         return $this;
       
   106     }
       
   107 
       
   108     /**
       
   109      * getType()
       
   110      *
       
   111      * @return string
       
   112      */
       
   113     public function getType()
       
   114     {
       
   115         return $this->_type;
       
   116     }
       
   117 
       
   118     /**
       
   119      * setName()
       
   120      *
       
   121      * @param string $name
       
   122      * @return Zend_CodeGenerator_Php_Parameter
       
   123      */
       
   124     public function setName($name)
       
   125     {
       
   126         $this->_name = $name;
       
   127         return $this;
       
   128     }
       
   129 
       
   130     /**
       
   131      * getName()
       
   132      *
       
   133      * @return string
       
   134      */
       
   135     public function getName()
       
   136     {
       
   137         return $this->_name;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Set the default value of the parameter.
       
   142      *
       
   143      * Certain variables are difficult to expres
       
   144      *
       
   145      * @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
       
   146      * @return Zend_CodeGenerator_Php_Parameter
       
   147      */
       
   148     public function setDefaultValue($defaultValue)
       
   149     {
       
   150         if($defaultValue === null) {
       
   151             $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
       
   152         } else if(is_array($defaultValue)) {
       
   153             $defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
       
   154             $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
       
   155         } else if(is_bool($defaultValue)) {
       
   156             if($defaultValue == true) {
       
   157                 $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
       
   158             } else {
       
   159                 $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
       
   160             }
       
   161         } else {
       
   162             $this->_defaultValue = $defaultValue;
       
   163         }
       
   164         return $this;
       
   165     }
       
   166 
       
   167     /**
       
   168      * getDefaultValue()
       
   169      *
       
   170      * @return string
       
   171      */
       
   172     public function getDefaultValue()
       
   173     {
       
   174         return $this->_defaultValue;
       
   175     }
       
   176 
       
   177     /**
       
   178      * setPosition()
       
   179      *
       
   180      * @param int $position
       
   181      * @return Zend_CodeGenerator_Php_Parameter
       
   182      */
       
   183     public function setPosition($position)
       
   184     {
       
   185         $this->_position = $position;
       
   186         return $this;
       
   187     }
       
   188 
       
   189     /**
       
   190      * getPosition()
       
   191      *
       
   192      * @return int
       
   193      */
       
   194     public function getPosition()
       
   195     {
       
   196         return $this->_position;
       
   197     }
       
   198 
       
   199     /**
       
   200      * @return bool
       
   201      */
       
   202     public function getPassedByReference()
       
   203     {
       
   204         return $this->_passedByReference;
       
   205     }
       
   206 
       
   207     /**
       
   208      * @param bool $passedByReference
       
   209      * @return Zend_CodeGenerator_Php_Parameter
       
   210      */
       
   211     public function setPassedByReference($passedByReference)
       
   212     {
       
   213         $this->_passedByReference = $passedByReference;
       
   214         return $this;
       
   215     }
       
   216 
       
   217     /**
       
   218      * generate()
       
   219      *
       
   220      * @return string
       
   221      */
       
   222     public function generate()
       
   223     {
       
   224         $output = '';
       
   225 
       
   226         if ($this->_type) {
       
   227             $output .= $this->_type . ' ';
       
   228         }
       
   229 
       
   230         if($this->_passedByReference === true) {
       
   231             $output .= '&';
       
   232         }
       
   233 
       
   234         $output .= '$' . $this->_name;
       
   235 
       
   236         if ($this->_defaultValue !== null) {
       
   237             $output .= ' = ';
       
   238             if (is_string($this->_defaultValue)) {
       
   239                 $output .= '\'' . $this->_defaultValue . '\'';
       
   240             } else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_Parameter_DefaultValue) {
       
   241                 $output .= (string)$this->_defaultValue;
       
   242             } else {
       
   243                 $output .= $this->_defaultValue;
       
   244             }
       
   245         }
       
   246 
       
   247         return $output;
       
   248     }
       
   249 
       
   250 }