PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatearithmeticoperator.php

https://github.com/aurelienRT1/ezpublish
PHP | 925 lines | 783 code | 54 blank | 88 comment | 122 complexity | ce42e25c30d836460c1d78723874accf MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //
  3. // Definition of eZTemplateArithmeticOperator class
  4. //
  5. // Created on: <06-Oct-2002 18:47:48 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. /*! \file
  31. */
  32. /*!
  33. \class eZTemplateArithmeticOperator eztemplatearithmeticoperator.php
  34. \brief The class eZTemplateArithmeticOperator does
  35. sum
  36. sub
  37. inc
  38. dec
  39. div
  40. mod
  41. mul
  42. max
  43. min
  44. abs
  45. ceil
  46. floor
  47. round
  48. int
  49. float
  50. count
  51. */
  52. class eZTemplateArithmeticOperator
  53. {
  54. /*!
  55. Constructor
  56. */
  57. function eZTemplateArithmeticOperator()
  58. {
  59. $this->Operators = array( 'sum', 'sub', 'inc', 'dec',
  60. 'div', 'mod', 'mul',
  61. 'max', 'min',
  62. 'abs', 'ceil', 'floor', 'round',
  63. 'int', 'float',
  64. 'count',
  65. 'roman',
  66. 'rand' );
  67. foreach ( $this->Operators as $operator )
  68. {
  69. $name = $operator . 'Name';
  70. $name[0] = $name[0] & "\xdf";
  71. $this->$name = $operator;
  72. }
  73. }
  74. /*!
  75. Returns the operators in this class.
  76. */
  77. function operatorList()
  78. {
  79. return $this->Operators;
  80. }
  81. function operatorTemplateHints()
  82. {
  83. return array( $this->SumName => array( 'input' => true,
  84. 'output' => true,
  85. 'parameters' => true,
  86. 'element-transformation' => true,
  87. 'transform-parameters' => true,
  88. 'input-as-parameter' => true,
  89. 'element-transformation-func' => 'basicTransformation'),
  90. $this->SubName => array( 'input' => true,
  91. 'output' => true,
  92. 'parameters' => true,
  93. 'element-transformation' => true,
  94. 'transform-parameters' => true,
  95. 'input-as-parameter' => true,
  96. 'element-transformation-func' => 'basicTransformation'),
  97. $this->MulName => array( 'input' => true,
  98. 'output' => true,
  99. 'parameters' => true,
  100. 'element-transformation' => true,
  101. 'transform-parameters' => true,
  102. 'input-as-parameter' => true,
  103. 'element-transformation-func' => 'basicTransformation'),
  104. $this->DivName => array( 'input' => true,
  105. 'output' => true,
  106. 'parameters' => true,
  107. 'element-transformation' => true,
  108. 'transform-parameters' => true,
  109. 'input-as-parameter' => true,
  110. 'element-transformation-func' => 'basicTransformation'),
  111. $this->IncName => array( 'input' => true,
  112. 'output' => true,
  113. 'parameters' => 1,
  114. 'element-transformation' => true,
  115. 'transform-parameters' => true,
  116. 'input-as-parameter' => true,
  117. 'element-transformation-func' => 'decIncTransformation'),
  118. $this->DecName => array( 'input' => true,
  119. 'output' => true,
  120. 'parameters' => 1,
  121. 'element-transformation' => true,
  122. 'transform-parameters' => true,
  123. 'input-as-parameter' => true,
  124. 'element-transformation-func' => 'decIncTransformation'),
  125. $this->ModName => array( 'input' => true,
  126. 'output' => true,
  127. 'parameters' => 2,
  128. 'element-transformation' => true,
  129. 'transform-parameters' => true,
  130. 'input-as-parameter' => true,
  131. 'element-transformation-func' => 'modTransformation'),
  132. $this->MaxName => array( 'input' => true,
  133. 'output' => true,
  134. 'parameters' => true,
  135. 'element-transformation' => true,
  136. 'transform-parameters' => true,
  137. 'input-as-parameter' => true,
  138. 'element-transformation-func' => 'minMaxTransformation'),
  139. $this->MinName => array( 'input' => true,
  140. 'output' => true,
  141. 'parameters' => true,
  142. 'element-transformation' => true,
  143. 'transform-parameters' => true,
  144. 'input-as-parameter' => true,
  145. 'element-transformation-func' => 'minMaxTransformation'),
  146. $this->AbsName => array( 'input' => true,
  147. 'output' => true,
  148. 'parameters' => 1,
  149. 'element-transformation' => true,
  150. 'transform-parameters' => true,
  151. 'input-as-parameter' => true,
  152. 'element-transformation-func' => 'roundTransformation'),
  153. $this->CeilName => array( 'input' => true,
  154. 'output' => true,
  155. 'parameters' => 1,
  156. 'element-transformation' => true,
  157. 'transform-parameters' => true,
  158. 'input-as-parameter' => true,
  159. 'element-transformation-func' => 'roundTransformation'),
  160. $this->FloorName => array( 'input' => true,
  161. 'output' => true,
  162. 'parameters' => 1,
  163. 'element-transformation' => true,
  164. 'transform-parameters' => true,
  165. 'input-as-parameter' => true,
  166. 'element-transformation-func' => 'roundTransformation'),
  167. $this->RoundName => array( 'input' => true,
  168. 'output' => true,
  169. 'parameters' => 1,
  170. 'element-transformation' => true,
  171. 'transform-parameters' => true,
  172. 'input-as-parameter' => true,
  173. 'element-transformation-func' => 'roundTransformation'),
  174. $this->IntName => array( 'input' => true,
  175. 'output' => true,
  176. 'parameters' => 1,
  177. 'element-transformation' => true,
  178. 'transform-parameters' => true,
  179. 'input-as-parameter' => true,
  180. 'element-transformation-func' => 'castTransformation'),
  181. $this->FloatName => array( 'input' => true,
  182. 'output' => true,
  183. 'parameters' => 1,
  184. 'element-transformation' => true,
  185. 'transform-parameters' => true,
  186. 'input-as-parameter' => true,
  187. 'element-transformation-func' => 'castTransformation'),
  188. $this->RomanName => array( 'input' => true,
  189. 'output' => true,
  190. 'parameters' => 1,
  191. 'element-transformation' => true,
  192. 'transform-parameters' => true,
  193. 'input-as-parameter' => true,
  194. 'element-transformation-func' => 'romanTransformation'),
  195. $this->CountName => array( 'input' => true,
  196. 'output' => true,
  197. 'parameters' => 1 ),
  198. $this->RandName => array( 'input' => true,
  199. 'output' => true,
  200. 'parameters' => true,
  201. 'element-transformation' => true,
  202. 'transform-parameters' => true,
  203. 'input-as-parameter' => true,
  204. 'element-transformation-func' => 'randTransformation') );
  205. }
  206. function basicTransformation( $operatorName, &$node, $tpl, &$resourceData,
  207. $element, $lastElement, $elementList, $elementTree, &$parameters )
  208. {
  209. $values = array();
  210. $function = $operatorName;
  211. $divOperation = false;
  212. if ( $function == $this->SumName )
  213. {
  214. $operator = '+';
  215. }
  216. else if ( $function == $this->SubName )
  217. {
  218. $operator = '-';
  219. }
  220. else if ( $function == $this->MulName )
  221. {
  222. $operator = '*';
  223. }
  224. else
  225. {
  226. $divOperation = true;
  227. $operator = '/';
  228. }
  229. if ( count( $parameters ) == 0 )
  230. return false;
  231. $newElements = array();
  232. // Reorder parameters, dynamic elements first then static ones
  233. // Also combine multiple static ones into a single element
  234. $notInitialised = true;
  235. $staticResult = 0;
  236. $isStaticFirst = false;
  237. $allNumeric = true;
  238. $newParameters = array();
  239. $endParameters = array();
  240. $parameterIndex = 0;
  241. foreach ( $parameters as $parameter )
  242. {
  243. if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
  244. {
  245. $allNumeric = false;
  246. $endParameters[] = $parameter;
  247. }
  248. else
  249. {
  250. $staticValue = eZTemplateNodeTool::elementStaticValue( $parameter );
  251. if ( $notInitialised )
  252. {
  253. $staticResult = $staticValue;
  254. if ( $parameterIndex == 0 )
  255. $isStaticFirst = true;
  256. $notInitialised = false;
  257. }
  258. else
  259. {
  260. if ( $function == 'sum' )
  261. {
  262. $staticResult += $staticValue;
  263. }
  264. else if ( $function == 'sub' )
  265. {
  266. if ( $isStaticFirst )
  267. $staticResult -= $staticValue;
  268. else
  269. $staticResult += $staticValue;
  270. }
  271. else if ( $function == 'mul' )
  272. {
  273. $staticResult *= $staticValue;
  274. }
  275. else
  276. {
  277. if ( $isStaticFirst )
  278. $staticResult /= $staticValue;
  279. else
  280. $staticResult *= $staticValue;
  281. }
  282. }
  283. $isPreviousStatic = true;
  284. }
  285. ++$parameterIndex;
  286. }
  287. if ( $allNumeric )
  288. {
  289. $newElements[] = eZTemplateNodeTool::createNumericElement( $staticResult );
  290. return $newElements;
  291. }
  292. else
  293. {
  294. if ( !$notInitialised )
  295. {
  296. if ( $isStaticFirst )
  297. $newParameters[] = array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  298. else
  299. $endParameters[] = array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  300. }
  301. $newParameters = array_merge( $newParameters, $endParameters );
  302. $code = '';
  303. if ( $divOperation )
  304. {
  305. $code .= '@';
  306. }
  307. $code .= '%output% =';
  308. $counter = 1;
  309. $index = 0;
  310. foreach ( $newParameters as $parameter )
  311. {
  312. if ( $index > 0 )
  313. {
  314. $code .= " $operator";
  315. }
  316. if ( eZTemplateNodeTool::isStaticElement( $parameter ) )
  317. {
  318. $staticValue = eZTemplateNodeTool::elementStaticValue( $parameter );
  319. if ( !is_numeric( $staticValue ) )
  320. $staticValue = (int)$staticValue;
  321. $code .= sprintf(" %F", $staticValue);
  322. }
  323. else
  324. {
  325. $code .= " %$counter%";
  326. $values[] = $parameter;
  327. ++$counter;
  328. }
  329. ++$index;
  330. }
  331. $code .= ";\n";
  332. }
  333. $knownType = 'integer';
  334. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, false, false, $knownType );
  335. return $newElements;
  336. }
  337. function minMaxTransformation( $operatorName, &$node, $tpl, &$resourceData,
  338. $element, $lastElement, $elementList, $elementTree, &$parameters )
  339. {
  340. $values = array();
  341. $function = $operatorName;
  342. if ( count( $parameters ) == 0 )
  343. return false;
  344. $newElements = array();
  345. /* Check if all variables are integers. This is for optimization */
  346. $staticResult = array();
  347. $allNumeric = true;
  348. foreach ( $parameters as $parameter )
  349. {
  350. if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
  351. {
  352. $allNumeric = false;
  353. }
  354. else
  355. {
  356. $staticResult[] = eZTemplateNodeTool::elementStaticValue( $parameter );
  357. }
  358. }
  359. if ( $allNumeric )
  360. {
  361. $staticResult = $function( $staticResult );
  362. return array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  363. }
  364. else
  365. {
  366. $code = "%output% = $function(";
  367. $counter = 1;
  368. foreach ( $parameters as $parameter )
  369. {
  370. if ( $counter > 1 )
  371. {
  372. $code .= ', ';
  373. }
  374. $code .= " %$counter%";
  375. $values[] = $parameter;
  376. ++$counter;
  377. }
  378. $code .= ");\n";
  379. }
  380. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  381. return $newElements;
  382. }
  383. function modTransformation( $operatorName, &$node, $tpl, &$resourceData,
  384. $element, $lastElement, $elementList, $elementTree, &$parameters )
  385. {
  386. $values = array();
  387. if ( count( $parameters ) != 2 )
  388. return false;
  389. $newElements = array();
  390. if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) && eZTemplateNodeTool::isStaticElement( $parameters[1] ) )
  391. {
  392. $staticResult = eZTemplateNodeTool::elementStaticValue( $parameters[0] ) % eZTemplateNodeTool::elementStaticValue( $parameters[1] );
  393. return array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  394. }
  395. else
  396. {
  397. $code = "%output% = %1% % %2%;\n";
  398. $values[] = $parameters[0];
  399. $values[] = $parameters[1];
  400. }
  401. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  402. return $newElements;
  403. }
  404. function roundTransformation( $operatorName, &$node, $tpl, &$resourceData,
  405. $element, $lastElement, $elementList, $elementTree, &$parameters )
  406. {
  407. $values = array();
  408. $function = $operatorName;
  409. if ( count( $parameters ) != 1 )
  410. return false;
  411. $newElements = array();
  412. if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
  413. {
  414. $staticResult = $function( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) );
  415. return array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  416. }
  417. else
  418. {
  419. $code = "%output% = $function( %1% );\n";
  420. $values[] = $parameters[0];
  421. }
  422. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  423. return $newElements;
  424. }
  425. function decIncTransformation( $operatorName, &$node, $tpl, &$resourceData,
  426. $element, $lastElement, $elementList, $elementTree, &$parameters )
  427. {
  428. $values = array();
  429. $function = $operatorName;
  430. $direction = $this->DecName == $function ? -1 : 1;
  431. if ( count( $parameters ) < 1 )
  432. return false;
  433. $newElements = array();
  434. if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
  435. {
  436. return array( eZTemplateNodeTool::createNumericElement( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) + $direction ) );
  437. }
  438. else
  439. {
  440. $code = "%output% = %1% + $direction;\n";
  441. $values[] = $parameters[0];
  442. }
  443. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  444. return $newElements;
  445. }
  446. function castTransformation( $operatorName, &$node, $tpl, &$resourceData,
  447. $element, $lastElement, $elementList, $elementTree, &$parameters )
  448. {
  449. $values = array();
  450. if ( count( $parameters ) != 1 )
  451. return false;
  452. $newElements = array();
  453. if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
  454. {
  455. $staticResult = ( $operatorName == $this->IntName ) ? (int) eZTemplateNodeTool::elementStaticValue( $parameters[0] ) : (float) eZTemplateNodeTool::elementStaticValue( $parameters[0] );
  456. return array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  457. }
  458. else
  459. {
  460. $code = "%output% = ($operatorName)%1%;\n";
  461. $values[] = $parameters[0];
  462. }
  463. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  464. return $newElements;
  465. }
  466. function randTransformation( $operatorName, &$node, $tpl, &$resourceData,
  467. $element, $lastElement, $elementList, $elementTree, &$parameters )
  468. {
  469. $paramCount = count( $parameters );
  470. if ( $paramCount != 0 ||
  471. $paramCount != 2 )
  472. {
  473. return false;
  474. }
  475. $values = array();
  476. $newElements = array();
  477. if ( $paramCount == 2 )
  478. {
  479. $code = "%output% = mt_rand( %1%, %2% );\n";
  480. $values[] = $parameters[0];
  481. $values[] = $parameters[1];
  482. }
  483. else
  484. {
  485. $code = "%output% = mt_rand();\n";
  486. }
  487. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  488. return $newElements;
  489. }
  490. function romanTransformation( $operatorName, &$node, $tpl, &$resourceData,
  491. $element, $lastElement, $elementList, $elementTree, &$parameters )
  492. {
  493. $values = array();
  494. if ( count( $parameters ) != 1 )
  495. return false;
  496. $newElements = array();
  497. if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
  498. {
  499. $staticResult = $this->buildRoman( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) );
  500. return array( eZTemplateNodeTool::createNumericElement( $staticResult ) );
  501. }
  502. else
  503. {
  504. return false;
  505. }
  506. }
  507. /*!
  508. \return true to tell the template engine that the parameter list exists per operator type.
  509. */
  510. function namedParameterPerOperator()
  511. {
  512. return true;
  513. }
  514. /*!
  515. See eZTemplateOperator::namedParameterList
  516. */
  517. function namedParameterList()
  518. {
  519. return array( $this->IncName => array( 'value' => array( 'type' => 'mixed',
  520. 'required' => false,
  521. 'default' => false ) ),
  522. $this->DecName => array( 'value' => array( 'type' => 'mixed',
  523. 'required' => false,
  524. 'default' => false ) ),
  525. $this->RomanName => array( 'value' => array( 'type' => 'mixed',
  526. 'required' => false,
  527. 'default' => false ) ) );
  528. }
  529. /*!
  530. \private
  531. \obsolete This function adds too much complexity, don't use it anymore
  532. */
  533. function numericalValue( $mixedValue )
  534. {
  535. if ( is_array( $mixedValue ) )
  536. {
  537. return count( $mixedValue );
  538. }
  539. else if ( is_object( $mixedValue ) )
  540. {
  541. if ( method_exists( $mixedValue, 'attributes' ) )
  542. return count( $mixedValue->attributes() );
  543. else if ( method_exists( $mixedValue, 'numericalValue' ) )
  544. return $mixedValue->numericalValue();
  545. }
  546. else if ( is_numeric( $mixedValue ) )
  547. return $mixedValue;
  548. else
  549. return 0;
  550. }
  551. /*!
  552. Examines the input value and outputs a boolean value. See class documentation for more information.
  553. */
  554. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
  555. $placement )
  556. {
  557. switch ( $operatorName )
  558. {
  559. case $this->RomanName:
  560. {
  561. if ( $namedParameters['value'] !== false )
  562. $value = $namedParameters['value'];
  563. else
  564. $value = $operatorValue;
  565. $operatorValue = $this->buildRoman( $value );
  566. } break;
  567. case $this->CountName:
  568. {
  569. if ( count( $operatorParameters ) == 0 )
  570. $mixedValue =& $operatorValue;
  571. else
  572. $mixedValue = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  573. if ( count( $operatorParameters ) > 1 )
  574. $tpl->extraParameters( $operatorName, count( $operatorParameters ), 1 );
  575. if ( is_array( $mixedValue ) )
  576. $operatorValue = count( $mixedValue );
  577. else if ( is_object( $mixedValue ) and
  578. method_exists( $mixedValue, 'attributes' ) )
  579. $operatorValue = count( $mixedValue->attributes() );
  580. else if ( is_string( $mixedValue ) )
  581. $operatorValue = strlen( $mixedValue );
  582. else
  583. $operatorValue = 0;
  584. } break;
  585. case $this->SumName:
  586. {
  587. $value = 0;
  588. if ( $operatorValue !== null )
  589. $value = $operatorValue;
  590. for ( $i = 0; $i < count( $operatorParameters ); ++$i )
  591. {
  592. $tmpValue = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  593. $value += $tmpValue;
  594. }
  595. $operatorValue = $value;
  596. } break;
  597. case $this->SubName:
  598. {
  599. $values = array();
  600. if ( $operatorValue !== null )
  601. $values[] = $operatorValue;
  602. for ( $i = 0; $i < count( $operatorParameters ); ++$i )
  603. {
  604. $values[] = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  605. }
  606. $value = 0;
  607. if ( count( $values ) > 0 )
  608. {
  609. $value = $values[0];
  610. for ( $i = 1; $i < count( $values ); ++$i )
  611. {
  612. $value -= $values[$i];
  613. }
  614. }
  615. $operatorValue = $value;
  616. } break;
  617. case $this->IncName:
  618. case $this->DecName:
  619. {
  620. if ( $operatorValue !== null )
  621. $value = $operatorValue;
  622. else
  623. $value = $namedParameters['value'];
  624. if ( $operatorName == $this->DecName )
  625. --$value;
  626. else
  627. ++$value;
  628. $operatorValue = $value;
  629. } break;
  630. case $this->DivName:
  631. {
  632. if ( count( $operatorParameters ) < 1 )
  633. {
  634. $tpl->warning( $operatorName, 'Requires at least 1 parameter value', $placement );
  635. return;
  636. }
  637. $i = 0;
  638. if ( $operatorValue !== null )
  639. $value = $operatorValue;
  640. else
  641. $value = $tpl->elementValue( $operatorParameters[$i++], $rootNamespace, $currentNamespace, $placement );
  642. for ( ; $i < count( $operatorParameters ); ++$i )
  643. {
  644. $tmpValue = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  645. @$value /= $tmpValue;
  646. }
  647. $operatorValue = $value;
  648. } break;
  649. case $this->ModName:
  650. {
  651. if ( count( $operatorParameters ) < 1 )
  652. {
  653. $tpl->warning( $operatorName, 'Missing dividend and divisor', $placement );
  654. return;
  655. }
  656. if ( count( $operatorParameters ) == 1 )
  657. {
  658. $dividend = $operatorValue;
  659. $divisor = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  660. }
  661. else
  662. {
  663. $dividend = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  664. $divisor = $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace, $placement );
  665. }
  666. $operatorValue = $dividend % $divisor;
  667. } break;
  668. case $this->MulName:
  669. {
  670. if ( count( $operatorParameters ) < 1 )
  671. {
  672. $tpl->warning( $operatorName, 'Requires at least 1 parameter value', $placement );
  673. return;
  674. }
  675. $i = 0;
  676. if ( $operatorValue !== null )
  677. $value = $operatorValue;
  678. else
  679. $value = $tpl->elementValue( $operatorParameters[$i++], $rootNamespace, $currentNamespace, $placement );
  680. for ( ; $i < count( $operatorParameters ); ++$i )
  681. {
  682. $tmpValue = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  683. $value *= $tmpValue;
  684. }
  685. $operatorValue = $value;
  686. } break;
  687. case $this->MaxName:
  688. {
  689. if ( count( $operatorParameters ) < 1 )
  690. {
  691. $tpl->warning( $operatorName, 'Requires at least 1 parameter value', $placement );
  692. return;
  693. }
  694. $i = 0;
  695. if ( $operatorValue !== null )
  696. {
  697. $value = $operatorValue;
  698. }
  699. else
  700. {
  701. $value = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  702. ++$i;
  703. }
  704. for ( ; $i < count( $operatorParameters ); ++$i )
  705. {
  706. $tmpValue = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  707. if ( $tmpValue > $value )
  708. $value = $tmpValue;
  709. }
  710. $operatorValue = $value;
  711. } break;
  712. case $this->MinName:
  713. {
  714. if ( count( $operatorParameters ) < 1 )
  715. {
  716. $tpl->warning( $operatorName, 'Requires at least 1 parameter value', $placement );
  717. return;
  718. }
  719. $value = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  720. for ( $i = 1; $i < count( $operatorParameters ); ++$i )
  721. {
  722. $tmpValue = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  723. if ( $tmpValue < $value )
  724. $value = $tmpValue;
  725. }
  726. $operatorValue = $value;
  727. } break;
  728. case $this->AbsName:
  729. case $this->CeilName:
  730. case $this->FloorName:
  731. case $this->RoundName:
  732. {
  733. if ( count( $operatorParameters ) < 1 )
  734. $value = $operatorValue;
  735. else
  736. $value = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  737. switch ( $operatorName )
  738. {
  739. case $this->AbsName:
  740. {
  741. $operatorValue = abs( $value );
  742. } break;
  743. case $this->CeilName:
  744. {
  745. $operatorValue = ceil( $value );
  746. } break;
  747. case $this->FloorName:
  748. {
  749. $operatorValue = floor( $value );
  750. } break;
  751. case $this->RoundName:
  752. {
  753. $operatorValue = round( $value );
  754. } break;
  755. }
  756. } break;
  757. case $this->IntName:
  758. {
  759. if ( count( $operatorParameters ) > 0 )
  760. $value = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  761. else
  762. $value = $operatorValue;
  763. $operatorValue = (int)$value;
  764. } break;
  765. case $this->FloatName:
  766. {
  767. if ( count( $operatorParameters ) > 0 )
  768. $value = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
  769. else
  770. $value = $operatorValue;
  771. $operatorValue = (float)$value;
  772. } break;
  773. case $this->RandName:
  774. {
  775. if ( count( $operatorParameters ) == 2 )
  776. {
  777. $operatorValue = mt_rand( $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement ),
  778. $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace, $placement ) );
  779. }
  780. else
  781. {
  782. $operatorValue = mt_rand();
  783. }
  784. } break;
  785. }
  786. }
  787. /// \privatesection
  788. /*!
  789. \private
  790. Recursive function for calculating roman numeral from integer
  791. \param integer value
  792. \return next chars for for current value
  793. */
  794. function buildRoman( $value )
  795. {
  796. if ( $value >= 1000 )
  797. return 'M'.$this->buildRoman( $value - 1000 );
  798. if ( $value >= 500 )
  799. {
  800. if ( $value >= 900 )
  801. return 'CM'.$this->buildRoman( $value - 900 );
  802. else
  803. return 'D'.$this->buildRoman( $value - 500 );
  804. }
  805. if ( $value >= 100 )
  806. {
  807. if( $value >= 400 )
  808. return 'CD'.$this->buildRoman( $value - 400 );
  809. else
  810. return 'C'.$this->buildRoman( $value - 100 );
  811. }
  812. if ( $value >= 50 )
  813. {
  814. if( $value >= 90 )
  815. return 'XC'.$this->buildRoman( $value - 90 );
  816. else
  817. return 'L'.$this->buildRoman( $value - 50 );
  818. }
  819. if ( $value >= 10 )
  820. {
  821. if( $value >= 40 )
  822. return 'XL'.$this->buildRoman( $value - 40 );
  823. else
  824. return 'X'.$this->buildRoman( $value - 10 );
  825. }
  826. if ( $value >= 5 )
  827. {
  828. if( $value == 9 )
  829. return 'IX'.$this->buildRoman( $value - 9 );
  830. else
  831. return 'V'.$this->buildRoman( $value - 5 );
  832. }
  833. if ( $value >= 1 )
  834. {
  835. if( $value == 4 )
  836. return 'IV'.$this->buildRoman( $value - 4 );
  837. else
  838. return 'I'.$this->buildRoman( $value - 1 );
  839. }
  840. return '';
  841. }
  842. public $Operators;
  843. public $SumName;
  844. public $SubName;
  845. public $IncName;
  846. public $DecName;
  847. public $DivName;
  848. public $ModName;
  849. public $MulName;
  850. public $MaxName;
  851. public $MinName;
  852. public $AbsName;
  853. public $CeilName;
  854. public $FloorName;
  855. public $RoundName;
  856. public $IntName;
  857. public $FloatName;
  858. public $CountName;
  859. public $RomanName;
  860. public $RandName;
  861. }
  862. ?>