lithium\data\source\Database::order PHP Method

order() public method

Return formatted clause for ORDER BY with known fields escaped and directions normalized to uppercase. When order direction is missing or unrecognized defaults to ASC.
public order ( string | array $order, object $context ) : string | null
$order string | array The clause to be formatted.
$context object
return string | null Formatted clause, `null` if there is nothing to format.
    public function order($order, $context)
    {
        if (!$order) {
            return null;
        }
        $model = $context->model();
        $alias = $context->alias();
        $normalized = array();
        if (is_string($order)) {
            if (preg_match('/^(.*?)\\s+((?:A|DE)SC)$/i', $order, $match)) {
                $normalized[$match[1]] = strtoupper($match[2]);
            } else {
                $normalized[$order] = 'ASC';
            }
        } else {
            foreach ($order as $field => $direction) {
                if (is_int($field)) {
                    $normalized[$direction] = 'ASC';
                } elseif (in_array($direction, array('ASC', 'DESC', 'asc', 'desc'))) {
                    $normalized[$field] = strtoupper($direction);
                } else {
                    $normalized[$field] = 'ASC';
                }
            }
        }
        $escaped = array();
        foreach ($normalized as $field => $direction) {
            if (!$model || !$model::schema($field)) {
                $field = $this->name($field);
            } else {
                $field = $this->name($alias) . '.' . $this->name($field);
            }
            $escaped[] = "{$field} {$direction}";
        }
        return 'ORDER BY ' . join(', ', $escaped);
    }