text.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (block.itemCount_ == 0) {
  35. return ['\'\'', Blockly.PHP.ORDER_ATOMIC];
  36. } else if (block.itemCount_ == 1) {
  37. var element = Blockly.PHP.valueToCode(block, 'ADD0',
  38. Blockly.PHP.ORDER_NONE) || '\'\'';
  39. var code = element;
  40. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  41. } else if (block.itemCount_ == 2) {
  42. var element0 = Blockly.PHP.valueToCode(block, 'ADD0',
  43. Blockly.PHP.ORDER_NONE) || '\'\'';
  44. var element1 = Blockly.PHP.valueToCode(block, 'ADD1',
  45. Blockly.PHP.ORDER_NONE) || '\'\'';
  46. var code = element0 + ' . ' + element1;
  47. return [code, Blockly.PHP.ORDER_ADDITION];
  48. } else {
  49. var elements = new Array(block.itemCount_);
  50. for (var i = 0; i < block.itemCount_; i++) {
  51. elements[i] = Blockly.PHP.valueToCode(block, 'ADD' + i,
  52. Blockly.PHP.ORDER_COMMA) || '\'\'';
  53. }
  54. var code = 'implode(\'\', array(' + elements.join(',') + '))';
  55. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  56. }
  57. };
  58. Blockly.PHP['text_append'] = function(block) {
  59. // Append to a variable in place.
  60. var varName = Blockly.PHP.variableDB_.getName(
  61. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  62. var value = Blockly.PHP.valueToCode(block, 'TEXT',
  63. Blockly.PHP.ORDER_ASSIGNMENT) || '\'\'';
  64. return varName + ' .= ' + value + ';\n';
  65. };
  66. Blockly.PHP['text_length'] = function(block) {
  67. // String or array length.
  68. var functionName = Blockly.PHP.provideFunction_(
  69. 'length',
  70. ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
  71. ' if (is_string($value)) {',
  72. ' return strlen($value);',
  73. ' } else {',
  74. ' return count($value);',
  75. ' }',
  76. '}']);
  77. var text = Blockly.PHP.valueToCode(block, 'VALUE',
  78. Blockly.PHP.ORDER_NONE) || '\'\'';
  79. return [functionName + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  80. };
  81. Blockly.PHP['text_isEmpty'] = function(block) {
  82. // Is the string null or array empty?
  83. var text = Blockly.PHP.valueToCode(block, 'VALUE',
  84. Blockly.PHP.ORDER_NONE) || '\'\'';
  85. return ['empty(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  86. };
  87. Blockly.PHP['text_indexOf'] = function(block) {
  88. // Search the text for a substring.
  89. var operator = block.getFieldValue('END') == 'FIRST' ?
  90. 'strpos' : 'strrpos';
  91. var substring = Blockly.PHP.valueToCode(block, 'FIND',
  92. Blockly.PHP.ORDER_NONE) || '\'\'';
  93. var text = Blockly.PHP.valueToCode(block, 'VALUE',
  94. Blockly.PHP.ORDER_NONE) || '\'\'';
  95. if (Blockly.PHP.ONE_BASED_INDEXING) {
  96. var errorIndex = ' 0';
  97. var indexAdjustment = ' + 1';
  98. } else {
  99. var errorIndex = ' -1';
  100. var indexAdjustment = '';
  101. }
  102. var functionName = Blockly.PHP.provideFunction_(
  103. block.getFieldValue('END') == 'FIRST' ?
  104. 'text_indexOf' : 'text_lastIndexOf',
  105. ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  106. '($text, $search) {',
  107. ' $pos = ' + operator + '($text, $search);',
  108. ' return $pos === false ? ' + errorIndex + ' : $pos' +
  109. indexAdjustment + ';',
  110. '}']);
  111. var code = functionName + '(' + text + ', ' + substring + ')';
  112. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  113. };
  114. Blockly.PHP['text_charAt'] = function(block) {
  115. // Get letter at index.
  116. var where = block.getFieldValue('WHERE') || 'FROM_START';
  117. var textOrder = (where == 'RANDOM') ? Blockly.PHP.ORDER_NONE :
  118. Blockly.PHP.ORDER_COMMA;
  119. var text = Blockly.PHP.valueToCode(block, 'VALUE', textOrder) || '\'\'';
  120. switch (where) {
  121. case 'FIRST':
  122. var code = 'substr(' + text + ', 0, 1)';
  123. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  124. case 'LAST':
  125. var code = 'substr(' + text + ', -1)';
  126. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  127. case 'FROM_START':
  128. var at = Blockly.PHP.getAdjusted(block, 'AT');
  129. var code = 'substr(' + text + ', ' + at + ', 1)';
  130. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  131. case 'FROM_END':
  132. var at = Blockly.PHP.getAdjusted(block, 'AT', 1, true);
  133. var code = 'substr(' + text + ', ' + at + ', 1)';
  134. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  135. case 'RANDOM':
  136. var functionName = Blockly.PHP.provideFunction_(
  137. 'text_random_letter',
  138. ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {',
  139. ' return $text[rand(0, strlen($text) - 1)];',
  140. '}']);
  141. code = functionName + '(' + text + ')';
  142. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  143. }
  144. throw 'Unhandled option (text_charAt).';
  145. };
  146. Blockly.PHP['text_getSubstring'] = function(block) {
  147. // Get substring.
  148. var text = Blockly.PHP.valueToCode(block, 'STRING',
  149. Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
  150. var where1 = block.getFieldValue('WHERE1');
  151. var where2 = block.getFieldValue('WHERE2');
  152. if (where1 == 'FIRST' && where2 == 'LAST') {
  153. var code = text;
  154. } else {
  155. var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
  156. var at2 = Blockly.PHP.getAdjusted(block, 'AT2');
  157. var functionName = Blockly.PHP.provideFunction_(
  158. 'text_get_substring',
  159. ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  160. '($text, $where1, $at1, $where2, $at2) {',
  161. ' if ($where1 == \'FROM_END\') {',
  162. ' $at1 = strlen($text) - 1 - $at1;',
  163. ' } else if ($where1 == \'FIRST\') {',
  164. ' $at1 = 0;',
  165. ' } else if ($where1 != \'FROM_START\'){',
  166. ' throw new Exception(\'Unhandled option (text_get_substring).\');',
  167. ' }',
  168. ' $length = 0;',
  169. ' if ($where2 == \'FROM_START\') {',
  170. ' $length = $at2 - $at1 + 1;',
  171. ' } else if ($where2 == \'FROM_END\') {',
  172. ' $length = strlen($text) - $at1 - $at2;',
  173. ' } else if ($where2 == \'LAST\') {',
  174. ' $length = strlen($text) - $at1;',
  175. ' } else {',
  176. ' throw new Exception(\'Unhandled option (text_get_substring).\');',
  177. ' }',
  178. ' return substr($text, $at1, $length);',
  179. '}']);
  180. var code = functionName + '(' + text + ', \'' +
  181. where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
  182. }
  183. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  184. };
  185. Blockly.PHP['text_changeCase'] = function(block) {
  186. // Change capitalization.
  187. var text = Blockly.PHP.valueToCode(block, 'TEXT',
  188. Blockly.PHP.ORDER_NONE) || '\'\'';
  189. if (block.getFieldValue('CASE') == 'UPPERCASE') {
  190. var code = 'strtoupper(' + text + ')';
  191. } else if (block.getFieldValue('CASE') == 'LOWERCASE') {
  192. var code = 'strtolower(' + text + ')';
  193. } else if (block.getFieldValue('CASE') == 'TITLECASE') {
  194. var code = 'ucwords(strtolower(' + text + '))';
  195. }
  196. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  197. };
  198. Blockly.PHP['text_trim'] = function(block) {
  199. // Trim spaces.
  200. var OPERATORS = {
  201. 'LEFT': 'ltrim',
  202. 'RIGHT': 'rtrim',
  203. 'BOTH': 'trim'
  204. };
  205. var operator = OPERATORS[block.getFieldValue('MODE')];
  206. var text = Blockly.PHP.valueToCode(block, 'TEXT',
  207. Blockly.PHP.ORDER_NONE) || '\'\'';
  208. return [operator + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
  209. };
  210. Blockly.PHP['text_print'] = function(block) {
  211. // Print statement.
  212. var msg = Blockly.PHP.valueToCode(block, 'TEXT',
  213. Blockly.PHP.ORDER_NONE) || '\'\'';
  214. return 'print(' + msg + ');\n';
  215. };
  216. Blockly.PHP['text_prompt_ext'] = function(block) {
  217. // Prompt function.
  218. if (block.getField('TEXT')) {
  219. // Internal message.
  220. var msg = Blockly.PHP.quote_(block.getFieldValue('TEXT'));
  221. } else {
  222. // External message.
  223. var msg = Blockly.PHP.valueToCode(block, 'TEXT',
  224. Blockly.PHP.ORDER_NONE) || '\'\'';
  225. }
  226. var code = 'readline(' + msg + ')';
  227. var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
  228. if (toNumber) {
  229. code = 'floatval(' + code + ')';
  230. }
  231. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  232. };
  233. Blockly.PHP['text_prompt'] = Blockly.PHP['text_prompt_ext'];