152 lines
5.6 KiB
PHP
152 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
class WineSchema
|
|
{
|
|
private const CONTEXT = "https://schema.org";
|
|
|
|
public static function card(array $in): array
|
|
{
|
|
$url = (string)$in["url"];
|
|
$store = ["@id" => (string)$in["host"] . "/#store"];
|
|
$images = [];
|
|
foreach ((array)$in["images"] as $index => $image) {
|
|
$node = [
|
|
"@type" => "ImageObject",
|
|
"@id" => $url . "#image-" . ($index + 1),
|
|
"url" => $image["src"],
|
|
"contentUrl" => $image["src"],
|
|
"caption" => $image["alt"],
|
|
"inLanguage" => $in["language"],
|
|
];
|
|
if ($image["width"] > 0 && $image["height"] > 0) {
|
|
$node["width"] = ["@type" => "QuantitativeValue", "value" => $image["width"], "unitCode" => "E37"];
|
|
$node["height"] = ["@type" => "QuantitativeValue", "value" => $image["height"], "unitCode" => "E37"];
|
|
}
|
|
$images[] = $node;
|
|
}
|
|
|
|
$product = [
|
|
"@type" => "Product",
|
|
"@id" => $url . "#product",
|
|
"name" => $in["name"],
|
|
"url" => $url,
|
|
];
|
|
if ($in["description"] !== "") {
|
|
$product["description"] = $in["description"];
|
|
}
|
|
if ($images) {
|
|
$product["image"] = $images;
|
|
}
|
|
if ($in["sku"] !== "") {
|
|
$product["sku"] = $in["sku"];
|
|
}
|
|
if ($in["category"] !== "") {
|
|
$product["category"] = $in["category"];
|
|
}
|
|
if ($in["country"] !== "") {
|
|
$product["countryOfOrigin"] = ["@type" => "Country", "name" => $in["country"]];
|
|
}
|
|
if ($in["vintage"] !== "") {
|
|
$product["productionDate"] = $in["vintage"];
|
|
}
|
|
if (!empty($in["brand"]["name"])) {
|
|
$brand = ["@type" => "Brand", "name" => $in["brand"]["name"]];
|
|
if ($in["brand"]["url"] !== "") {
|
|
$brand["@id"] = $in["brand"]["url"] . "#brand";
|
|
$brand["url"] = $in["brand"]["url"];
|
|
}
|
|
$product["brand"] = $brand;
|
|
}
|
|
$properties = [];
|
|
foreach ((array)$in["properties"] as $row) {
|
|
$node = ["@type" => "PropertyValue", "name" => $row["name"], "value" => $row["value"]];
|
|
if (!empty($row["unitCode"])) {
|
|
$node["unitCode"] = $row["unitCode"];
|
|
}
|
|
$properties[] = $node;
|
|
}
|
|
if ($properties) {
|
|
$product["additionalProperty"] = $properties;
|
|
}
|
|
if ($in["price"] > 0 && $in["currency"] !== "") {
|
|
$offer = [
|
|
"@type" => "Offer",
|
|
"@id" => $url . "#offer",
|
|
"url" => $url,
|
|
"price" => $in["price"],
|
|
"priceCurrency" => $in["currency"],
|
|
"availability" => $in["canBuy"] ? "https://schema.org/InStock" : "https://schema.org/OutOfStock",
|
|
"itemCondition" => "https://schema.org/NewCondition",
|
|
"seller" => $store,
|
|
];
|
|
if ($in["listPrice"] > $in["price"]) {
|
|
$offer["priceSpecification"] = [
|
|
"@type" => "UnitPriceSpecification",
|
|
"priceType" => "https://schema.org/ListPrice",
|
|
"price" => $in["listPrice"],
|
|
"priceCurrency" => $in["currency"],
|
|
];
|
|
}
|
|
$product["offers"] = $offer;
|
|
}
|
|
if ($in["reviewCount"] > 0 && $in["reviewRating"] > 0) {
|
|
$product["aggregateRating"] = [
|
|
"@type" => "AggregateRating",
|
|
"ratingValue" => $in["reviewRating"],
|
|
"reviewCount" => $in["reviewCount"],
|
|
"bestRating" => 5,
|
|
"worstRating" => 1,
|
|
];
|
|
}
|
|
$reviews = [];
|
|
foreach ((array)$in["reviews"] as $review) {
|
|
$reviews[] = [
|
|
"@type" => "Review",
|
|
"author" => ["@type" => "Person", "name" => $review["author"]],
|
|
"datePublished" => $review["date"],
|
|
"reviewRating" => ["@type" => "Rating", "ratingValue" => $review["rating"], "bestRating" => 5, "worstRating" => 1],
|
|
"reviewBody" => $review["text"],
|
|
"inLanguage" => $review["language"],
|
|
];
|
|
}
|
|
if ($reviews) {
|
|
$product["review"] = $reviews;
|
|
}
|
|
$related = [];
|
|
foreach ((array)$in["related"] as $item) {
|
|
$related[] = ["@type" => "Product", "@id" => $item["url"] . "#product", "name" => $item["name"], "url" => $item["url"]];
|
|
}
|
|
if ($related) {
|
|
$product["isRelatedTo"] = $related;
|
|
}
|
|
|
|
$page = [
|
|
"@type" => "ItemPage",
|
|
"@id" => $url . "#webpage",
|
|
"url" => $url,
|
|
"name" => $in["name"],
|
|
"description" => $in["pageDescription"],
|
|
"inLanguage" => $in["language"],
|
|
"mainEntity" => ["@id" => $url . "#product"],
|
|
"datePublished" => $in["created"],
|
|
"dateModified" => $in["modified"],
|
|
];
|
|
if ($images) {
|
|
$page["primaryImageOfPage"] = ["@id" => $images[0]["@id"]];
|
|
}
|
|
$translations = [];
|
|
foreach ((array)$in["translations"] as $language => $translationUrl) {
|
|
$translations[] = ["@type" => "ItemPage", "@id" => $translationUrl . "#webpage", "url" => $translationUrl, "inLanguage" => $language];
|
|
}
|
|
if ($translations) {
|
|
$page["workTranslation"] = $translations;
|
|
}
|
|
|
|
return ["@context" => self::CONTEXT, "@graph" => [$page, $product]];
|
|
}
|
|
}
|