<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Traits;

use App\Libraries\DbCursorHelper;

trait WithDbCursorHelper
{
    public static function makeDbCursorHelper(?string $sort = null)
    {
        return new DbCursorHelper(static::SORTS, static::DEFAULT_SORT, $sort);
    }

    private static function cursorSortExecOrder($query, array $sort)
    {
        foreach ($sort as $i => $sortItem) {
            $orderMethod = $i === 0 ? 'reorderBy' : 'orderBy';
            $query->$orderMethod($sortItem['column'], $sortItem['order']);
        }

        return $query;
    }

    private static function cursorSortExecWhere($query, ?array $preparedCursor)
    {
        if (empty($preparedCursor)) {
            return;
        }

        $current = array_shift($preparedCursor);

        $dir = strtoupper($current['order']) === 'DESC' ? '<' : '>';

        if (count($preparedCursor) === 0) {
            $query->where($current['column'], $dir, $current['value']);
        } else {
            $query->where($current['column'], "{$dir}=", $current['value'])
                ->where(function ($q) use ($current, $dir, $preparedCursor) {
                    return $q->where($current['column'], $dir, $current['value'])
                        ->orWhere(function ($qq) use ($preparedCursor) {
                            static::cursorSortExecWhere($qq, $preparedCursor);
                        });
                });
        }

        return $query;
    }

    public function scopeCursorSort($query, $sortOrCursorHelper, $cursorArrayOrStatic)
    {
        $cursorHelper = $sortOrCursorHelper instanceof DbCursorHelper
            ? $sortOrCursorHelper<commit_msg>Missing return<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Traits;

use App\Libraries\DbCursorHelper;

trait WithDbCursorHelper
{
    public static function makeDbCursorHelper(?string $sort = null)
    {
        return new DbCursorHelper(static::SORTS, static::DEFAULT_SORT, $sort);
    }

    private static function cursorSortExecOrder($query, array $sort)
    {
        foreach ($sort as $i => $sortItem) {
            $orderMethod = $i === 0 ? 'reorderBy' : 'orderBy';
            $query->$orderMethod($sortItem['column'], $sortItem['order']);
        }

        return $query;
    }

    private static function cursorSortExecWhere($query, ?array $preparedCursor)
    {
        if (empty($preparedCursor)) {
            return;
        }

        $current = array_shift($preparedCursor);

        $dir = strtoupper($current['order']) === 'DESC' ? '<' : '>';

        if (count($preparedCursor) === 0) {
            $query->where($current['column'], $dir, $current['value']);
        } else {
            $query->where($current['column'], "{$dir}=", $current['value'])
                ->where(function ($q) use ($current, $dir, $preparedCursor) {
                    return $q->where($current['column'], $dir, $current['value'])
                        ->orWhere(function ($qq) use ($preparedCursor) {
                            return static::cursorSortExecWhere($qq, $preparedCursor);
                        });
                });
        }

        return $query;
    }

    public function scopeCursorSort($query, $sortOrCursorHelper, $cursorArrayOrStatic)
    {
        $cursorHelper = $sortOrCursorHelper instanceof DbCursorHelper
            ? $sortOrCursorHelper
{'fraction_non_alphanumeric': 0.14238952536824878, 'fraction_numerical': 0.002182214948172395, 'mean_word_length': 2.443192488262911, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6417380', 'n_tokens_mistral': 1033, 'n_tokens_neox': 986, 'n_words': 235}