[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/eztemplate/classes/ -> eztemplatelogicoperator.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateLogicOperator class
   4  //
   5  // Created on: <18-Apr-2002 12:15:07 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZTemplateLogicOperator eztemplatelogicoperator.php
  31    \ingroup eZTemplateOperators
  32    \brief Logical operators for creating and manipulating booleans
  33  
  34    This class adds powerful template handling by enabling logical operators
  35    which alter the output of templates from input values.
  36  
  37    How counts are interpreted:
  38    -# If the data is an array the array count is used
  39    -# If the data is an object the object attribute count is used
  40    -# If the data is a numeric the value is used
  41    -# If the data is a boolean false is 0 and true is 1
  42    -# For all other data 0 is used
  43  
  44    Data is considered null (or false) if the data count is 0 (see above) or
  45    the data is really null (is_null). Data is considered true if it is not null.
  46  
  47    The supported operators are:
  48    - lt\n
  49      Returns true if the input count is less than the parameter data count. See how
  50      count is interpreted above.
  51    - le\n
  52      Same as lt but use less than or equal to.
  53    - gt\n
  54      Same as lt but returns true for input greater than data.
  55    - ge\n
  56      Same as gt but use greater than or equal to.
  57    - eq\n
  58      Returns true if all the input parameters match. Matching is casual meaning
  59      that an integer of value 0 will match a boolean of type false.
  60    - ne\n
  61      Returns true if one or more of the input parameters does not match.
  62      Matching is casual meaning that an integer of value 0 will match a boolean
  63      of type false.
  64    - null\n
  65      Returns true if the data is null, false otherwise
  66    - not\n
  67      Returns true if the data is false or false if data is true
  68    - true
  69    - false\n
  70      Creates a true/false boolean
  71    - or\n
  72      Evaluates all parameter values until one is found to be true (see above), then
  73      returns that value. The remaining parameters are not evaluated at all.
  74      If there are no parameter or all elements were false it returns false.
  75    - and\n
  76      Evaluates all parameter values until one is found to be false (see above), then
  77      returns that false. The remaining parameters are not evaluated at all.
  78      If there are no parameter it returns false, if no elements were false it returns the last parameter value.
  79    - choose\n
  80      Uses the input count to pick one of the parameter elements. The input count equals
  81      the parameter index.
  82  
  83  */
  84  
  85  class eZTemplateLogicOperator
  86  {
  87      /*!
  88       Initializes the operator class with the various operator names.
  89      */
  90      function eZTemplateLogicOperator()
  91      {
  92          $this->Operators = array( 'lt', 'gt', 'le', 'ge', 'eq', 'ne',
  93                                    'null', 'not',
  94                                    'or', 'and',
  95                                    'true', 'false', 'choose' );
  96          foreach ( $this->Operators as $operator )
  97          {
  98              $name = $operator . 'Name';
  99              $name[0] = $name[0] & "\xdf";
 100              $this->$name = $operator;
 101          }
 102      }
 103  
 104      /*!
 105       Returns the operators in this class.
 106      */
 107      function &operatorList()
 108      {
 109          return $this->Operators;
 110      }
 111  
 112      /*!
 113       \return true to tell the template engine that the parameter list exists per operator type.
 114      */
 115      function namedParameterPerOperator()
 116      {
 117          return true;
 118      }
 119  
 120      function operatorTemplateHints()
 121      {
 122          return array( $this->LtName => array( 'input' => true,
 123                                                'output' => true,
 124                                                'parameters' => 1,
 125                                                'element-transformation' => true,
 126                                                'transform-parameters' => true,
 127                                                'input-as-parameter' => true,
 128                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 129                        $this->GtName => array( 'input' => true,
 130                                                'output' => true,
 131                                                'parameters' => 1,
 132                                                'element-transformation' => true,
 133                                                'transform-parameters' => true,
 134                                                'input-as-parameter' => true,
 135                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 136                        $this->LeName => array( 'input' => true,
 137                                                'output' => true,
 138                                                'parameters' => 1,
 139                                                'element-transformation' => true,
 140                                                'transform-parameters' => true,
 141                                                'input-as-parameter' => true,
 142                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 143                        $this->GeName => array( 'input' => true,
 144                                                'output' => true,
 145                                                'parameters' => 1,
 146                                                'element-transformation' => true,
 147                                                'transform-parameters' => true,
 148                                                'input-as-parameter' => true,
 149                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 150  
 151                        $this->EqName => array( 'input' => true,
 152                                                'output' => true,
 153                                                'parameters' => true,
 154                                                'element-transformation' => true,
 155                                                'transform-parameters' => true,
 156                                                'input-as-parameter' => true,
 157                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 158                        $this->NeName => array( 'input' => true,
 159                                                'output' => true,
 160                                                'parameters' => true,
 161                                                'element-transformation' => true,
 162                                                'transform-parameters' => true,
 163                                                'input-as-parameter' => true,
 164                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 165  
 166                        $this->NullName  => array( 'input' => true,
 167                                                   'output' => true,
 168                                                   'parameters' => false ),
 169  
 170                        $this->OrName => array( 'input' => true,
 171                                                'output' => true,
 172                                                'parameters' => true,
 173                                                'element-transformation' => true,
 174                                                'transform-parameters' => true,
 175                                                'input-as-parameter' => true,
 176                                                'element-transformation-func' => 'logicalComparisonTransformation'),
 177                        $this->AndName => array( 'input' => true,
 178                                                 'output' => true,
 179                                                 'parameters' => true,
 180                                                 'element-transformation' => true,
 181                                                 'transform-parameters' => true,
 182                                                 'input-as-parameter' => true,
 183                                                 'element-transformation-func' => 'logicalComparisonTransformation'),
 184  
 185                        $this->NotName => array( 'input' => true,
 186                                                 'output' => true,
 187                                                 'parameters' => true,
 188                                                 'element-transformation' => true,
 189                                                 'transform-parameters' => true,
 190                                                 'input-as-parameter' => true,
 191                                                 'element-transformation-func' => 'negateTransformation'),
 192  
 193                        $this->ChooseName => array( 'input' => true,
 194                                                    'output' => true,
 195                                                    'parameters' => true,
 196                                                    'element-transformation' => true,
 197                                                    'transform-parameters' => true,
 198                                                    'input-as-parameter' => 'always',
 199                                                    'element-transformation-func' => 'chooseTransformation'),
 200                        $this->TrueName => array( 'input' => false,
 201                                                  'output' => true,
 202                                                  'parameters' => false,
 203                                                  'static' => true,
 204                                                  'element-transformation' => true,
 205                                                  'transform-parameters' => true,
 206                                                  'input-as-parameter' => true,
 207                                                  'element-transformation-func' => 'trueFalseTransformation'),
 208                        $this->FalseName => array( 'input' => false,
 209                                                   'output' => true,
 210                                                   'parameters' => false,
 211                                                   'static' => true,
 212                                                   'element-transformation' => true,
 213                                                   'transform-parameters' => true,
 214                                                   'input-as-parameter' => true,
 215                                                   'element-transformation-func' => 'trueFalseTransformation') );
 216      }
 217  
 218      function operatorCompiledStaticData( $operatorName )
 219      {
 220          switch( $operatorName )
 221          {
 222              case $this->TrueName:
 223              {
 224                  return true;
 225              } break;
 226              case $this->FalseName:
 227              {
 228                  return false;
 229              } break;
 230          }
 231          return false;
 232      }
 233  
 234      /*!
 235       See eZTemplateOperator::namedParameterList
 236      */
 237      function namedParameterList()
 238      {
 239          return array( $this->LtName => array( "threshold" => array( "type" => "mixed",
 240                                                                      "required" => true,
 241                                                                      "default" => false ) ),
 242                        $this->GtName => array( "threshold" => array( "type" => "mixed",
 243                                                                      "required" => true,
 244                                                                      "default" => false ) ),
 245                        $this->LeName => array( "threshold" => array( "type" => "mixed",
 246                                                                      "required" => true,
 247                                                                      "default" => false ) ),
 248                        $this->GeName => array( "threshold" => array( "type" => "mixed",
 249                                                                      "required" => true,
 250                                                                      "default" => false ) ) );
 251      }
 252  
 253  
 254      function logicalComparisonTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 255                                                 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 256      {
 257          $values = array();
 258          $function = $operatorName;
 259          $minParameterCount = $maxParameterCount = 2;
 260  
 261          switch ( $operatorName )
 262          {
 263          case 'lt':
 264              $operator = '<';
 265              break;
 266          case 'le':
 267              $operator = '<=';
 268              break;
 269          case 'gt':
 270              $operator = '>';
 271              break;
 272          case 'ge':
 273              $operator = '>=';
 274              break;
 275          case 'eq':
 276              $operator = '==';
 277              break;
 278          case 'ne':
 279              $operator = '!=';
 280              break;
 281          case 'and':
 282              $operator = 'and';
 283              $maxParameterCount = false;
 284              break;
 285          case 'or':
 286              $operator = 'or';
 287              $maxParameterCount = false;
 288              break;
 289          }
 290          if ( ( count( $parameters ) < 2 ) ||
 291               ( $maxParameterCount && ( count( $parameters ) > $maxParameterCount ) ) )
 292          {
 293              return false;
 294          }
 295          $newElements = array();
 296  
 297          if ( $operatorName == 'or' )
 298          {
 299              $staticResult = false;
 300              $staticValue = null;
 301              $dynamicParameters = array();
 302              $addDynamic = false;
 303              $lastValue = null;
 304              foreach ( $parameters as $parameter )
 305              {
 306                  if ( $addDynamic )
 307                  {
 308                      $dynamicParameters[] = $parameter;
 309                      continue;
 310                  }
 311                  if ( eZTemplateNodeTool::isStaticElement( $parameter ) )
 312                  {
 313                      $parameterValue = eZTemplateNodeTool::elementStaticValue( $parameter );
 314                      if ( $staticValue === null )
 315                      {
 316                          $staticValue = $parameterValue;
 317                      }
 318                      else
 319                      {
 320                          $staticValue = ( $staticValue or $parameterValue );
 321                      }
 322                      $lastValue = $parameterValue;
 323                      if ( $parameterValue )
 324                      {
 325                          $staticResult = true;
 326                          break;
 327                      }
 328                      continue;
 329                  }
 330                  $addDynamic = true;
 331                  $dynamicParameters[] = $parameter;
 332                  $staticValue = null;
 333              }
 334              if ( count( $dynamicParameters ) == 0 )
 335              {
 336                  if ( !$staticResult )
 337                      $lastValue = false;
 338                  $newElements[] = eZTemplateNodeTool::createStaticElement( $lastValue );
 339                  return $newElements;
 340              }
 341  
 342              $code = '';
 343              $counter = 0;
 344              foreach ( $dynamicParameters as $parameter )
 345              {
 346                  if ( $counter++ )
 347                  {
 348                      $code .= "else ";
 349                  }
 350                  $code .= ( "if ( %$counter% )\n" .
 351                             "    %output% = %$counter%;\n" );
 352                  $values[] = $parameter;
 353              }
 354              $code .= ( "else\n" .
 355                         "    %output% = false;\n" );
 356          }
 357          else if ( $operatorName == 'and' )
 358          {
 359              $staticResult = false;
 360              $staticValue = null;
 361              $dynamicParameters = array();
 362              $addDynamic = false;
 363              $lastValue = null;
 364              foreach ( $parameters as $parameter )
 365              {
 366                  if ( $addDynamic )
 367                  {
 368                      $dynamicParameters[] = $parameter;
 369                      continue;
 370                  }
 371                  if ( eZTemplateNodeTool::isStaticElement( $parameter ) )
 372                  {
 373                      $parameterValue = eZTemplateNodeTool::elementStaticValue( $parameter );
 374                      if ( $staticValue === null )
 375                      {
 376                          $staticValue = $parameterValue;
 377                      }
 378                      else
 379                      {
 380                          $staticValue = ( $staticValue and $parameterValue );
 381                      }
 382                      $lastValue = $parameterValue;
 383                      if ( !$parameterValue )
 384                      {
 385                          $lastValue = false;
 386                          $staticResult = true;
 387                          break;
 388                      }
 389                      continue;
 390                  }
 391                  $addDynamic = true;
 392                  $dynamicParameters[] = $parameter;
 393                  $staticValue = null;
 394              }
 395              if ( count( $dynamicParameters ) == 0 )
 396              {
 397                  $newElements[] = eZTemplateNodeTool::createStaticElement( $lastValue );
 398                  return $newElements;
 399              }
 400  
 401              $code = '';
 402              $counter = 0;
 403              foreach ( $dynamicParameters as $parameter )
 404              {
 405                  if ( $counter++ )
 406                  {
 407                      $code .= "else ";
 408                  }
 409                  $code .= ( "if ( !%$counter% )\n" .
 410                             "    %output% = false;\n" );
 411                  $values[] = $parameter;
 412              }
 413              $code .= ( "else\n" .
 414                         "    %output% = %$counter%;\n" );
 415          }
 416          else
 417          {
 418              $code = '%output% = (';
 419              $counter = 0;
 420              $allStatic = true;
 421              foreach ( $parameters as $parameter )
 422              {
 423                  if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
 424                      $allStatic = false;
 425              }
 426              if ( $allStatic )
 427              {
 428                  switch ( $operatorName )
 429                  {
 430                      case 'lt':
 431                      {
 432                          $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) <
 433                                          eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
 434                      } break;
 435  
 436                      case 'le':
 437                      {
 438                          $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) <=
 439                                          eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
 440                      } break;
 441  
 442                      case 'gt':
 443                      {
 444                          $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) >
 445                                          eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
 446                      } break;
 447  
 448                      case 'ge':
 449                      {
 450                          $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) >=
 451                                          eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
 452                      } break;
 453  
 454                      case 'eq':
 455                      {
 456                          $staticParameters = array();
 457                          foreach ( $parameters as $parameter )
 458                          {
 459                              $staticParameters[] = eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameter ),
 460                                                                                0, 0, false );
 461                          }
 462                          eval( '$evalStatus = ( ' . implode( ' == ', $staticParameters ) . ' );' );
 463                      } break;
 464  
 465                      case 'ne':
 466                      {
 467                          $staticParameters = array();
 468                          foreach ( $parameters as $parameter )
 469                          {
 470                              $staticParameters[] = eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameter ),
 471                                                                                0, 0, false );
 472                          }
 473                          eval( '$evalStatus = ( ' . implode( ' != ', $staticParameters ) . ' );' );
 474                      } break;
 475                      break;
 476                  }
 477                  $newElements[] = eZTemplateNodeTool::createBooleanElement( $evalStatus );
 478                  return $newElements;
 479              }
 480  
 481              foreach ( $parameters as $parameter )
 482              {
 483                  if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
 484                      $allStatic = false;
 485                  if ( $counter++ )
 486                  {
 487                      $code .= " $operator";
 488                  }
 489                  $code .= " ( %$counter% )";
 490                  $values[] = $parameter;
 491              }
 492              $code .= " );\n";
 493          }
 494          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
 495          return $newElements;
 496      }
 497  
 498      function negateTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 499                                     &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 500      {
 501          $values = array();
 502          $function = $operatorName;
 503  
 504          if ( ( count( $parameters ) != 1) )
 505          {
 506              return false;
 507          }
 508          $newElements = array();
 509  
 510          $values[] = $parameters[0];
 511          $code = "%output% = !( %1% );\n";
 512  
 513          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
 514          return $newElements;
 515      }
 516  
 517      function trueFalseTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 518                                        &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 519      {
 520          $values = array();
 521          if ( ( count( $parameters ) != 0 ) )
 522          {
 523              return false;
 524          }
 525          $newElements = array();
 526  
 527          $value = false;
 528          if ( $operatorName == $this->TrueName )
 529              $value = true;
 530          $newElements[] = eZTemplateNodeTool::createBooleanElement( $value );
 531          return $newElements;
 532      }
 533  
 534      function chooseTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 535                                     &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 536      {
 537          $values = array();
 538          $function = $operatorName;
 539  
 540          if ( ( count( $parameters ) < 2) )
 541          {
 542              return false;
 543          }
 544  
 545          $tmpValues = false;
 546          $newElements = array();
 547          if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
 548          {
 549              $selected = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
 550  
 551              if ( $selected < 0 or $selected > ( count( $parameters ) - 1 ) )
 552              {
 553                  return false;
 554              }
 555  
 556              return $parameters[$selected + 1];
 557          }
 558          else
 559          {
 560              $values[] = $parameters[0];
 561              $array = $parameters;
 562              unset( $array[0] );
 563  
 564              $count = count( $parameters ) - 1;
 565              $operatorNameText = eZPHPCreator::variableText( $operatorName );
 566  
 567              if ( count( $parameters ) == ( 2 + 1 ) )
 568              {
 569                  $code = "%output% = %1% ? %3% : %2%;\n";
 570                  $values[] = $parameters[1];
 571                  $values[] = $parameters[2];
 572              }
 573              else
 574              {
 575                  $code = ( "if ( %1% < 0 and\n" .
 576                            "     %1% >= $count )\n" .
 577                            "{\n" .
 578                            "    \$tpl->error( $operatorNameText, \"Index \" . %1% . \" out of range\" );\n" .
 579                            "     %output% = false;\n" .
 580                            "}\n" );
 581                  $code .= "else switch ( %1% )\n{\n";
 582                  $valueNumber = 2;
 583                  for ( $i = 0; $i < $count; ++$i )
 584                  {
 585                      $parameterNumber = $i + 1;
 586                      $code .= "    case $i:";
 587                      if ( eZTemplateNodeTool::isStaticElement( $parameters[$parameterNumber] ) )
 588                      {
 589                          $value = eZTemplateNodeTool::elementStaticValue( $parameters[$parameterNumber] );
 590                          $valueText = eZPHPCreator::variableText( $value, 0, 0, false );
 591                          $code .= " %output% = $valueText; break;\n";
 592                      }
 593                      else
 594                      {
 595                          $code .= "\n    {\n";
 596                          $code .= "%code$valueNumber%\n";
 597                          $code .= "%output% = %$valueNumber%;\n";
 598                          $code .= "    } break;\n";
 599                          $values[] = $parameters[$parameterNumber];
 600                          ++$valueNumber;
 601                      }
 602                  }
 603                  $code .= "}\n";
 604              }
 605          }
 606          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, eZTemplateNodeTool::extractVariableNodePlacement( $node ), false );
 607          return $newElements;
 608      }
 609  
 610      /*!
 611       * Returns the 'count' value as described in the introduction or 'false' in
 612       * case of an unsupported type
 613       */
 614      function getValueCount( $val )
 615      {
 616          $val_cnt = false;
 617  
 618          if ( is_array( $val ) )
 619          {
 620              $val_cnt = count( $val );
 621          }
 622          else if ( is_null( $val ) )
 623          {
 624              $val_cnt = 0;
 625          }
 626          else if ( is_bool( $val ) )
 627          {
 628              $val_cnt = (int)$val;
 629          }
 630          else if ( is_object( $val ) and
 631                    method_exists( $val, "attributes" ) )
 632          {
 633              $val_cnt = count( $val->attributes() );
 634          }
 635          else if ( is_numeric( $val ) )
 636          {
 637              $val_cnt = $val;
 638          }
 639          else if ( is_string( $val ) )
 640          {
 641              $val_cnt = strlen( $val );
 642          }
 643          return $val_cnt;
 644      }
 645  
 646      /*!
 647       Examines the input value and outputs a boolean value. See class documentation for more information.
 648      */
 649      function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$value, &$namedParameters,
 650                       $placement )
 651      {
 652          if ( $operatorName == $this->LtName or $operatorName == $this->GtName or
 653               $operatorName == $this->LeName or $operatorName == $this->GeName )
 654          {
 655              $val = $namedParameters["threshold"];
 656  
 657              if ( ( $val_cnt = $this->getValueCount( $val ) ) === false )
 658              {
 659                  $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $val ) . "( $val ), must be either array, attribute object or numerical", $placement );
 660                  return;
 661              }
 662          }
 663          switch ( $operatorName )
 664          {
 665              case $this->TrueName:
 666              case $this->FalseName:
 667              {
 668                  $value = ( $operatorName == $this->TrueName );
 669              } break;
 670              case $this->NeName:
 671              {
 672                  if ( count( $operatorParameters ) >= 1 )
 673                  {
 674                      if ( count( $operatorParameters ) == 1 )
 675                      {
 676                          $lastOperand =& $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 677                          $value = ( $lastOperand != $value );
 678                      }
 679                      else
 680                      {
 681                          $similar = false;
 682                          $value = false;
 683                          $lastOperand =& $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 684                          for ( $i = 1; $i < count( $operatorParameters ); ++$i )
 685                          {
 686                              $operand =& $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
 687                              if ( $operand != $lastOperand )
 688                              {
 689                                  $value = true;
 690                                  break;
 691                              }
 692                              unset( $lastOperand );
 693                              $lastOperand =& $operand;
 694                          }
 695                      }
 696                  }
 697                  else
 698                  {
 699                      $value = false;
 700                      $tpl->warning( $operatorName, "Requires one parameter for input checking or two or more for parameter checking", $placement );
 701                  }
 702              } break;
 703              case $this->EqName:
 704              {
 705                  if ( count( $operatorParameters ) >= 1 )
 706                  {
 707                      if ( count( $operatorParameters ) == 1 )
 708                      {
 709                          $lastOperand =& $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 710                          $value = ( $lastOperand == $value );
 711                      }
 712                      else
 713                      {
 714                          $similar = false;
 715                          $value = true;
 716                          $lastOperand =& $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 717                          for ( $i = 1; $i < count( $operatorParameters ); ++$i )
 718                          {
 719                              $operand =& $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
 720                              if ( $operand != $lastOperand )
 721                              {
 722                                  $value = false;
 723                                  break;
 724                              }
 725                              unset( $lastOperand );
 726                              $lastOperand =& $operand;
 727                          }
 728                      }
 729                  }
 730                  else
 731                  {
 732                      $value = false;
 733                      $tpl->warning( $operatorName, "Requires one parameter for input checking or two or more for parameter checking", $placement );
 734                  }
 735              } break;
 736              case $this->OrName:
 737              {
 738                  for ( $i = 0; $i < count( $operatorParameters ); ++$i )
 739                  {
 740                      $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
 741                      $operand_logic = false;
 742                      if ( is_array( $operand ) )
 743                          $operand_logic = count( $operand ) > 0;
 744                      else if ( is_numeric( $operand ) )
 745                          $operand_logic = $operand != 0;
 746                      else if ( is_null( $operand ) )
 747                          $operand_logic = false;
 748                      else if ( is_object( $operand ) )
 749                          $operand_logic = ( method_exists( $operand, "attributes" ) and
 750                                             method_exists( $operand, "attribute" ) );
 751                      else if ( is_bool( $operand ) )
 752                          $operand_logic = $operand;
 753                      else if ( is_string( $operand ) )
 754                          $operand_logic =  strlen(trim($operand)) > 0;
 755                      if ( $operand_logic )
 756                      {
 757                          $value = $operand;
 758                          return;
 759                      }
 760                  }
 761                  $value = false;
 762              } break;
 763              case $this->AndName:
 764              {
 765                  $operand = null;
 766                  for ( $i = 0; $i < count( $operatorParameters ); ++$i )
 767                  {
 768                      $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
 769                      $operand_logic = false;
 770                      if ( is_array( $operand ) )
 771                          $operand_logic = count( $operand ) > 0;
 772                      else if ( is_numeric( $operand ) )
 773                          $operand_logic = $operand != 0;
 774                      else if ( is_null( $operand ) )
 775                          $operand_logic = false;
 776                      else if ( is_object( $operand ) )
 777                          $operand_logic = ( method_exists( $operand, "attributes" ) and
 778                                             method_exists( $operand, "attribute" ) );
 779                      else if ( is_bool( $operand ) )
 780                          $operand_logic = $operand;
 781                      else if ( is_string( $operand ) )
 782                          $operand_logic =  strlen(trim($operand)) > 0;
 783                      if ( !$operand_logic )
 784                      {
 785                          $value = false;
 786                          return;
 787                      }
 788                  }
 789                  $value = $operand;
 790              } break;
 791              case $this->ChooseName:
 792              {
 793                  if ( is_array( $value ) or
 794                       ( is_object( $value ) and
 795                         method_exists( $value, "attributes" ) ) )
 796                  {
 797                      $tpl->error( $operatorName, "Only supports numeric and boolean values", $placement );
 798                      return;
 799                  }
 800                  else if ( is_numeric( $value ) )
 801                      $index = $value;
 802                  else if ( is_null( $value ) )
 803                      $index = 0;
 804                  else
 805                      $index = $value ? 1 : 0;
 806                  if ( $index < 0 or
 807                       $index > count( $operatorParameters ) - 1 )
 808                  {
 809                      $tpl->error( $operatorName, "Index $index out of range 0 => " . ( count( $operatorParameters ) - 1 ),
 810                                   $placement );
 811                      $value = false;
 812                      return;
 813                  }
 814                  $value = $tpl->elementValue( $operatorParameters[$index], $rootNamespace, $currentNamespace, $placement );
 815              } break;
 816              case $this->LtName:
 817              case $this->GtName:
 818              case $this->LeName:
 819              case $this->GeName:
 820              {
 821                  if ( $value !== null )
 822                  {
 823                      $operandA = $value;
 824                      $operandB = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 825                  }
 826                  else
 827                  {
 828                      $operandA = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 829                      $operandB = $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace, $placement );
 830                  }
 831  
 832                  if ( ( $cnt = $this->getValueCount( $operandA ) ) === false )
 833                  {
 834                      $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $operandA ) . "( $operandA ), must be either array, attribute object or numerical", $placement );
 835                      return;
 836                  }
 837                  if ( ( $val_cnt = $this->getValueCount( $operandB ) ) === false )
 838                  {
 839                      $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $operandB ) . "( $operandB ), must be either array, attribute object or numerical", $placement );
 840                      return;
 841                  }
 842                  if ( $operatorName == $this->LtName )
 843                      $value = ( $cnt < $val_cnt );
 844                  else if ( $operatorName == $this->GtName )
 845                      $value = ( $cnt > $val_cnt );
 846                  else if ( $operatorName == $this->LeName )
 847                      $value = ( $cnt <= $val_cnt );
 848                  else if ( $operatorName == $this->GeName )
 849                      $value = ( $cnt >= $val_cnt );
 850              } break;
 851              case $this->NullName:
 852              {
 853                  $value = is_null( $value );
 854              } break;
 855              case $this->NotName:
 856              {
 857                  if ( $value === null and isset( $operatorParameters[0] ) )
 858                  {
 859                      $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
 860                  }
 861                  else
 862                  {
 863                      $operand = $value;
 864                  }
 865                  if ( is_array( $operand ) )
 866                      $operand = ( count( $operand ) == 0 );
 867                  else if ( is_null( $operand ) )
 868                      $operand = true;
 869                  else if ( is_object( $operand ) and
 870                            method_exists( $operand, "attributes" ) )
 871                      $operand = ( count( $operand->attributes() ) == 0 );
 872                  else if ( is_numeric( $operand ) )
 873                      $operand = ( $operand == 0 );
 874                  else if ( is_string( $operand ) )
 875                      $operand = ( strlen( $operand ) == 0 );
 876                  else
 877                      $operand = !$operand;
 878                  $value = $operand;
 879              } break;
 880          }
 881      }
 882  
 883      /// \privatesection
 884      /// The array of operators
 885      var $Operators;
 886      /// The "less than" name
 887      var $LtName;
 888      /// The "greater than" name
 889      var $GtName;
 890      /// The "less than or equal" name
 891      var $LeName;
 892      /// The "greater than or equal" name
 893      var $GeName;
 894      /// The "equal" name
 895      var $EqName;
 896      /// The "not equal" name
 897      var $NeName;
 898      /// The "null" name
 899      var $NullName;
 900      /// The "not" name
 901      var $NotName;
 902      /// The "or" name
 903      var $OrName;
 904      /// The "and" name
 905      var $AndName;
 906      /// The "true" name
 907      var $TrueName;
 908      /// The "false" name
 909      var $FalseName;
 910      /// The "choose" name
 911      var $ChooseName;
 912  };
 913  
 914  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7