connection = $mirror->connection(); } public function ready(): bool { return $this->connection->getSchemaBuilder()->hasTable(self::TABLE); } /** * @param array{company_name?:string,tag?:string} $filters * @return array{data: list>, total: int} */ public function paginate(int $page, int $perPage, array $filters = [], ?string $sort = null, string $direction = 'asc'): array { if (! $this->ready()) { return ['data' => [], 'total' => 0]; } $query = $this->connection->table(self::TABLE); $companyName = trim((string) ($filters['company_name'] ?? '')); if ($companyName !== '') { $query->where('company_name', 'like', '%' . addcslashes($companyName, '%_\\') . '%'); } $tag = trim((string) ($filters['tag'] ?? '')); if ($tag !== '') { $query->where('tag', '=', $tag); } $total = (clone $query)->count(); $column = in_array($sort, self::SORTABLE, true) ? $sort : null; $direction = strtolower($direction) === 'desc' ? 'desc' : 'asc'; if ($column !== null) { $query->orderBy($column, $direction)->orderBy('sort_order'); } else { $query->orderBy('sort_order')->orderBy('id'); } $rows = $query ->forPage(max($page, 1), min(max($perPage, 1), 100)) ->get() ->map(static fn (object $row): array => (array) $row) ->all(); return ['data' => $rows, 'total' => $total]; } }