in generators/php/lists.js [347:438]
Blockly.PHP['lists_getSublist'] = function(block) {
// Get sublist.
var list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_NONE) || 'array()';
var where1 = block.getFieldValue('WHERE1');
var where2 = block.getFieldValue('WHERE2');
if (where1 == 'FIRST' && where2 == 'LAST') {
var code = list;
} else if (list.match(/^\$\w+$/) ||
(where1 != 'FROM_END' && where2 == 'FROM_START')) {
// If the list is a simple value or doesn't require a call for length, don't
// generate a helper function.
switch (where1) {
case 'FROM_START':
var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
break;
case 'FROM_END':
var at1 = Blockly.PHP.getAdjusted(block, 'AT1', 1, false,
Blockly.PHP.ORDER_SUBTRACTION);
at1 = 'count(' + list + ') - ' + at1;
break;
case 'FIRST':
var at1 = '0';
break;
default:
throw Error('Unhandled option (lists_getSublist).');
}
switch (where2) {
case 'FROM_START':
var at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
Blockly.PHP.ORDER_SUBTRACTION);
var length = at2 + ' - ';
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
length += at1;
} else {
length += '(' + at1 + ')';
}
length += ' + 1';
break;
case 'FROM_END':
var at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
Blockly.PHP.ORDER_SUBTRACTION);
var length = 'count(' + list + ') - ' + at2 + ' - ';
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
length += at1;
} else {
length += '(' + at1 + ')';
}
break;
case 'LAST':
var length = 'count(' + list + ') - ';
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
length += at1;
} else {
length += '(' + at1 + ')';
}
break;
default:
throw Error('Unhandled option (lists_getSublist).');
}
code = 'array_slice(' + list + ', ' + at1 + ', ' + length + ')';
} else {
var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
var at2 = Blockly.PHP.getAdjusted(block, 'AT2');
var functionName = Blockly.PHP.provideFunction_(
'lists_get_sublist',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list, $where1, $at1, $where2, $at2) {',
' if ($where1 == \'FROM_END\') {',
' $at1 = count($list) - 1 - $at1;',
' } else if ($where1 == \'FIRST\') {',
' $at1 = 0;',
' } else if ($where1 != \'FROM_START\') {',
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
' }',
' $length = 0;',
' if ($where2 == \'FROM_START\') {',
' $length = $at2 - $at1 + 1;',
' } else if ($where2 == \'FROM_END\') {',
' $length = count($list) - $at1 - $at2;',
' } else if ($where2 == \'LAST\') {',
' $length = count($list) - $at1;',
' } else {',
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
' }',
' return array_slice($list, $at1, $length);',
'}']);
var code = functionName + '(' + list + ', \'' +
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
}
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};