protected function Twig_Template::getAttribute

Returns the attribute value for a given array/object.

Parameters

mixed $object The object or array from where to get the item:

mixed $item The item to get from the array or object:

array $arguments An array of arguments to pass if the item is an object method:

string $type The type of attribute (@see Twig_TemplateInterface):

Boolean $isDefinedTest Whether this is only a defined check:

Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not:

Return value

mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true

Throws

Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false

1 call to Twig_Template::getAttribute()
Twig_TemplateTest::getAttribute in drupal/core/vendor/twig/twig/test/Twig/Tests/TemplateTest.php
Returns the attribute value for a given array/object.
1 method overrides Twig_Template::getAttribute()
Twig_TemplateTest::getAttribute in drupal/core/vendor/twig/twig/test/Twig/Tests/TemplateTest.php
Returns the attribute value for a given array/object.

File

drupal/core/vendor/twig/twig/lib/Twig/Template.php, line 337

Class

Twig_Template
Default base class for compiled templates.

Code

protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) {
  $item = ctype_digit((string) $item) ? (int) $item : (string) $item;

  // array
  if (Twig_TemplateInterface::METHOD_CALL !== $type) {
    if (is_array($object) && array_key_exists($item, $object) || $object instanceof ArrayAccess && isset($object[$item])) {
      if ($isDefinedTest) {
        return true;
      }
      return $object[$item];
    }
    if (Twig_TemplateInterface::ARRAY_CALL === $type) {
      if ($isDefinedTest) {
        return false;
      }
      if ($ignoreStrictCheck || !$this->env
        ->isStrictVariables()) {
        return null;
      }
      if (is_object($object)) {
        throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), -1, $this
          ->getTemplateName());
      }
      elseif (is_array($object)) {
        throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), -1, $this
          ->getTemplateName());
      }
      else {
        throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $item, gettype($object)), -1, $this
          ->getTemplateName());
      }
    }
  }
  if (!is_object($object)) {
    if ($isDefinedTest) {
      return false;
    }
    if ($ignoreStrictCheck || !$this->env
      ->isStrictVariables()) {
      return null;
    }
    throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object), -1, $this
      ->getTemplateName());
  }
  $class = get_class($object);

  // object property
  if (Twig_TemplateInterface::METHOD_CALL !== $type) {
    if (isset($object->{$item}) || array_key_exists($item, $object)) {
      if ($isDefinedTest) {
        return true;
      }
      if ($this->env
        ->hasExtension('sandbox')) {
        $this->env
          ->getExtension('sandbox')
          ->checkPropertyAllowed($object, $item);
      }
      return $object->{$item};
    }
  }

  // object method
  if (!isset(self::$cache[$class]['methods'])) {
    self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
  }
  $lcItem = strtolower($item);
  if (isset(self::$cache[$class]['methods'][$lcItem])) {
    $method = $item;
  }
  elseif (isset(self::$cache[$class]['methods']['get' . $lcItem])) {
    $method = 'get' . $item;
  }
  elseif (isset(self::$cache[$class]['methods']['is' . $lcItem])) {
    $method = 'is' . $item;
  }
  elseif (isset(self::$cache[$class]['methods']['__call'])) {
    $method = $item;
  }
  else {
    if ($isDefinedTest) {
      return false;
    }
    if ($ignoreStrictCheck || !$this->env
      ->isStrictVariables()) {
      return null;
    }
    throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this
      ->getTemplateName());
  }
  if ($isDefinedTest) {
    return true;
  }
  if ($this->env
    ->hasExtension('sandbox')) {
    $this->env
      ->getExtension('sandbox')
      ->checkMethodAllowed($object, $method);
  }
  $ret = call_user_func_array(array(
    $object,
    $method,
  ), $arguments);

  // useful when calling a template method from a template
  // this is not supported but unfortunately heavily used in the Symfony profiler
  if ($object instanceof Twig_TemplateInterface) {
    return $ret === '' ? '' : new Twig_Markup($ret, $this->env
      ->getCharset());
  }
  return $ret;
}