*/ private array $pending = []; /** @var array */ private array $results = []; public function request(string $paraId): void { $parts = explode('-', $paraId); if (count($parts) < 2 || ! ctype_digit($parts[0]) || ! ctype_digit($parts[1])) { $this->results[$paraId] = null; return; } if (! array_key_exists($paraId, $this->results)) { $this->pending[$paraId] = [(int) $parts[0], (int) $parts[1]]; } } /** * @return array{toc: string, path: mixed}|null */ public function result(string $paraId): ?array { return $this->results[$paraId] ?? null; } public function hasPending(): bool { return $this->pending !== []; } public function load(): void { $batch = $this->pending; $this->pending = []; $rows = PaliText::query() ->where(function ($query) use ($batch) { foreach ($batch as [$book, $para]) { $query->orWhere(fn ($where) => $where->where('book', $book)->where('paragraph', $para)); } }) ->get(['book', 'paragraph', 'toc', 'path']) ->keyBy(fn (PaliText $row) => "{$row->book}-{$row->paragraph}"); foreach ($batch as $paraId => [$book, $para]) { $row = $rows->get("{$book}-{$para}"); $this->results[$paraId] = $row === null ? null : [ 'toc' => (string) $row->toc, 'path' => json_decode((string) $row->path, true), ]; } } }