<?php

namespace App\Rules\Fragments;

use Illuminate\Validation\Rule;

/**
 * Shared rule-array fragment for the standard datatable paging parameters.
 * Spread into a Validator rules array: ...PagedTable::rules([...columns...])
 */
final class PagedTable
{

    /**
     * @param array $order_columns        Whitelist of sortable column keys.
     * @param bool  $require_start_length Controller endpoints require start/length;
     *                                    BaseController::paged_table does not.
     */
    public static function rules(array $order_columns, bool $require_start_length = true): array
    {
        return [
            'draw'            => [ 'integer' ],
            'start'           => $require_start_length ? [ 'required', 'min:0' ] : [ 'min:0' ],
            'length'          => $require_start_length ? [ 'required', 'min:0' ] : [ 'min:0' ],
            'order_direction' => [ Rule::in(['asc', 'desc']) ],
            'order_column'    => [ Rule::in($order_columns) ],
        ];
    }
}
