1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| <?php
namespace App\Services\SEO;
class SchemaBuilder { protected array $data = []; public function article(array $article): self { $this->data = [ '@context' => 'https://schema.org', '@type' => 'Article', 'headline' => $article['title'], 'description' => $article['description'] ?? '', 'image' => $article['image'] ?? '', 'author' => [ '@type' => 'Person', 'name' => $article['author'] ?? '', ], 'publisher' => [ '@type' => 'Organization', 'name' => config('app.name'), 'logo' => [ '@type' => 'ImageObject', 'url' => asset('images/logo.png'), ], ], 'datePublished' => $article['published_at'] ?? '', 'dateModified' => $article['updated_at'] ?? '', ]; return $this; } public function product(array $product): self { $this->data = [ '@context' => 'https://schema.org', '@type' => 'Product', 'name' => $product['name'], 'description' => $product['description'] ?? '', 'image' => $product['images'] ?? [], 'brand' => [ '@type' => 'Brand', 'name' => $product['brand'] ?? '', ], 'offers' => [ '@type' => 'Offer', 'price' => $product['price'], 'priceCurrency' => $product['currency'] ?? 'USD', 'availability' => $product['in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock', ], ]; if (!empty($product['rating'])) { $this->data['aggregateRating'] = [ '@type' => 'AggregateRating', 'ratingValue' => $product['rating']['value'], 'reviewCount' => $product['rating']['count'], ]; } return $this; } public function breadcrumb(array $items): self { $this->data = [ '@context' => 'https://schema.org', '@type' => 'BreadcrumbList', 'itemListElement' => array_map(function ($item, $index) { return [ '@type' => 'ListItem', 'position' => $index + 1, 'name' => $item['name'], 'item' => $item['url'], ]; }, $items, array_keys($items)), ]; return $this; } public function organization(array $org = []): self { $this->data = [ '@context' => 'https://schema.org', '@type' => 'Organization', 'name' => $org['name'] ?? config('app.name'), 'url' => $org['url'] ?? config('app.url'), 'logo' => $org['logo'] ?? asset('images/logo.png'), 'contactPoint' => $org['contact'] ?? [], 'sameAs' => $org['social'] ?? [], ]; return $this; } public function faq(array $questions): self { $this->data = [ '@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => array_map(function ($q) { return [ '@type' => 'Question', 'name' => $q['question'], 'acceptedAnswer' => [ '@type' => 'Answer', 'text' => $q['answer'], ], ]; }, $questions), ]; return $this; } public function render(): string { if (empty($this->data)) { return ''; } return '<script type="application/ld+json">' . json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>'; } public function toArray(): array { return $this->data; } }
|