public function walkSelectStatement(SelectStatement $AST)
{
$query = $this->_getQuery();
$sort = $query->getHint('newscoop.api.sort');
$orderByItems = array();
$components = $this->_getQueryComponents();
foreach ($sort as $field => $direction) {
$entityData = $this->findAliasForField($field);
$realField = \Newscoop\Gimme\PropertyMatcher::match($entityData[1], $field);
if ($field != $realField) {
$entityData = $this->findAliasForField($realField);
$field = $realField;
}
$alias = $entityData[0];
if ($alias !== false) {
if (!array_key_exists($alias, $components)) {
throw new \UnexpectedValueException("There is no component aliased by [{$alias}] in the given Query");
}
$meta = $components[$alias];
if (!$meta['metadata']->hasField($field)) {
throw new \UnexpectedValueException("There is no such field [{$field}] in the given Query component, aliased by [{$alias}]");
}
} else {
if (!array_key_exists($field, $components)) {
throw new \UnexpectedValueException("There is no component field [{$field}] in the given Query");
}
}
if ($alias !== false) {
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD, $alias, $field);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
} else {
$pathExpression = $field;
}
$orderByItem = new OrderByItem($pathExpression);
$orderByItem->type = $direction;
$orderByItems[] = $orderByItem;
}
$AST->orderByClause = new OrderByClause($orderByItems);
}