public function variable($key)
{
/* Support [0] style array indicies */
if (preg_match("|\\[[0-9]+\\]|", $key)) {
$key = preg_replace("|\\[([0-9]+)\\]|", ".\$1", $key);
}
$parts = explode(LIQUID_VARIABLE_ATTRIBUTE_SEPARATOR, $key);
$object = $this->fetch(array_shift($parts));
if (is_object($object)) {
if (!method_exists($object, 'toLiquid')) {
$object = (array) $object;
} else {
$object = $object->toLiquid();
}
}
if (!is_null($object)) {
while (count($parts) > 0) {
if ($object instanceof LiquidDrop) {
$object->setContext($this);
}
$nextPartName = array_shift($parts);
if (is_array($object)) {
// if the last part of the context variable is .size we just return the count
if ($nextPartName == 'size' && count($parts) == 0 && !array_key_exists('size', $object)) {
return count($object);
}
if (array_key_exists($nextPartName, $object)) {
$object = $object[$nextPartName];
} else {
return null;
}
} elseif (is_object($object)) {
if ($object instanceof LiquidDrop) {
// if the object is a drop, make sure it supports the given method
if (!$object->hasKey($nextPartName)) {
return null;
}
// php4 doesn't support array access, so we have
// to use the invoke method instead
$object = $object->invokeDrop($nextPartName);
} elseif (method_exists($object, LIQUID_HAS_PROPERTY_METHOD)) {
if (!call_user_method(LIQUID_HAS_PROPERTY_METHOD, $object, $nextPartName)) {
return null;
}
$object = call_user_method(LIQUID_GET_PROPERTY_METHOD, $object, $nextPartName);
} else {
// if it's just a regular object, attempt to access a property
if (!property_exists($object, $nextPartName)) {
return null;
}
$object = $object->{$nextPartName};
}
}
if (is_object($object) && method_exists($object, 'toLiquid')) {
$object = $object->toLiquid();
}
}
return $object;
} else {
return null;
}
}