eZ Publish  [4.0]
eztemplatetypeoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateTypeOperator class
00004 //
00005 // Created on: <18-Apr-2002 12:15:07 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZTemplateTypeOperator eztemplatetypeoperator.php
00033   \ingroup eZTemplateOperators
00034   \brief Operators for checking variable type
00035 
00036 Usage:
00037 $var|is_array or is_array( $var )
00038 $var|is_boolean or is_boolean( $var )
00039 $var|is_integer or is_integer( $var )
00040 $var|is_float or is_float( $var )
00041 $var|is_numeric or is_numeric( $var )
00042 $var|is_string or is_string( $var )
00043 $var|is_object or is_object( $var )
00044 $var|is_class('my_class') or is_class( 'my_class', $var )
00045 $var|is_null or is_null( $var )
00046 is_set( $var )
00047 is_unset( $var )
00048 $var|get_type or get_type( $var )
00049 $var|get_class or get_class( $var )
00050 
00051 
00052 */
00053 
00054 class eZTemplateTypeOperator
00055 {
00056     /*!
00057      Initializes the operator class with the various operator names.
00058     */
00059     function eZTemplateTypeOperator(  /*! The name array */
00060                                       $isArrayName = "is_array",
00061                                       $isBooleanName = "is_boolean",
00062                                       $isIntegerName = "is_integer",
00063                                       $isFloatName = "is_float",
00064                                       $isNumericName = "is_numeric",
00065                                       $isStringName = "is_string",
00066                                       $isObjectName = "is_object",
00067                                       $isClassName = "is_class",
00068                                       $isNullName = "is_null",
00069                                       $isSetName = "is_set",
00070                                       $isUnsetName = "is_unset",
00071                                       $getTypeName = "get_type",
00072                                       $getClassName = "get_class" )
00073     {
00074         $this->Operators = array( $isArrayName,
00075                                   $isBooleanName,
00076                                   $isIntegerName,
00077                                   $isFloatName,
00078                                   $isNumericName,
00079                                   $isStringName,
00080                                   $isObjectName,
00081                                   $isClassName,
00082                                   $isNullName,
00083                                   $isSetName,
00084                                   $isUnsetName,
00085                                   $getTypeName,
00086                                   $getClassName );
00087         $this->IsArrayName = $isArrayName;
00088         $this->IsBooleanName = $isBooleanName;
00089         $this->IsIntegerName = $isIntegerName;
00090         $this->IsFloatName = $isFloatName;
00091         $this->IsNumericName = $isNumericName;
00092         $this->IsStringName = $isStringName;
00093         $this->IsObjectName = $isObjectName;
00094         $this->IsClassName = $isClassName;
00095         $this->IsNullName = $isNullName;
00096         $this->IsSetName = $isSetName;
00097         $this->IsUnsetName = $isUnsetName;
00098         $this->GetTypeName = $getTypeName;
00099         $this->GetClassName = $getClassName;
00100         $this->PHPNameMap = array( $isArrayName => 'is_array',
00101                                    $isBooleanName => 'is_bool',
00102                                    $isIntegerName => 'is_integer',
00103                                    $isFloatName => 'is_float',
00104                                    $isNumericName => 'is_numeric',
00105                                    $isStringName => 'is_string',
00106                                    $isObjectName => 'is_object',
00107                                    $isNullName => 'is_null' );
00108     }
00109 
00110     /*!
00111      Returns the operators in this class.
00112     */
00113     function operatorList()
00114     {
00115         return $this->Operators;
00116     }
00117 
00118     function operatorTemplateHints()
00119     {
00120         return array( $this->IsArrayName => array( 'input' => true,
00121                                                    'output' => true,
00122                                                    'parameters' => true,
00123                                                    'element-transformation' => true,
00124                                                    'transform-parameters' => true,
00125                                                    'input-as-parameter' => true,
00126                                                    'element-transformation-func' => 'isTransform'),
00127                       $this->IsBooleanName => array( 'input' => true,
00128                                                      'output' => true,
00129                                                      'parameters' => true,
00130                                                      'element-transformation' => true,
00131                                                      'transform-parameters' => true,
00132                                                      'input-as-parameter' => true,
00133                                                      'element-transformation-func' => 'isTransform' ),
00134                       $this->IsIntegerName => array( 'input' => true,
00135                                                      'output' => true,
00136                                                      'parameters' => true,
00137                                                      'element-transformation' => true,
00138                                                      'transform-parameters' => true,
00139                                                      'input-as-parameter' => true,
00140                                                      'element-transformation-func' => 'isTransform' ),
00141                       $this->IsFloatName => array( 'input' => true,
00142                                                    'output' => true,
00143                                                    'parameters' => true,
00144                                                    'element-transformation' => true,
00145                                                    'transform-parameters' => true,
00146                                                    'input-as-parameter' => true,
00147                                                    'element-transformation-func' => 'isTransform' ),
00148                       $this->IsNumericName => array( 'input' => true,
00149                                                      'output' => true,
00150                                                      'parameters' => true,
00151                                                      'element-transformation' => true,
00152                                                      'transform-parameters' => true,
00153                                                      'input-as-parameter' => true,
00154                                                      'element-transformation-func' => 'isTransform' ),
00155                       $this->IsStringName => array( 'input' => true,
00156                                                     'output' => true,
00157                                                     'parameters' => true,
00158                                                     'element-transformation' => true,
00159                                                     'transform-parameters' => true,
00160                                                     'input-as-parameter' => true,
00161                                                     'element-transformation-func' => 'isTransform' ),
00162                       $this->IsObjectName => array( 'input' => true,
00163                                                     'output' => true,
00164                                                     'parameters' => true,
00165                                                     'element-transformation' => true,
00166                                                     'transform-parameters' => true,
00167                                                     'input-as-parameter' => true,
00168                                                     'element-transformation-func' => 'isTransform' ),
00169                       $this->IsClassName => array( 'input' => true,
00170                                                    'output' => true,
00171                                                    'parameters' => true,
00172                                                    'element-transformation' => true,
00173                                                    'transform-parameters' => true,
00174                                                    'input-as-parameter' => true,
00175                                                    'element-transformation-func' => 'isTransform' ),
00176                       $this->IsNullName => array( 'input' => true,
00177                                                   'output' => true,
00178                                                   'parameters' => true,
00179                                                   'element-transformation' => true,
00180                                                   'transform-parameters' => true,
00181                                                   'input-as-parameter' => true,
00182                                                   'element-transformation-func' => 'isTransform' ),
00183                       $this->IsSetName => array( 'input' => true,
00184                                                  'output' => true,
00185                                                  'parameters' => true,
00186                                                  'element-transformation' => true,
00187                                                  'transform-parameters' => true,
00188                                                  'input-as-parameter' => true,
00189                                                  'element-transformation-func' => 'isTransform' ),
00190                       $this->IsUnsetName => array( 'input' => true,
00191                                                    'output' => true,
00192                                                    'parameters' => true,
00193                                                    'element-transformation' => true,
00194                                                    'transform-parameters' => true,
00195                                                    'input-as-parameter' => true,
00196                                                    'element-transformation-func' => 'isTransform' ),
00197                       $this->GetTypeName => array( 'input' => true,
00198                                                    'output' => true,
00199                                                    'parameters' => true,
00200                                                    'element-transformation' => true,
00201                                                    'transform-parameters' => true,
00202                                                    'input-as-parameter' => true,
00203                                                    'element-transformation-func' => 'isTransform' ),
00204                       $this->GetClassName => array( 'input' => true,
00205                                                     'output' => true,
00206                                                     'parameters' => true,
00207                                                     'element-transformation' => true,
00208                                                     'transform-parameters' => true,
00209                                                     'input-as-parameter' => true,
00210                                                     'element-transformation-func' => 'isTransform' ) );
00211     }
00212 
00213     /*!
00214      \reimp
00215     */
00216     function isTransform( $operatorName, &$node, $tpl, &$resourceData,
00217                           $element, $lastElement, $elementList, $elementTree, &$parameters )
00218     {
00219         $values = array();
00220         $values[] = $parameters[0];
00221         $code = '%output% = ';
00222 
00223         switch( $operatorName )
00224         {
00225             case $this->IsArrayName:
00226             {
00227                 $code .= 'is_array( %1% );';
00228             } break;
00229 
00230             case $this->IsBooleanName:
00231             {
00232                 $code .= 'is_bool( %1% );';
00233             } break;
00234 
00235             case $this->IsIntegerName:
00236             {
00237                 $code .= 'is_int( %1% );';
00238             } break;
00239 
00240             case $this->IsFloatName:
00241             {
00242                 $code .= 'is_float( %1% );';
00243             } break;
00244 
00245             case $this->IsNumericName:
00246             {
00247                 $code .= 'is_numeric( %1% );';
00248             } break;
00249 
00250             case $this->IsStringName:
00251             {
00252                 $code .= 'is_string( %1% );';
00253             } break;
00254 
00255             case $this->IsObjectName:
00256             {
00257                 $code .= 'is_object( %1% );';
00258             } break;
00259 
00260             case $this->IsClassName:
00261             {
00262                 $code .= '( strtolower( get_class( %1% ) ) == strtolower( %2% ) );';
00263                 $values[] = $parameters[1];
00264             } break;
00265 
00266             case $this->IsNullName:
00267             {
00268                 $code .= 'is_null( %1% );';
00269             } break;
00270 
00271             case $this->IsSetName:
00272             {
00273                 $code .= 'isset( %1% );';
00274             } break;
00275 
00276             case $this->IsUnsetName:
00277             {
00278                 $code .= '!isset( %1% );';
00279             } break;
00280 
00281             case $this->GetTypeName:
00282             {
00283                 return false;
00284             } break;
00285 
00286             case $this->GetClassName:
00287             {
00288                 $code .= 'strtolower( get_class( %1% ) );';
00289             } break;
00290         }
00291 
00292         return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
00293     }
00294 
00295     /*!
00296      \return true to tell the template engine that the parameter list exists per operator type.
00297     */
00298     function namedParameterPerOperator()
00299     {
00300         return true;
00301     }
00302 
00303     /*!
00304      See eZTemplateOperator::namedParameterList
00305     */
00306     function namedParameterList()
00307     {
00308         return array();
00309     }
00310 
00311     /*!
00312      Examines the input value and outputs a boolean value. See class documentation for more information.
00313     */
00314     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$value, $namedParameters, $placement )
00315     {
00316         if ( isset( $this->PHPNameMap[$operatorName] ) )
00317         {
00318             $typeFunction = $this->PHPNameMap[$operatorName];
00319             $this->checkType( $typeFunction, $tpl, $value, $operatorParameters, $rootNamespace, $currentNamespace, $placement );
00320             return;
00321         }
00322         switch ( $operatorName )
00323         {
00324             case $this->IsClassName:
00325             {
00326                 if ( count( $operatorParameters ) == 1 )
00327                 {
00328                     $className = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00329                     $value = strtolower( get_class( $value ) ) == strtolower( $className );
00330                 }
00331                 else
00332                 {
00333                     $className = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00334                     $value = strtolower( get_class( $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace, $placement ) ) ) == strtolower( $className );
00335                 }
00336             } break;
00337             case $this->IsSetName:
00338             {
00339                 if ( count( $operatorParameters ) > 0 )
00340                 {
00341                     if ( count( $operatorParameters ) > 1 )
00342                     {
00343                         $tpl->extraParameters( $operatorName, count( $operatorParameters ), 1 );
00344                     }
00345 
00346                     $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement, true );
00347                     $value = $operand !== null;
00348 
00349 
00350                 }
00351                 else
00352                     $tpl->missingParameter( $operatorName, 'input' );
00353 
00354             } break;
00355             case $this->IsUnsetName:
00356             {
00357                 if ( count( $operatorParameters ) > 0 )
00358                 {
00359                     if ( count( $operatorParameters ) > 1 )
00360                         $tpl->extraParameters( $operatorName,
00361                                                count( $operatorParameters ),
00362                                                1 );
00363                     $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement, true );
00364                     $value = $operand === null;
00365                 }
00366                 else
00367                     $tpl->missingParameter( $operatorName, 'input' );
00368             } break;
00369             case $this->GetTypeName:
00370             {
00371                 if ( count( $operatorParameters ) > 0 )
00372                 {
00373                     if ( count( $operatorParameters ) > 1 )
00374                         $tpl->extraParameters( $operatorName,
00375                                                count( $operatorParameters ),
00376                                                1 );
00377                     $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00378                 }
00379                 else
00380                     $operand =& $value;
00381                 if ( $operand === null )
00382                     $value = 'null';
00383                 else if ( is_bool( $operand ) )
00384                     $value = 'boolean[' . ( $operand ? 'true' : 'false' ) . ']';
00385                 else if ( is_object( $operand ) )
00386                     $value = 'object[' . get_class( $operand ) . ']';
00387                 else if ( is_array( $operand ) )
00388                     $value = 'array[' . count( $operand ) . ']';
00389                 else if ( is_string( $operand ) )
00390                     $value = 'string[' . strlen( $operand ) . ']';
00391                 else
00392                     $value = gettype( $operand );
00393             } break;
00394             case $this->GetClassName:
00395             {
00396                 if ( count( $operatorParameters ) > 0 )
00397                 {
00398                     if ( count( $operatorParameters ) > 1 )
00399                         $tpl->extraParameters( $operatorName,
00400                                                count( $operatorParameters ),
00401                                                1 );
00402                     $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00403                     $value = strtolower( get_class( $operand ) );
00404                 }
00405                 else
00406                 {
00407                     $value = strtolower( get_class( $value ) );
00408                 }
00409             } break;
00410         }
00411     }
00412 
00413     function checkType( $typeFunction, $tpl, &$value, $operatorParameters, $rootNamespace, $currentNamespace, $placement )
00414     {
00415         if ( count( $operatorParameters ) > 0 )
00416         {
00417             $value = true;
00418             for ( $i = 0; $i < count( $operatorParameters ); ++$i )
00419             {
00420                 $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00421                 if ( !$typeFunction( $operand) )
00422                     $value = false;
00423             }
00424         }
00425         else
00426         {
00427             $value = $typeFunction( $value );
00428         }
00429     }
00430 
00431     /// The array of operators
00432     public $Operators;
00433     /// The "less than" name
00434     public $IsArrayName;
00435 };
00436 
00437 ?>