PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatelogicoperator.php

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