/protected/components/ezcomponents/Template/src/functions/string_functions.php

https://github.com/kamarulismail/kamarul-playground · PHP · 395 lines · 129 code · 51 blank · 215 comment · 1 complexity · 57d26e481b4ba820021181e8e5ac776a MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcTemplateStringFunctions class
  4. *
  5. * @package Template
  6. * @version 1.4.2
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * @package Template
  13. * @version 1.4.2
  14. * @access private
  15. */
  16. class ezcTemplateStringFunctions extends ezcTemplateFunctions
  17. {
  18. /**
  19. * Translates a function used in the Template language to a PHP function call.
  20. * The function call is represented by an array with three elements:
  21. *
  22. * 1. The return typehint. Is it an array, a non-array, or both.
  23. * 2. The parameter input definition.
  24. * 3. The AST nodes.
  25. *
  26. * @param string $functionName
  27. * @param array(ezcTemplateAstNode) $parameters
  28. * @return array(mixed)
  29. */
  30. public static function getFunctionSubstitution( $functionName, $parameters )
  31. {
  32. switch ( $functionName )
  33. {
  34. // str_replace( $sl, $index, $len, $sr )
  35. // substr( $sl, 0, $index ) . $sr . substr( $sl, $index + $len );
  36. case "str_replace":
  37. return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%index", "%length", "%right" ),
  38. self::concat(
  39. self::functionCall( "substr", array( "%left", self::value( 0 ), "%index" ) ),
  40. self::concat(
  41. "%right",
  42. self::functionCall(
  43. "substr",
  44. array( "%left", array( "ezcTemplateAdditionOperatorAstNode", array( "%index", "%length" ) ) )
  45. )
  46. )
  47. )
  48. );
  49. // str_remove( $s, $index, $len )
  50. // substr( $s, 0, $index ) . substr( $s, $index + $len );
  51. case "str_remove":
  52. return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%index", "%length" ),
  53. self::concat(
  54. self::functionCall( "substr", array( "%string", self::value( 0 ), "%index" ) ),
  55. self::functionCall( "substr", array( "%string", array( "ezcTemplateAdditionOperatorAstNode", array( "%index", "%length" ) ) )
  56. )
  57. )
  58. );
  59. // string str_chop( $s, $len ) ( QString::chop ):
  60. // substr( $s, 0, strlen( $string ) - $len );
  61. case "str_chop":
  62. return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length" ),
  63. self::functionCall( "substr", array(
  64. "%string",
  65. self::value( 0 ),
  66. array( "ezcTemplateSubtractionOperatorAstNode", array( self::functionCall( "strlen", array( "%string" ) ), "%length" ) )
  67. )
  68. ) );
  69. // string str_chop_front( $s, $len )
  70. // substr( $s, $len );
  71. case "str_chop_front": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length" ), self::functionCall( "substr", array( "%string", "%length" ) ) );
  72. case "str_append": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%right" ), self::concat( "%left", "%right" ) );
  73. case "str_prepend": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%right"), self::concat( "%right", "%left" ) );
  74. // str_compare( $sl, $sr )
  75. // strcmp( $sl, $sr );
  76. case "str_compare": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%right"), self::functionCall( "strcmp", array( "%left", "%right" ) ) );
  77. // str_nat_compare( $sl, $sr )
  78. // strnatcmp( $sl, $sr );
  79. case "str_nat_compare": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%right"), self::functionCall( "strnatcmp", array( "%left", "%right" ) ) );
  80. // str_contains( $sl, $sr ) ( QString::compare )::
  81. // strpos( $sl, $sr ) !== false
  82. case "str_contains": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%left", "%right" ),
  83. array( "ezcTemplateNotIdenticalOperatorAstNode",
  84. array( self::functionCall( "strpos", array( "%left", "%right" ) ), self::value( false ) ) ) );
  85. // str_len( $s )
  86. // strlen( $s )
  87. case "str_len": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ), self::functionCall( "strlen", array( "%string" ) ) );
  88. // str_left( $s, $len )
  89. // substr( $s, 0, $len )
  90. case "str_left": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length"), self::functionCall( "substr", array( "%string", self::value( 0 ), "%length" ) ) );
  91. // str_starts_with( $sl, $sr )
  92. // strpos( $sl, $sr ) === 0
  93. case "str_starts_with": return array(
  94. ezcTemplateAstNode::TYPE_VALUE,
  95. array( "%haystack", "%needle" ),
  96. array( "ezcTemplateIdenticalOperatorAstNode", array(
  97. self::functionCall( "strpos", array( "%haystack", "%needle" ) ),
  98. self::value( 0 )
  99. ) ) );
  100. // str_right( $s, $len )
  101. // substr( $s, -$len )
  102. case "str_right": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length" ),
  103. self::functionCall( "substr", array( "%string", array( "ezcTemplateArithmeticNegationOperatorAstNode", array( "%length" ) ) ) ) );
  104. // str_ends_with( $sl, $sr )
  105. // strrpos( $sl, $sr ) === ( strlen( $sl ) - strlen( $sr) )
  106. case "str_ends_with": return array(
  107. ezcTemplateAstNode::TYPE_VALUE,
  108. array( "%haystack", "%needle" ),
  109. array( "ezcTemplateIdenticalOperatorAstNode", array(
  110. self::functionCall( "strrpos", array( "%haystack", "%needle" ) ),
  111. array( "ezcTemplateSubtractionOperatorAstNode", array(
  112. self::functionCall( "strlen", array( "%haystack" ) ),
  113. self::functionCall( "strlen", array( "%needle" ) )
  114. ) ) ) ) );
  115. // str_mid( $s, $index, $len )
  116. // substr( $s, $index, $len )
  117. case "str_mid": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%index", "%length" ),
  118. self::functionCall( "substr", array( "%string", "%index", "%length" ) ) );
  119. // str_at( $s, $index )
  120. // substr( $s, $index, 1 )
  121. case "str_at": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%index" ),
  122. self::functionCall( "substr", array( "%string", "%index", self::value( 1 ) ) ) );
  123. // str_fill( $s, $len )
  124. // str_repeat( $s, $len )
  125. case "str_fill": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length" ),
  126. self::functionCall( "str_repeat", array( "%string", "%length" ) ) );
  127. // str_index_of( $sl, $sr [, $index ] )
  128. // strpos( $sl, $sr [, $index ] )
  129. case "str_index_of": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%haystack", "%needle", "[%index]" ),
  130. self::functionCall( "strpos", array( "%haystack", "%needle", "[%index]" ) ) );
  131. // str_last_index( $sl, $sr [, $index] )
  132. // strrpos( $sl, $sr [, $index ] )
  133. case "str_last_index": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%haystack", "%needle", "[%index]" ),
  134. self::functionCall( "strrpos", array( "%haystack", "%needle", "[%index]" ) ) );
  135. // str_is_empty( $s )
  136. // strlen( $s ) === 0
  137. case "str_is_empty": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  138. array( "ezcTemplateIdenticalOperatorAstNode", array(
  139. self::functionCall( "strlen", array( "%string" ) ),
  140. self::value( 0 ) ) ) );
  141. // str_pad_left( $s, $len, $fill )
  142. // str_pad( $s, $len, $fill, STR_PAD_LEFT )
  143. case "str_pad_left": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length", "%fill" ),
  144. self::functionCall( "str_pad", array( "%string", "%length", "%fill", self::constant( "STR_PAD_LEFT" ) ) ) );
  145. // str_pad_right( $s, $len, $fill ) ( QString::rightJustified() )::
  146. // str_pad( $s, $len, $fill, STR_PAD_RIGHT )
  147. case "str_pad_right": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%length", "%fill" ),
  148. self::functionCall( "str_pad", array( "%string", "%length", "%fill", self::constant( "STR_PAD_RIGHT" ) ) ) );
  149. // str_number( $num, $decimals, $point, $sep )
  150. // number_format( $num, $decimals, $point, $sep )
  151. case "str_number": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%number", "%decimals", "%point", "%separator" ),
  152. self::functionCall( "number_format", array( "%number", "%decimals", "%point", "%separator") ) );
  153. // str_trim( $s [, $chars ] )
  154. // trim( $s [, $chars] )
  155. case "str_trim": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "[%chars]" ),
  156. self::functionCall( "trim", array( "%string", "[%chars]") ) );
  157. // str_trim_left( $s [, $chars] )
  158. // ltrim( $s [, $chars] )
  159. case "str_trim_left": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "[%chars]" ),
  160. self::functionCall( "ltrim", array( "%string", "[%chars]") ) );
  161. // str_trim_right( $s [, $chars] )
  162. // rtrim( $s, [$chars] )
  163. case "str_trim_right": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "[%chars]" ),
  164. self::functionCall( "rtrim", array( "%string", "[%chars]") ) );
  165. // str_simplified( $s )
  166. // trim( preg_replace( "/(\n|\t|\r\n|\s)+/", " ", $s ) )
  167. case "str_simplify": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  168. self::functionCall( "trim", array(
  169. self::functionCall( "preg_replace", array( self::constant( '"/(\n|\t|\r\n|\s)+/"' ), self::value( " " ), "%string" ) )
  170. ) ) );
  171. // str_split( $s, $sep[, $max] )
  172. // explode( $s, $sep, $max )
  173. case "str_split": return array( ezcTemplateAstNode::TYPE_VALUE | ezcTemplateAstNode::TYPE_ARRAY, array( "%string", "%separator", "[%max]" ),
  174. self::functionCall( "explode", array( "%separator", "%string", "[%max]" ) ) );
  175. // str_join( $s_list, $sep )
  176. // join( $sList, $sep )
  177. case "str_join": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%list", "%separator" ),
  178. self::functionCall( "join", array( "%separator", "%list" ) ) );
  179. // str_printf( $format [...] )
  180. // sprintf( $format [...] )
  181. // TODO
  182. // str_chr( $ord1 [, $ord2...] )::
  183. // ord( $ord1 ) [ . ord( $ord2 ) ...]
  184. // TODO
  185. // str_ord( $c )
  186. // ord( $c )
  187. case "str_ord": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%char" ),
  188. self::functionCall( "ord", array( "%char" ) ) );
  189. // chr( $c )
  190. case "str_chr": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%char" ),
  191. self::functionCall( "chr", array( "%char" ) ) );
  192. // str_ord_list( $s )::
  193. // chr( $s[0] ) [ . chr( $s[1] ) ]
  194. // TODO
  195. // str_upper( $s )
  196. // strtoupper( $s )
  197. case "str_upper": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  198. self::functionCall( "strtoupper", array( "%string") ) );
  199. // str_lower( $s )
  200. // strtolower( $s )
  201. case "str_lower": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  202. self::functionCall( "strtolower", array( "%string") ) );
  203. // str_capitalize( $s )::
  204. // ucfirst( $s )
  205. case "str_capitalize": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  206. self::functionCall( "ucfirst", array( "%string") ) );
  207. // str_find_replace( $s, $find, $replace, $count )::
  208. // str_replace( $s, $replace, $find, $count )
  209. case "str_find_replace": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%find", "%replace", "[%count]" ),
  210. self::functionCall( "str_replace", array( "%find", "%replace", "%string", "[%count]") ) );
  211. // str_reverse( $s )::
  212. // strrev( $s )
  213. case "str_reverse": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  214. self::functionCall( "strrev", array( "%string" ) ) );
  215. // str_section( $s, $sep, $start, $end = -1 )
  216. // join( array_slice( split( $s, $sep, $end != -1 ? $end, false ), $start, $end ? $end : false ) )
  217. // TODO
  218. // str_char_count( $s )::
  219. // strlen( $s )
  220. case "str_char_count": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  221. self::functionCall( "strlen", array( "%string" ) ) );
  222. // str_word_count( $s [, $wordsep] )
  223. // str_word_count( $s, 0 [, $wordsep] )
  224. case "str_word_count": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "[%wordsep]" ),
  225. self::functionCall( "str_word_count", array( "%string", self::value( 0 ), "[%wordsep]" ) ) );
  226. // - *string* str_paragraph_count( $s )::
  227. // Code.
  228. case "str_paragraph_count": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ),
  229. self::functionCall( "ezcTemplateString::str_paragraph_count", array( "%string" ) ) );
  230. //
  231. // - *string* str_sentence_count( $s )::
  232. //
  233. // $pos = 0;
  234. // $count = 0;
  235. // while ( preg_match( "/. /", $s, $m, PREG_OFFSET_CAPTURE, $pos )
  236. // {
  237. // ++$count;
  238. // $pos = $m[0][1];
  239. // }
  240. // TODO
  241. //
  242. // - *string* str_break( $s, $eol = contextaware, $lbreak = contextaware )::
  243. //
  244. // str_replace( context_eol_char( $eol ), context_linebreak_char( $eol ), $s )
  245. //
  246. // TODO
  247. //
  248. // - *string* str_break_chars( $s, $cbreak )::
  249. //
  250. // $sNew = '';
  251. // for ( $i = 0; $i < strlen( $s ) - 1; ++$i )
  252. // {
  253. // $sNew .= $s[$i] . $cbreak;
  254. // }
  255. // $sNew .= $s[strlen( $s ) - 1];
  256. //
  257. // TODO
  258. // str_wrap( $s, $width, $break, $cut )
  259. // wordwrap( $s, $width, $break, $cut )
  260. case "str_wrap": return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string", "%width", "%break", "[%cut]" ),
  261. self::functionCall( "wordwrap", array( "%string", "%width", "%break", "[%cut]" ) ) );
  262. // base64_encode( $s )
  263. case "str_base64_encode":
  264. return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ), self::functionCall( "base64_encode", array( "%string" ) ) );
  265. // base64_decode( $s )
  266. case "str_base64_decode":
  267. return array( ezcTemplateAstNode::TYPE_VALUE, array( "%string" ), self::functionCall( "base64_decode", array( "%string" ) ) );
  268. //
  269. // - *string* str_wrap_indent::
  270. //
  271. // $tmp = wordwrap( $s, $width, $break, $cut )
  272. // $lines = explode( "\n", $tmp );
  273. // $newLines = array();
  274. // foreach ( $lines as $line )
  275. // {
  276. // $newLines[] = $prefix . $line . $suffix;
  277. // }
  278. // return join( "\n", $newLines )
  279. //
  280. // TODO
  281. // - *string* str_block( $s, $prefix, $suffix )
  282. //
  283. //
  284. // - *string* str_shorten_right( $s, $max_size )
  285. //
  286. // - *string* str_shorten_mid( $s, $max_size )
  287. //
  288. // - *string* str_shorten_left( $s, $max_size )
  289. //
  290. // - *string* str_crc32( $s )::
  291. //
  292. // crc32( $s )
  293. //
  294. // - *string* str_md5( $s )::
  295. //
  296. // md5( $s )
  297. //
  298. // - *string* str_sha1( $s )::
  299. //
  300. // sha1( $s )
  301. //
  302. // - *string* str_rot13( $s )::
  303. //
  304. // str_rot13( $s )
  305. //
  306. // Some of the functions are also available as case insensitive versions, they are:
  307. //
  308. // - *string* stri_contains( $sl, $sr ) ( QString::compare )::
  309. //
  310. // stristr( $sl, $sr ) !== false
  311. //
  312. // - *string* stri_starts_with( $sl, $sr ) ( QString::startsWith )::
  313. //
  314. // stripos( strtolower( $sl ), strtolower( $sr ) ) === 0
  315. //
  316. // - *string* stri_ends_with( $sl, $sr ) ( QString::endsWith )::
  317. //
  318. // strripos( $sl, $sr ) === ( strlen( $sl ) - 1 )
  319. //
  320. // - *string* stri_index( $sl, $sr [, $from] ) ( QString::indexOf )::
  321. //
  322. // stripos( $sl, $sr [, $from ] )
  323. //
  324. // - *string* stri_last_index( $sl, $sr [, $from] ) ( QString::lastIndexOf )::
  325. //
  326. // strirpos( $sl, $sr [, $from ] )
  327. //
  328. // - *string* stri_find_replace( $s, $find, $replace, $count )::
  329. //
  330. // str_ireplace( $s, $replace, $find, $count )
  331. //
  332. // - *string* stri_compare( $sl, $sr ) ( QString::compare )::
  333. //
  334. // strcasecmp( $sl, $sr );
  335. //
  336. // - *string* stri_nat_compare( $sl, $sr )::
  337. //
  338. // strnatcasecmp( $sl, $sr );
  339. //
  340. }
  341. return null;
  342. }
  343. }
  344. ?>