public function __construct($str) { static $templateFunctions; if (!$templateFunctions) { $templateFunctions = new Functions(); } $str = Template::unTokenize($str); // Parse all arg function calls in the passed string, // callback creates default values. $self = $this; $captureCallback = function ($str) use(&$self) { $args = Functions::parseArgsSimple($str); $position = array_shift($args); // Match the argument index integer. if (!isset($position) || !ctype_digit($position)) { return ''; } // Store the default value. $defaultValue = isset($args[0]) ? $args[0] : null; if (isset($defaultValue)) { $self->defaults[$position] = $defaultValue; } // Update argument count. $argNumber = (int) $position + 1; $self->argCount = max($self->argCount, $argNumber); return "?a{$position}?"; }; $templateFunctions->register['#'] = $captureCallback; $templateFunctions->register['arg'] = $captureCallback; $this->string = $templateFunctions->apply($str); }
function fn__this($input, $context) { $args = Functions::parseArgsSimple($input); $property = $args[0]; // Function relies on a context rule, bail if none. if (!isset($context->rule)) { return ''; } $rule = $context->rule; $rule->declarations->expandData('data', $property); if (isset($rule->declarations->data[$property])) { return $rule->declarations->data[$property]; } elseif (isset($args[1])) { return $args[1]; } return ''; }
protected function placeVars(&$value) { static $varFunction, $varFunctionSimple; if (!$varFunction) { $varFunctionSimple = Regex::make('~\\$\\( \\s* ({{ ident }}) \\s* \\)~xS'); $varFunction = new Functions(array('$' => function ($rawArgs) { list($name, $defaultValue) = Functions::parseArgsSimple($rawArgs); if (isset(Crush::$process->vars[$name])) { return Crush::$process->vars[$name]; } else { return $defaultValue; } })); } // Variables with no default value. $value = preg_replace_callback($varFunctionSimple, function ($m) { $varName = $m[1]; if (isset(Crush::$process->vars[$varName])) { return Crush::$process->vars[$varName]; } }, $value, -1, $varsPlaced); // Variables with default value. if (strpos($value, '$(') !== false) { // Assume at least one replace. $varsPlaced = true; // Variables may be nested so need to apply full function parsing. $value = $varFunction->apply($value); } // If we know replacements have been made we may want to update $value. e.g URL tokens. return $varsPlaced; }
function px2em($input, $unit, $default_base) { list($px, $base) = Functions::parseArgsSimple($input) + array(16, $default_base); return round($px / $base, 5) . $unit; }