Fix JS and PHP list/text blocks.

The length and is_empty blocks can take either text or lists.
This commit is contained in:
Neil Fraser
2015-10-03 07:45:16 -07:00
parent ac9de0d915
commit c413fa1678
10 changed files with 43 additions and 23 deletions

View File

@@ -73,14 +73,23 @@ Blockly.PHP['text_append'] = function(block) {
};
Blockly.PHP['text_length'] = function(block) {
// String length.
// String or array length.
var functionName = Blockly.PHP.provideFunction_(
'length',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
' if (is_string($value)) {',
' return strlen($value);',
' } else {',
' return count($value);',
' }',
'}']);
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
return ['strlen(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
return [functionName + '(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['text_isEmpty'] = function(block) {
// Is the string null?
// Is the string null or array empty?
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];