PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatetextoperator.php

https://github.com/zerustech/ezpublish
PHP | 281 lines | 235 code | 19 blank | 27 comment | 34 complexity | 0a3e921720be07513d25dce79e06c1df MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZTemplateTextOperator 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 eZTemplateTextOperator eztemplatetextoperator.php
  12. \brief The class eZTemplateTextOperator does
  13. */
  14. class eZTemplateTextOperator
  15. {
  16. public function __construct()
  17. {
  18. $this->Operators= array( 'concat', 'indent' );
  19. foreach ( $this->Operators as $operator )
  20. {
  21. $name = $operator . 'Name';
  22. $name[0] = $name[0] & "\xdf";
  23. $this->$name = $operator;
  24. }
  25. }
  26. /*!
  27. Returns the operators in this class.
  28. */
  29. function operatorList()
  30. {
  31. return $this->Operators;
  32. }
  33. function operatorTemplateHints()
  34. {
  35. return array( $this->ConcatName => array( 'input' => true,
  36. 'output' => true,
  37. 'parameters' => true,
  38. 'element-transformation' => true,
  39. 'transform-parameters' => true,
  40. 'input-as-parameter' => true,
  41. 'element-transformation-func' => 'concatTransformation'),
  42. $this->IndentName => array( 'input' => true,
  43. 'output' => true,
  44. 'parameters' => 3,
  45. 'element-transformation' => true,
  46. 'transform-parameters' => true,
  47. 'input-as-parameter' => true,
  48. 'element-transformation-func' => 'indentTransformation') ) ;
  49. }
  50. /*!
  51. \return true to tell the template engine that the parameter list exists per operator type.
  52. */
  53. function namedParameterPerOperator()
  54. {
  55. return true;
  56. }
  57. /*!
  58. See eZTemplateOperator::namedParameterList
  59. */
  60. function namedParameterList()
  61. {
  62. return array( $this->IndentName => array( 'indent_count' => array( 'type' => 'integer',
  63. 'required' => true,
  64. 'default' => false ),
  65. 'indent_type' => array( 'type' => 'identifier',
  66. 'required' => false,
  67. 'default' => 'space' ),
  68. 'indent_filler' => array( 'type' => 'string',
  69. 'required' => false,
  70. 'default' => false ) ) );
  71. }
  72. function indentTransformation( $operatorName, &$node, $tpl, &$resourceData,
  73. $element, $lastElement, $elementList, $elementTree, &$parameters )
  74. {
  75. $values = array();
  76. $count = $type = $filler = false;
  77. $paramCount = count( $parameters );
  78. if ( $paramCount == 4 )
  79. {
  80. if ( eZTemplateNodeTool::isConstantElement( $parameters[3] ) )
  81. {
  82. $filler = eZTemplateNodeTool::elementConstantValue( $parameters[3] );
  83. }
  84. }
  85. if ( $paramCount >= 3 )
  86. {
  87. if ( eZTemplateNodeTool::isConstantElement( $parameters[2] ) )
  88. {
  89. $type = eZTemplateNodeTool::elementConstantValue( $parameters[2] );
  90. if ( $type == 'space' )
  91. {
  92. $filler = ' ';
  93. }
  94. else if ( $type == 'tab' )
  95. {
  96. $filler = "\t";
  97. }
  98. else if ( $type != 'custom' )
  99. {
  100. $filler = ' ';
  101. }
  102. }
  103. }
  104. if ( $paramCount >= 2 )
  105. {
  106. if ( eZTemplateNodeTool::isConstantElement( $parameters[1] ) )
  107. {
  108. $count = eZTemplateNodeTool::elementConstantValue( $parameters[1] );
  109. }
  110. if ( $paramCount < 3 )
  111. {
  112. $type = 'space';
  113. $filler = ' ';
  114. }
  115. }
  116. $newElements = array();
  117. if ( $count and $type and $filler )
  118. {
  119. $tmpCount = 0;
  120. $values[] = $parameters[0];
  121. if ( $count < 0 )
  122. {
  123. //$code = ( $tpl->error( "indent", "Count parameter can not be negative" );
  124. $code = ( "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  125. "%output% = %1%;\n" );
  126. }
  127. else
  128. {
  129. $indentation = str_repeat( $filler, $count );
  130. $code = ( "%output% = '$indentation' . str_replace( '\n', '\n$indentation', %1% );\n" );
  131. }
  132. }
  133. else if ( $filler and $type )
  134. {
  135. $tmpCount = 1;
  136. $values[] = $parameters[0];
  137. $values[] = $parameters[1];
  138. $code = ( "if ( %2% < 0 )\n{" .
  139. "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  140. "%output% = %1%;\n" .
  141. "}else{\n" .
  142. "%tmp1% = str_repeat( '$filler', %2% );\n" .
  143. "%output% = %tmp1% . str_replace( '\n', '\n' . %tmp1%, %1% );\n" .
  144. "}\n");
  145. }
  146. else
  147. {
  148. $tmpCount = 2;
  149. $code = ( "if ( %2% < 0 ){\n" .
  150. "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  151. "%output% = %1%;\n" .
  152. "}else{" .
  153. "if ( %3% == 'tab' )\n{\n\t%tmp1% = \"\\t\";\n}\nelse " .
  154. "if ( %3% == 'space' )\n{\n\t%tmp1% = ' ';\n}\nelse\n" );
  155. if ( count ( $parameters ) == 4 )
  156. {
  157. $code .= "{\n\t%tmp1% = %4%;\n}\n";
  158. }
  159. else
  160. {
  161. $code.= "{\n\t%tmp1% = ' ';\n}\n";
  162. }
  163. $code .= ( "%tmp2% = str_repeat( %tmp1%, %2% );\n" .
  164. "%output% = %tmp2% . str_replace( '\n', '\n' . %tmp2%, %1% );\n" );
  165. $code .= "}\n";
  166. foreach ( $parameters as $parameter )
  167. {
  168. $values[] = $parameter;
  169. }
  170. }
  171. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, 'false', $tmpCount );
  172. return $newElements;
  173. }
  174. function concatTransformation( $operatorName, &$node, $tpl, &$resourceData,
  175. $element, $lastElement, $elementList, $elementTree, &$parameters )
  176. {
  177. $values = array();
  178. $function = $operatorName;
  179. if ( ( count( $parameters ) < 1 ) )
  180. {
  181. return false;
  182. }
  183. if ( ( count( $parameters ) == 1 ) and
  184. eZTemplateNodeTool::isConstantElement( $parameters[0] ) )
  185. {
  186. return array( eZTemplateNodeTool::createConstantElement( eZTemplateNodeTool::elementConstantValue( $parameters[0] ) ) );
  187. }
  188. $newElements = array();
  189. $counter = 1;
  190. $code = "%output% = ( ";
  191. foreach ( $parameters as $parameter )
  192. {
  193. $values[] = $parameter;
  194. if ( $counter > 1 )
  195. {
  196. $code .= ' . ';
  197. }
  198. $code .= "%$counter%";
  199. $counter++;
  200. }
  201. $code .= " );\n";
  202. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  203. return $newElements;
  204. }
  205. /*!
  206. Handles concat and indent operators.
  207. */
  208. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
  209. $placement )
  210. {
  211. switch ( $operatorName )
  212. {
  213. case $this->ConcatName:
  214. {
  215. $operands = array();
  216. if ( $operatorValue !== null )
  217. $operands[] = $operatorValue;
  218. for ( $i = 0; $i < count( $operatorParameters ); ++$i )
  219. {
  220. $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  221. if ( !is_object( $operand ) )
  222. $operands[] = $operand;
  223. }
  224. $operatorValue = implode( '', $operands );
  225. } break;
  226. case $this->IndentName:
  227. {
  228. if( $namedParameters['indent_count'] < 0 )
  229. {
  230. eZDebug::writeError( 'The value of the "count" argument is negative, indent() will not be called' );
  231. break;
  232. }
  233. $indentCount = $namedParameters['indent_count'];
  234. $indentType = $namedParameters['indent_type'];
  235. $filler = false;
  236. switch ( $indentType )
  237. {
  238. case 'space':
  239. default:
  240. {
  241. $filler = ' ';
  242. } break;
  243. case 'tab':
  244. {
  245. $filler = "\t";
  246. } break;
  247. case 'custom':
  248. {
  249. $filler = $namedParameters['indent_filler'];
  250. } break;
  251. }
  252. $fillText = str_repeat( $filler, $indentCount );
  253. $operatorValue = $fillText . str_replace( "\n", "\n" . $fillText, $operatorValue );
  254. } break;
  255. }
  256. }
  257. /// \privatesection
  258. public $ConcatName;
  259. public $Operators;
  260. }
  261. ?>