text.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2015 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating PHP for text blocks.
  22. * @author daarond@gmail.com (Daaron Dwyer)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.PHP.texts');
  26. goog.require('Blockly.PHP');
  27. Blockly.PHP['text'] = function(block) {
  28. // Text value.
  29. var code = Blockly.PHP.quote_(block.getFieldValue('TEXT'));
  30. return [code, Blockly.PHP.ORDER_ATOMIC];
  31. };
  32. Blockly.PHP['text_join'] = function(block) {
  33. // Create a string made up of any number of elements of any type.
  34. var code;
  35. if (block.itemCount_ == 0) {
  36. return ['\'\'', Blockly.PHP.ORDER_ATOMIC];
  37. } else if (block.itemCount_ == 1) {
  38. var argument0 = Blockly.PHP.valueToCode(block, 'ADD0',
  39. Blockly.PHP.ORDER_NONE) || '\'\'';
  40. code = argument0;
  41. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  42. } else if (block.itemCount_ == 2) {
  43. var argument0 = Blockly.PHP.valueToCode(block, 'ADD0',
  44. Blockly.PHP.ORDER_NONE) || '\'\'';
  45. var argument1 = Blockly.PHP.valueToCode(block, 'ADD1',
  46. Blockly.PHP.ORDER_NONE) || '\'\'';
  47. code = argument0 + ' . ' + argument1;
  48. return [code, Blockly.PHP.ORDER_ADDITION];
  49. } else {
  50. code = new Array(block.itemCount_);
  51. for (var n = 0; n < block.itemCount_; n++) {
  52. code[n] = Blockly.PHP.valueToCode(block, 'ADD' + n,
  53. Blockly.PHP.ORDER_COMMA) || '\'\'';
  54. }
  55. code = 'implode(\'\', array(' + code.join(',') + '))';
  56. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  57. }
  58. };
  59. Blockly.PHP['text_append'] = function(block) {
  60. // Append to a variable in place.
  61. var varName = Blockly.PHP.variableDB_.getName(
  62. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  63. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  64. Blockly.PHP.ORDER_NONE) || '\'\'';
  65. return varName + ' .= ' + argument0 + ';\n';
  66. };
  67. Blockly.PHP['text_length'] = function(block) {
  68. // String or array length.
  69. var functionName = Blockly.PHP.provideFunction_(
  70. 'length',
  71. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
  72. ' if (is_string($value)) {',
  73. ' return strlen($value);',
  74. ' } else {',
  75. ' return count($value);',
  76. ' }',
  77. '}']);
  78. var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
  79. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  80. return [functionName + '(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  81. };
  82. Blockly.PHP['text_isEmpty'] = function(block) {
  83. // Is the string null or array empty?
  84. var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
  85. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  86. return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  87. };
  88. Blockly.PHP['text_indexOf'] = function(block) {
  89. // Search the text for a substring.
  90. var operator = block.getFieldValue('END') == 'FIRST' ?
  91. 'strpos' : 'strrpos';
  92. var argument0 = Blockly.PHP.valueToCode(block, 'FIND',
  93. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  94. var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
  95. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  96. var code = operator + '(' + argument1 + ', ' + argument0 + ') + 1';
  97. var functionName = Blockly.PHP.provideFunction_(
  98. block.getFieldValue('END') == 'FIRST' ?
  99. 'text_indexOf' : 'text_lastIndexOf',
  100. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  101. '($text, $search) {',
  102. ' $pos = ' + operator + '($text, $search);',
  103. ' return $pos === false ? 0 : $pos + 1;',
  104. '}']);
  105. code = functionName + '(' + argument1 + ', ' + argument0 + ')';
  106. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  107. };
  108. Blockly.PHP['text_charAt'] = function(block) {
  109. // Get letter at index.
  110. var where = block.getFieldValue('WHERE') || 'FROM_START';
  111. var at = Blockly.PHP.valueToCode(block, 'AT',
  112. Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
  113. var text = Blockly.PHP.valueToCode(block, 'VALUE',
  114. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  115. switch (where) {
  116. case 'FIRST':
  117. var code = text + '[0]';
  118. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  119. case 'LAST':
  120. var code = 'substr(' + text + ', -1, 1)';
  121. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  122. case 'FROM_START':
  123. // Blockly uses one-based indicies.
  124. if (Blockly.isNumber(at)) {
  125. // If the index is a naked number, decrement it right now.
  126. at = parseFloat(at) - 1;
  127. } else {
  128. // If the index is dynamic, decrement it in code.
  129. at += ' - 1';
  130. }
  131. var code = 'substr(' + text + ', ' + at + ', 1)';
  132. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  133. case 'FROM_END':
  134. var code = 'substr(' + text + ', -' + at + ', 1)';
  135. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  136. case 'RANDOM':
  137. var functionName = Blockly.PHP.provideFunction_(
  138. 'text_random_letter',
  139. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {',
  140. ' return $text[rand(0, strlen($text) - 1)];',
  141. '}']);
  142. code = functionName + '(' + text + ')';
  143. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  144. }
  145. throw 'Unhandled option (text_charAt).';
  146. };
  147. Blockly.PHP['text_getSubstring'] = function(block) {
  148. // Get substring.
  149. var text = Blockly.PHP.valueToCode(block, 'STRING',
  150. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  151. var where1 = block.getFieldValue('WHERE1');
  152. var where2 = block.getFieldValue('WHERE2');
  153. var at1 = Blockly.PHP.valueToCode(block, 'AT1',
  154. Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
  155. var at2 = Blockly.PHP.valueToCode(block, 'AT2',
  156. Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
  157. if (where1 == 'FIRST' && where2 == 'LAST') {
  158. var code = text;
  159. } else {
  160. var functionName = Blockly.PHP.provideFunction_(
  161. 'text_get_substring',
  162. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  163. '($text, $where1, $at1, $where2, $at2) {',
  164. ' if ($where2 == \'FROM_START\') {',
  165. ' $at2--;',
  166. ' } else if ($where2 == \'FROM_END\') {',
  167. ' $at2 = $at2 - $at1;',
  168. ' } else if ($where2 == \'FIRST\') {',
  169. ' $at2 = 0;',
  170. ' } else if ($where2 == \'LAST\') {',
  171. ' $at2 = strlen($text);',
  172. ' } else { $at2 = 0; }',
  173. ' if ($where1 == \'FROM_START\') {',
  174. ' $at1--;',
  175. ' } else if ($where1 == \'FROM_END\') {',
  176. ' $at1 = strlen($text) - $at1;',
  177. ' } else if ($where1 == \'FIRST\') {',
  178. ' $at1 = 0;',
  179. ' } else if ($where1 == \'LAST\') {',
  180. ' $at1 = strlen($text) - 1;',
  181. ' } else { $at1 = 0; }',
  182. ' return substr($text, $at1, $at2);',
  183. '}']);
  184. var code = functionName + '(' + text + ', \'' +
  185. where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
  186. }
  187. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  188. };
  189. Blockly.PHP['text_changeCase'] = function(block) {
  190. // Change capitalization.
  191. var code;
  192. if (block.getFieldValue('CASE') == 'UPPERCASE') {
  193. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  194. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  195. code = 'strtoupper(' + argument0 + ')';
  196. } else if (block.getFieldValue('CASE') == 'LOWERCASE') {
  197. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  198. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  199. code = 'strtolower(' + argument0 + ')';
  200. } else if (block.getFieldValue('CASE') == 'TITLECASE') {
  201. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  202. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  203. code = 'ucwords(strtolower(' + argument0 + '))';
  204. }
  205. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  206. };
  207. Blockly.PHP['text_trim'] = function(block) {
  208. // Trim spaces.
  209. var OPERATORS = {
  210. 'LEFT': 'ltrim',
  211. 'RIGHT': 'rtrim',
  212. 'BOTH': 'trim'
  213. };
  214. var operator = OPERATORS[block.getFieldValue('MODE')];
  215. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  216. Blockly.PHP.ORDER_NONE) || '\'\'';
  217. return [ operator + '(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  218. };
  219. Blockly.PHP['text_print'] = function(block) {
  220. // Print statement.
  221. var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
  222. Blockly.PHP.ORDER_NONE) || '\'\'';
  223. return 'print(' + argument0 + ');\n';
  224. };
  225. Blockly.PHP['text_prompt_ext'] = function(block) {
  226. // Prompt function.
  227. if (block.getField('TEXT')) {
  228. // Internal message.
  229. var msg = Blockly.PHP.quote_(block.getFieldValue('TEXT'));
  230. } else {
  231. // External message.
  232. var msg = Blockly.PHP.valueToCode(block, 'TEXT',
  233. Blockly.PHP.ORDER_NONE) || '\'\'';
  234. }
  235. var code = 'readline(' + msg + ')';
  236. var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
  237. if (toNumber) {
  238. code = 'floatval(' + code + ')';
  239. }
  240. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  241. };
  242. Blockly.PHP['text_prompt'] = Blockly.PHP['text_prompt_ext'];