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  * @category   Zend
0030  * @package    Zend_CodeGenerator
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Php_Abstract
0035 {
0036     /**#@+
0037      * Constant values
0038      */
0039     const TYPE_AUTO     = 'auto';
0040     const TYPE_BOOLEAN  = 'boolean';
0041     const TYPE_BOOL     = 'bool';
0042     const TYPE_NUMBER   = 'number';
0043     const TYPE_INTEGER  = 'integer';
0044     const TYPE_INT      = 'int';
0045     const TYPE_FLOAT    = 'float';
0046     const TYPE_DOUBLE   = 'double';
0047     const TYPE_STRING   = 'string';
0048     const TYPE_ARRAY    = 'array';
0049     const TYPE_CONSTANT = 'constant';
0050     const TYPE_NULL     = 'null';
0051     const TYPE_OTHER    = 'other';
0052     /**#@-*/
0053 
0054     /**
0055      * @var array of reflected constants
0056      */
0057     protected static $_constants = array();
0058 
0059     /**
0060      * @var mixed
0061      */
0062     protected $_value = null;
0063 
0064     /**
0065      * @var string
0066      */
0067     protected $_type  = self::TYPE_AUTO;
0068 
0069     /**
0070      * @var int
0071      */
0072     protected $_arrayDepth = 1;
0073 
0074     /**
0075      * _init()
0076      *
0077      * This method will prepare the constant array for this class
0078      */
0079     protected function _init()
0080     {
0081         if(count(self::$_constants) == 0) {
0082             $reflect = new ReflectionClass(get_class($this));
0083             self::$_constants = $reflect->getConstants();
0084             unset($reflect);
0085         }
0086     }
0087 
0088     /**
0089      * isValidConstantType()
0090      *
0091      * @return bool
0092      */
0093     public function isValidConstantType()
0094     {
0095         if ($this->_type == self::TYPE_AUTO) {
0096             $type = $this->_getAutoDeterminedType($this->_value);
0097         } else {
0098             $type = $this->_type;
0099         }
0100 
0101         // valid types for constants
0102         $scalarTypes = array(
0103             self::TYPE_BOOLEAN,
0104             self::TYPE_BOOL,
0105             self::TYPE_NUMBER,
0106             self::TYPE_INTEGER,
0107             self::TYPE_INT,
0108             self::TYPE_FLOAT,
0109             self::TYPE_DOUBLE,
0110             self::TYPE_STRING,
0111             self::TYPE_CONSTANT,
0112             self::TYPE_NULL
0113             );
0114 
0115         return in_array($type, $scalarTypes);
0116     }
0117 
0118     /**
0119      * setValue()
0120      *
0121      * @param mixed $value
0122      * @return Zend_CodeGenerator_Php_Property_DefaultValue
0123      */
0124     public function setValue($value)
0125     {
0126         $this->_value = $value;
0127         return $this;
0128     }
0129 
0130     /**
0131      * getValue()
0132      *
0133      * @return mixed
0134      */
0135     public function getValue()
0136     {
0137         return $this->_value;
0138     }
0139 
0140     /**
0141      * setType()
0142      *
0143      * @param string $type
0144      * @return Zend_CodeGenerator_Php_Property_DefaultValue
0145      */
0146     public function setType($type)
0147     {
0148         $this->_type = $type;
0149         return $this;
0150     }
0151 
0152     /**
0153      * getType()
0154      *
0155      * @return string
0156      */
0157     public function getType()
0158     {
0159         return $this->_type;
0160     }
0161 
0162     /**
0163      * setArrayDepth()
0164      *
0165      * @param int $arrayDepth
0166      * @return Zend_CodeGenerator_Php_Property_DefaultValue
0167      */
0168     public function setArrayDepth($arrayDepth)
0169     {
0170         $this->_arrayDepth = $arrayDepth;
0171         return $this;
0172     }
0173 
0174     /**
0175      * getArrayDepth()
0176      *
0177      * @return int
0178      */
0179     public function getArrayDepth()
0180     {
0181         return $this->_arrayDepth;
0182     }
0183 
0184     /**
0185      * _getValidatedType()
0186      *
0187      * @param string $type
0188      * @return string
0189      */
0190     protected function _getValidatedType($type)
0191     {
0192         if (($constName = array_search($type, self::$_constants)) !== false) {
0193             return $type;
0194         }
0195 
0196         return self::TYPE_AUTO;
0197     }
0198 
0199     /**
0200      * _getAutoDeterminedType()
0201      *
0202      * @param mixed $value
0203      * @return string
0204      */
0205     public function _getAutoDeterminedType($value)
0206     {
0207         switch (gettype($value)) {
0208             case 'boolean':
0209                 return self::TYPE_BOOLEAN;
0210             case 'integer':
0211                 return self::TYPE_INT;
0212             case 'string':
0213                 return self::TYPE_STRING;
0214             case 'double':
0215             case 'float':
0216             case 'integer':
0217                 return self::TYPE_NUMBER;
0218             case 'array':
0219                 return self::TYPE_ARRAY;
0220             case 'NULL':
0221                 return self::TYPE_NULL;
0222             case 'object':
0223             case 'resource':
0224             case 'unknown type':
0225             default:
0226                 return self::TYPE_OTHER;
0227         }
0228     }
0229 
0230     /**
0231      * generate()
0232      *
0233      * @return string
0234      */
0235     public function generate()
0236     {
0237         $type = $this->_type;
0238 
0239         if ($type != self::TYPE_AUTO) {
0240             $type = $this->_getValidatedType($type);
0241         }
0242 
0243         $value = $this->_value;
0244 
0245         if ($type == self::TYPE_AUTO) {
0246             $type = $this->_getAutoDeterminedType($value);
0247 
0248             if ($type == self::TYPE_ARRAY) {
0249                 $rii = new RecursiveIteratorIterator(
0250                     $it = new RecursiveArrayIterator($value),
0251                     RecursiveIteratorIterator::SELF_FIRST
0252                     );
0253                 foreach ($rii as $curKey => $curValue) {
0254                     if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) {
0255                         $curValue = new self(array('value' => $curValue));
0256                         $rii->getSubIterator()->offsetSet($curKey, $curValue);
0257                     }
0258                     $curValue->setArrayDepth($rii->getDepth());
0259                 }
0260                 $value = $rii->getSubIterator()->getArrayCopy();
0261             }
0262 
0263         }
0264 
0265         $output = '';
0266 
0267         switch ($type) {
0268             case self::TYPE_BOOLEAN:
0269             case self::TYPE_BOOL:
0270                 $output .= ( $value ? 'true' : 'false' );
0271                 break;
0272             case self::TYPE_STRING:
0273                 $output .= "'" . addcslashes($value, "'") . "'";
0274                 break;
0275             case self::TYPE_NULL:
0276                 $output .= 'null';
0277                 break;
0278             case self::TYPE_NUMBER:
0279             case self::TYPE_INTEGER:
0280             case self::TYPE_INT:
0281             case self::TYPE_FLOAT:
0282             case self::TYPE_DOUBLE:
0283             case self::TYPE_CONSTANT:
0284                 $output .= $value;
0285                 break;
0286             case self::TYPE_ARRAY:
0287                 $output .= 'array(';
0288                 $curArrayMultiblock = false;
0289                 if (count($value) > 1) {
0290                     $curArrayMultiblock = true;
0291                     $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
0292                 }
0293                 $outputParts = array();
0294                 $noKeyIndex = 0;
0295                 foreach ($value as $n => $v) {
0296                     $v->setArrayDepth($this->_arrayDepth + 1);
0297                     $partV = $v->generate();
0298                     $partV = substr($partV, 0, strlen($partV)-1);
0299                     if ($n === $noKeyIndex) {
0300                         $outputParts[] = $partV;
0301                         $noKeyIndex++;
0302                     } else {
0303                         $outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV;
0304                     }
0305 
0306                 }
0307                 $output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts);
0308                 if ($curArrayMultiblock == true) {
0309                     $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
0310                 }
0311                 $output .= ')';
0312                 break;
0313             case self::TYPE_OTHER:
0314             default:
0315                 // require_once "Zend/CodeGenerator/Php/Exception.php";
0316                 throw new Zend_CodeGenerator_Php_Exception(
0317                     "Type '".get_class($value)."' is unknown or cannot be used as property default value."
0318                 );
0319         }
0320 
0321         $output .= ';';
0322 
0323         return $output;
0324     }
0325 }