|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Moox\Core\Services; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Config; |
| 6 | + |
| 7 | +class TaxonomyService |
| 8 | +{ |
| 9 | + private ?string $currentResource = null; |
| 10 | + |
| 11 | + public function setCurrentResource(string $resource): void |
| 12 | + { |
| 13 | + $this->currentResource = $resource; |
| 14 | + } |
| 15 | + |
| 16 | + public function getCurrentResource(): ?string |
| 17 | + { |
| 18 | + return $this->currentResource; |
| 19 | + } |
| 20 | + |
| 21 | + private function ensureResourceIsSet(): void |
| 22 | + { |
| 23 | + if ($this->currentResource === null) { |
| 24 | + throw new \RuntimeException('Current resource is not set. Call setCurrentResource() first.'); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public function getTaxonomies(): array |
| 29 | + { |
| 30 | + $this->ensureResourceIsSet(); |
| 31 | + |
| 32 | + return Config::get("previews.{$this->currentResource}.taxonomies", []) |
| 33 | + ?: Config::get("builder.resources.{$this->currentResource}.taxonomies", []); |
| 34 | + } |
| 35 | + |
| 36 | + public function getTaxonomyModel(string $taxonomy): ?string |
| 37 | + { |
| 38 | + return $this->getTaxonomies()[$taxonomy]['model'] ?? null; |
| 39 | + } |
| 40 | + |
| 41 | + public function validateTaxonomy(string $taxonomy): void |
| 42 | + { |
| 43 | + $modelClass = $this->getTaxonomyModel($taxonomy); |
| 44 | + |
| 45 | + if (! $modelClass || ! class_exists($modelClass)) { |
| 46 | + throw new \InvalidArgumentException("Invalid model class for taxonomy: $taxonomy in resource: {$this->currentResource}"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function getTaxonomyRelationship(string $taxonomy): string |
| 51 | + { |
| 52 | + return $this->getTaxonomies()[$taxonomy]['relationship'] ?? 'taggable'; |
| 53 | + } |
| 54 | + |
| 55 | + public function getTaxonomyTable(string $taxonomy): string |
| 56 | + { |
| 57 | + return $this->getTaxonomies()[$taxonomy]['table'] ?? 'taggables'; |
| 58 | + } |
| 59 | + |
| 60 | + public function getTaxonomyForeignKey(string $taxonomy): string |
| 61 | + { |
| 62 | + return $this->getTaxonomies()[$taxonomy]['foreignKey'] ?? 'taggable_id'; |
| 63 | + } |
| 64 | + |
| 65 | + public function getTaxonomyRelatedKey(string $taxonomy): string |
| 66 | + { |
| 67 | + return $this->getTaxonomies()[$taxonomy]['relatedKey'] ?? 'tag_id'; |
| 68 | + } |
| 69 | + |
| 70 | + public function hasTaxonomies(): bool |
| 71 | + { |
| 72 | + $this->ensureResourceIsSet(); |
| 73 | + |
| 74 | + return ! empty($this->getTaxonomies()); |
| 75 | + } |
| 76 | +} |
0 commit comments