protected function Twig_Template::getAttribute

You are here

8 Template.php protected Twig_Template::getAttribute($object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)

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_Template constants):

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

File

core/vendor/twig/twig/lib/Twig/Template.php, line 349

Class

Twig_Template
Default base class for compiled templates.

Code

protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) {
  // array
  if (Twig_Template::METHOD_CALL !== $type) {
    $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;

    if ((is_array($object) && array_key_exists($arrayItem, $object)) || ($object instanceof ArrayAccess && isset($object[$arrayItem]))) {
      if ($isDefinedTest) {
        return true;
      }

      return $object[$arrayItem];
    }

    if (Twig_Template::ARRAY_CALL === $type || !is_object($object)) {
      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', $arrayItem, 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', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName());
      }
      elseif (Twig_Template::ARRAY_CALL === $type) {
        throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
      }
      else {
        throw new Twig_Error_Runtime(sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $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('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
  }

  $class = get_class($object);

  // object property
  if (Twig_Template::METHOD_CALL !== $type) {
    if (isset($object->$item) || array_key_exists((string) $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)));
  }

  $call = false;
  $lcItem = strtolower($item);
  if (isset(self::$cache[$class]['methods'][$lcItem])) {
    $method = (string) $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 = (string) $item;
    $call = true;
  }
  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);
  }

  // Some objects throw exceptions when they have __call, and the method we try
  // to call is not supported. If ignoreStrictCheck is true, we should return null.
  try {
    $ret = call_user_func_array(array($object, $method), $arguments);
  }
  catch (BadMethodCallException $e) {
    if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
      return null;
    }
    throw $e;
  }

  // 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;
}