/** * Generic function handling '#forargs' and '#fornumargs' as one */ protected static function perform_forargs(Parser &$parser, PPFrame $frame, array $funcArgs, array $templateArgs, $prefix = '') { // if not called within template instance: if (!$frame->isTemplate()) { return ''; } // name of the variable to store the argument name: $keyVar = array_shift($funcArgs); $keyVar = $keyVar !== null ? trim($frame->expand($keyVar)) : ''; // name of the variable to store the argument value: $valVar = array_shift($funcArgs); $valVar = $valVar !== null ? trim($frame->expand($valVar)) : ''; // unexpanded code: $rawCode = array_shift($funcArgs); $rawCode = $rawCode !== null ? $rawCode : ''; $output = ''; // if prefix contains numbers only or isn't set, get all arguments, otherwise just non-numeric $tArgs = preg_match('/^([1-9][0-9]*)?$/', $prefix) > 0 ? $frame->getArguments() : $frame->getNamedArguments(); foreach ($templateArgs as $argName => $argVal) { // if no filter or prefix in argument name: if ($prefix !== '' && strpos($argName, $prefix) !== 0) { continue; } if ($keyVar !== $valVar) { // variable with the argument name without prefix as value: self::setVariable($parser, $keyVar, substr($argName, strlen($prefix))); } // variable with the arguments value: self::setVariable($parser, $valVar, $argVal); // expand current run: $output .= trim($frame->expand($rawCode)); } return $output; }
/** * Function ensures that arrays are used for merging * * @param PPFrame $frame * * @return array */ protected function getFrameParams(PPFrame $frame) { //we use both getNamedArguments and getArguments to ensure we acquire variables no matter what frame is used $namedArgs = $frame->getNamedArguments(); $namedArgs = isset($namedArgs) ? is_array($namedArgs) ? $namedArgs : [$namedArgs] : []; $args = $frame->getArguments(); $args = isset($args) ? is_array($args) ? $args : [$args] : []; return array_merge($namedArgs, $args); }