protected function value($root, $path=null)
    {
        if (!$path instanceof KeyExpression) {
            return $path instanceof Expression ? "$path" : $path;
        }

        if (!is_array($root) && !is_object($root)) {
            return $root;
        }

        $pathString = $path->toString();

        if ($value = $this->extractor->value($root, $pathString)) {
            return $value;
        }

        // If a "to many" relation is checked here (like categories.id)
        // the extractor will not be able to retrieve the id, because it will
        // look for $root->categories['id'] but it is a numeric array like
        // this: categories[0]->id. So we have to make a workaround for this
        if (!strpos($pathString, '.')) {
            return $value;
        }

        return $this->buildCollection($root, $pathString);

    }