protected function Twig_Node_Expression_Call::getArguments

1 call to Twig_Node_Expression_Call::getArguments()
Twig_Node_Expression_Call::compileArguments in drupal/core/vendor/twig/twig/lib/Twig/Node/Expression/Call.php

File

drupal/core/vendor/twig/twig/lib/Twig/Node/Expression/Call.php, line 93

Class

Twig_Node_Expression_Call

Code

protected function getArguments($callable, $arguments) {
  $parameters = array();
  $named = false;
  foreach ($arguments as $name => $node) {
    if (!is_int($name)) {
      $named = true;
      $name = $this
        ->normalizeName($name);
    }
    $parameters[$name] = $node;
  }
  if (!$named) {
    return $parameters;
  }
  if (!$callable) {
    throw new LogicException(sprintf('Named arguments are not supported for %s "%s".', $this
      ->getAttribute('type'), $this
      ->getAttribute('name')));
  }

  // manage named arguments
  if (is_array($callable)) {
    $r = new ReflectionMethod($callable[0], $callable[1]);
  }
  elseif (is_object($callable) && !$callable instanceof Closure) {
    $r = new ReflectionObject($callable);
    $r = $r
      ->getMethod('__invoke');
  }
  else {
    $r = new ReflectionFunction($callable);
  }
  $definition = $r
    ->getParameters();
  if ($this
    ->hasNode('node')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('needs_environment') && $this
    ->getAttribute('needs_environment')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('needs_context') && $this
    ->getAttribute('needs_context')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('arguments') && null !== $this
    ->getAttribute('arguments')) {
    foreach ($this
      ->getAttribute('arguments') as $argument) {
      array_shift($definition);
    }
  }
  $arguments = array();
  $pos = 0;
  foreach ($definition as $param) {
    $name = $this
      ->normalizeName($param->name);
    if (array_key_exists($name, $parameters)) {
      $arguments[] = $parameters[$name];
      unset($parameters[$name]);
    }
    elseif (array_key_exists($pos, $parameters)) {
      $arguments[] = $parameters[$pos];
      unset($parameters[$pos]);
      ++$pos;
    }
    elseif ($param
      ->isDefaultValueAvailable()) {
      $arguments[] = new Twig_Node_Expression_Constant($param
        ->getDefaultValue(), -1);
    }
    elseif ($param
      ->isOptional()) {
      break;
    }
    else {
      throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $this
        ->getAttribute('type'), $this
        ->getAttribute('name')));
    }
  }
  foreach (array_keys($parameters) as $name) {
    throw new Twig_Error_Syntax(sprintf('Unknown argument "%s" for %s "%s".', $name, $this
      ->getAttribute('type'), $this
      ->getAttribute('name')));
  }
  return $arguments;
}