mirror of
https://github.com/google/blockly.git
synced 2026-01-06 00:20:37 +01:00
refactor(generators): Introduce PhpGenerator class, Order enum (#7162)
* refactor(generators): Introduce PhpGenerator class, Order enum * refactor(generators): Use Order.* instead of .ORDER_* * refactor(generators): Don't rename phpGenerator
This commit is contained in:
committed by
GitHub
parent
2f89c0dd09
commit
eeb89194c4
@@ -12,151 +12,154 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.PHP.texts');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {phpGenerator as PHP} from '../php.js';
|
||||
import {phpGenerator, Order} from '../php.js';
|
||||
|
||||
|
||||
PHP.forBlock['text'] = function(block) {
|
||||
phpGenerator.forBlock['text'] = function(block) {
|
||||
// Text value.
|
||||
const code = PHP.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, PHP.ORDER_ATOMIC];
|
||||
const code = phpGenerator.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_multiline'] = function(block) {
|
||||
phpGenerator.forBlock['text_multiline'] = function(block) {
|
||||
// Text value.
|
||||
const code = PHP.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
const code = phpGenerator.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
const order =
|
||||
code.indexOf('.') !== -1 ? PHP.ORDER_STRING_CONCAT : PHP.ORDER_ATOMIC;
|
||||
code.indexOf('.') !== -1 ? Order.STRING_CONCAT : Order.ATOMIC;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_join'] = function(block) {
|
||||
phpGenerator.forBlock['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
if (block.itemCount_ === 0) {
|
||||
return ["''", PHP.ORDER_ATOMIC];
|
||||
return ["''", Order.ATOMIC];
|
||||
} else if (block.itemCount_ === 1) {
|
||||
const element = PHP.valueToCode(block, 'ADD0', PHP.ORDER_NONE) || "''";
|
||||
const element = phpGenerator.valueToCode(block, 'ADD0', Order.NONE) || "''";
|
||||
const code = element;
|
||||
return [code, PHP.ORDER_NONE];
|
||||
return [code, Order.NONE];
|
||||
} else if (block.itemCount_ === 2) {
|
||||
const element0 =
|
||||
PHP.valueToCode(block, 'ADD0', PHP.ORDER_STRING_CONCAT) || "''";
|
||||
phpGenerator.valueToCode(block, 'ADD0', Order.STRING_CONCAT) || "''";
|
||||
const element1 =
|
||||
PHP.valueToCode(block, 'ADD1', PHP.ORDER_STRING_CONCAT) || "''";
|
||||
phpGenerator.valueToCode(block, 'ADD1', Order.STRING_CONCAT) || "''";
|
||||
const code = element0 + ' . ' + element1;
|
||||
return [code, PHP.ORDER_STRING_CONCAT];
|
||||
return [code, Order.STRING_CONCAT];
|
||||
} else {
|
||||
const elements = new Array(block.itemCount_);
|
||||
for (let i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = PHP.valueToCode(block, 'ADD' + i, PHP.ORDER_NONE) || "''";
|
||||
elements[i] =
|
||||
phpGenerator.valueToCode(block, 'ADD' + i, Order.NONE) || "''";
|
||||
}
|
||||
const code = 'implode(\'\', array(' + elements.join(',') + '))';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
};
|
||||
|
||||
PHP.forBlock['text_append'] = function(block) {
|
||||
phpGenerator.forBlock['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
const varName =
|
||||
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||
const value = PHP.valueToCode(block, 'TEXT', PHP.ORDER_ASSIGNMENT) || "''";
|
||||
phpGenerator.nameDB_.getName(
|
||||
block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||
const value =
|
||||
phpGenerator.valueToCode(block, 'TEXT', Order.ASSIGNMENT) || "''";
|
||||
return varName + ' .= ' + value + ';\n';
|
||||
};
|
||||
|
||||
PHP.forBlock['text_length'] = function(block) {
|
||||
phpGenerator.forBlock['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
const functionName = PHP.provideFunction_('length', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
|
||||
const functionName = phpGenerator.provideFunction_('length', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($value) {
|
||||
if (is_string($value)) {
|
||||
return strlen($value);
|
||||
}
|
||||
return count($value);
|
||||
}
|
||||
`);
|
||||
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
|
||||
return [functionName + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
|
||||
const text = phpGenerator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
||||
return [functionName + '(' + text + ')', Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_isEmpty'] = function(block) {
|
||||
phpGenerator.forBlock['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
|
||||
return ['empty(' + text + ')', PHP.ORDER_FUNCTION_CALL];
|
||||
const text = phpGenerator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
||||
return ['empty(' + text + ')', Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_indexOf'] = function(block) {
|
||||
phpGenerator.forBlock['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
const operator =
|
||||
block.getFieldValue('END') === 'FIRST' ? 'strpos' : 'strrpos';
|
||||
const substring = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || "''";
|
||||
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
|
||||
const substring = phpGenerator.valueToCode(block, 'FIND', Order.NONE) || "''";
|
||||
const text = phpGenerator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
||||
let errorIndex = ' -1';
|
||||
let indexAdjustment = '';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
errorIndex = ' 0';
|
||||
indexAdjustment = ' + 1';
|
||||
}
|
||||
const functionName = PHP.provideFunction_(
|
||||
const functionName = phpGenerator.provideFunction_(
|
||||
block.getFieldValue('END') === 'FIRST' ? 'text_indexOf' :
|
||||
'text_lastIndexOf',
|
||||
`
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $search) {
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($text, $search) {
|
||||
$pos = ${operator}($text, $search);
|
||||
return $pos === false ? ${errorIndex} : $pos${indexAdjustment};
|
||||
}
|
||||
`);
|
||||
const code = functionName + '(' + text + ', ' + substring + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_charAt'] = function(block) {
|
||||
phpGenerator.forBlock['text_charAt'] = function(block) {
|
||||
// Get letter at index.
|
||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
const textOrder = (where === 'RANDOM') ? PHP.ORDER_NONE : PHP.ORDER_NONE;
|
||||
const text = PHP.valueToCode(block, 'VALUE', textOrder) || "''";
|
||||
const textOrder = (where === 'RANDOM') ? Order.NONE : Order.NONE;
|
||||
const text = phpGenerator.valueToCode(block, 'VALUE', textOrder) || "''";
|
||||
switch (where) {
|
||||
case 'FIRST': {
|
||||
const code = 'substr(' + text + ', 0, 1)';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
case 'LAST': {
|
||||
const code = 'substr(' + text + ', -1)';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
case 'FROM_START': {
|
||||
const at = PHP.getAdjusted(block, 'AT');
|
||||
const at = phpGenerator.getAdjusted(block, 'AT');
|
||||
const code = 'substr(' + text + ', ' + at + ', 1)';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
case 'FROM_END': {
|
||||
const at = PHP.getAdjusted(block, 'AT', 1, true);
|
||||
const at = phpGenerator.getAdjusted(block, 'AT', 1, true);
|
||||
const code = 'substr(' + text + ', ' + at + ', 1)';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
case 'RANDOM': {
|
||||
const functionName = PHP.provideFunction_('text_random_letter', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text) {
|
||||
const functionName = phpGenerator.provideFunction_('text_random_letter', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($text) {
|
||||
return $text[rand(0, strlen($text) - 1)];
|
||||
}
|
||||
`);
|
||||
const code = functionName + '(' + text + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
}
|
||||
throw Error('Unhandled option (text_charAt).');
|
||||
};
|
||||
|
||||
PHP.forBlock['text_getSubstring'] = function(block) {
|
||||
phpGenerator.forBlock['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
const where1 = block.getFieldValue('WHERE1');
|
||||
const where2 = block.getFieldValue('WHERE2');
|
||||
const text = PHP.valueToCode(block, 'STRING', PHP.ORDER_NONE) || "''";
|
||||
const text = phpGenerator.valueToCode(block, 'STRING', Order.NONE) || "''";
|
||||
if (where1 === 'FIRST' && where2 === 'LAST') {
|
||||
const code = text;
|
||||
return [code, PHP.ORDER_NONE];
|
||||
return [code, Order.NONE];
|
||||
} else {
|
||||
const at1 = PHP.getAdjusted(block, 'AT1');
|
||||
const at2 = PHP.getAdjusted(block, 'AT2');
|
||||
const functionName = PHP.provideFunction_('text_get_substring', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $where1, $at1, $where2, $at2) {
|
||||
const at1 = phpGenerator.getAdjusted(block, 'AT1');
|
||||
const at2 = phpGenerator.getAdjusted(block, 'AT2');
|
||||
const functionName = phpGenerator.provideFunction_('text_get_substring', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($text, $where1, $at1, $where2, $at2) {
|
||||
if ($where1 == 'FROM_END') {
|
||||
$at1 = strlen($text) - 1 - $at1;
|
||||
} else if ($where1 == 'FIRST') {
|
||||
@@ -179,13 +182,13 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $where1, $at1, $where2, $at2)
|
||||
`);
|
||||
const code = functionName + '(' + text + ', \'' + where1 + '\', ' + at1 +
|
||||
', \'' + where2 + '\', ' + at2 + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
};
|
||||
|
||||
PHP.forBlock['text_changeCase'] = function(block) {
|
||||
phpGenerator.forBlock['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
const text = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
let code;
|
||||
if (block.getFieldValue('CASE') === 'UPPERCASE') {
|
||||
code = 'strtoupper(' + text + ')';
|
||||
@@ -194,62 +197,62 @@ PHP.forBlock['text_changeCase'] = function(block) {
|
||||
} else if (block.getFieldValue('CASE') === 'TITLECASE') {
|
||||
code = 'ucwords(strtolower(' + text + '))';
|
||||
}
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_trim'] = function(block) {
|
||||
phpGenerator.forBlock['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
const OPERATORS = {'LEFT': 'ltrim', 'RIGHT': 'rtrim', 'BOTH': 'trim'};
|
||||
const operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
return [operator + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
|
||||
const text = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
return [operator + '(' + text + ')', Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_print'] = function(block) {
|
||||
phpGenerator.forBlock['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
const msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
const msg = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
return 'print(' + msg + ');\n';
|
||||
};
|
||||
|
||||
PHP.forBlock['text_prompt_ext'] = function(block) {
|
||||
phpGenerator.forBlock['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
let msg;
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
msg = PHP.quote_(block.getFieldValue('TEXT'));
|
||||
msg = phpGenerator.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
msg = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
}
|
||||
let code = 'readline(' + msg + ')';
|
||||
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'floatval(' + code + ')';
|
||||
}
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_prompt'] = PHP.forBlock['text_prompt_ext'];
|
||||
phpGenerator.forBlock['text_prompt'] = phpGenerator.forBlock['text_prompt_ext'];
|
||||
|
||||
PHP.forBlock['text_count'] = function(block) {
|
||||
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
const sub = PHP.valueToCode(block, 'SUB', PHP.ORDER_NONE) || "''";
|
||||
phpGenerator.forBlock['text_count'] = function(block) {
|
||||
const text = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
const sub = phpGenerator.valueToCode(block, 'SUB', Order.NONE) || "''";
|
||||
const code = 'strlen(' + sub + ') === 0' +
|
||||
' ? strlen(' + text + ') + 1' +
|
||||
' : substr_count(' + text + ', ' + sub + ')';
|
||||
return [code, PHP.ORDER_CONDITIONAL];
|
||||
return [code, Order.CONDITIONAL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_replace'] = function(block) {
|
||||
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
const from = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || "''";
|
||||
const to = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || "''";
|
||||
phpGenerator.forBlock['text_replace'] = function(block) {
|
||||
const text = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
const from = phpGenerator.valueToCode(block, 'FROM', Order.NONE) || "''";
|
||||
const to = phpGenerator.valueToCode(block, 'TO', Order.NONE) || "''";
|
||||
const code = 'str_replace(' + from + ', ' + to + ', ' + text + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['text_reverse'] = function(block) {
|
||||
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
|
||||
phpGenerator.forBlock['text_reverse'] = function(block) {
|
||||
const text = phpGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
const code = 'strrev(' + text + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user