|null */ private ?array $categories = null; public function tool(string $slug): ?array { if ($this->bySlug === null) { $tools = model(ToolModel::class)->allActive(); $this->bySlug = []; foreach ($tools as $tool) { // decode JSON columns once per request foreach (['input_formats', 'output_formats', 'how_to', 'faqs', 'related_slugs', 'aliases', 'requires_binaries', 'accepts_mimes'] as $col) { if (isset($tool[$col]) && is_string($tool[$col])) { $decoded = json_decode($tool[$col], true); $tool[$col] = is_array($decoded) ? $decoded : []; } elseif (! isset($tool[$col])) { $tool[$col] = []; } } $this->bySlug[$tool['slug']] = $tool; } } return $this->bySlug[mb_strtolower($slug)] ?? null; } public function categories(): array { if ($this->categories === null) { $this->categories = model(CategoryModel::class)->activeOrdered(); } return $this->categories; } public function category(string $slug): ?array { foreach ($this->categories() as $category) { if ($category['slug'] === $slug) { foreach (['faqs'] as $col) { if (isset($category[$col]) && is_string($category[$col])) { $decoded = json_decode($category[$col], true); $category[$col] = is_array($decoded) ? $decoded : []; } } return $category; } } return null; } public function categoryById(int $id): ?array { foreach ($this->categories() as $category) { if ((int) $category['id'] === $id) { return $category; } } return null; } public function toolsInCategory(string $slug): array { $category = $this->category($slug); if ($category === null) { return []; } $tools = model(ToolModel::class)->activeInCategory((int) $category['id']); foreach ($tools as &$t) { $this->decodeTool($t); } return $tools; } public function popular(int $limit = 8): array { $tools = model(ToolModel::class)->popular($limit); array_walk($tools, [$this, 'decodeTool']); return $tools; } public function featured(int $limit = 8): array { $tools = model(ToolModel::class)->featured($limit); array_walk($tools, [$this, 'decodeTool']); return $tools; } public function recent(int $limit = 6): array { $tools = model(ToolModel::class)->recent($limit); array_walk($tools, [$this, 'decodeTool']); return $tools; } public function all(): array { return array_values($this->index()); } public function countTools(): int { return count($this->index()); } private function index(): array { if ($this->bySlug === null) { $this->tool('__warm__'); // forces load } return $this->bySlug ?? []; } public static function decodeTool(array &$tool): void { foreach (['input_formats', 'output_formats', 'how_to', 'faqs', 'related_slugs', 'aliases', 'requires_binaries', 'accepts_mimes'] as $col) { if (isset($tool[$col]) && is_string($tool[$col])) { $decoded = json_decode($tool[$col], true); $tool[$col] = is_array($decoded) ? $decoded : []; } elseif (! isset($tool[$col])) { $tool[$col] = []; } } } }