PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatearithmeticoperator.php

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