Skip to content

Commit 225784b

Browse files
committed
Use Spaties library instead of our own implementation of structured data objects
1 parent 653a4eb commit 225784b

File tree

63 files changed

+105
-1121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+105
-1121
lines changed

src/DataMapper/OnlineStore/CompositeOnlineStoreDataMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\OnlineStore;
66

77
use Setono\SyliusSEOPlugin\DataMapper\AbstractDataMapper;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Organization\OnlineBusiness\OnlineStore;
8+
use Spatie\SchemaOrg\OnlineStore;
99
use Sylius\Component\Core\Model\ChannelInterface;
1010

1111
/**

src/DataMapper/OnlineStore/OnlineStoreDataMapper.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Setono\SyliusSEOPlugin\DataMapper\OnlineStore;
66

7-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Intangible\StructuredValue\ContactPoint;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Organization\OnlineBusiness\OnlineStore;
7+
use Spatie\SchemaOrg\OnlineStore;
8+
use Spatie\SchemaOrg\Schema;
99
use Sylius\Component\Core\Model\ChannelInterface;
1010
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1111

@@ -17,24 +17,28 @@ public function __construct(private readonly UrlGeneratorInterface $urlGenerator
1717

1818
public function map(ChannelInterface $channel, OnlineStore $onlineStore): void
1919
{
20-
$onlineStore->name = $channel->getName();
21-
$onlineStore->vatID = $channel->getShopBillingData()?->getTaxId();
22-
$onlineStore->contactPoint = new ContactPoint([
23-
'email' => $channel->getContactEmail(),
24-
'telephone' => $channel->getContactPhoneNumber(),
25-
]);
26-
$onlineStore->address = new ContactPoint\PostalAddress([
27-
'streetAddress' => $channel->getShopBillingData()?->getStreet(),
28-
'addressLocality' => $channel->getShopBillingData()?->getCity(),
29-
'addressCountry' => $channel->getShopBillingData()?->getCountryCode(),
30-
'postalCode' => $channel->getShopBillingData()?->getPostcode(),
31-
]);
20+
$onlineStore
21+
->name((string) $channel->getName())
22+
->vatID((string) $channel->getShopBillingData()?->getTaxId())
23+
->contactPoint(
24+
Schema::contactPoint()
25+
->email((string) $channel->getContactEmail())
26+
->telephone((string) $channel->getContactPhoneNumber()),
27+
)
28+
->address(
29+
Schema::postalAddress()
30+
->streetAddress((string) $channel->getShopBillingData()?->getStreet())
31+
->addressLocality((string) $channel->getShopBillingData()?->getCity())
32+
->addressCountry((string) $channel->getShopBillingData()?->getCountryCode())
33+
->postalCode((string) $channel->getShopBillingData()?->getPostcode()),
34+
)
35+
;
3236

3337
$hostname = $channel->getHostname();
3438
if (null === $hostname) {
35-
$onlineStore->url = $this->urlGenerator->generate(name: 'sylius_shop_homepage', referenceType: UrlGeneratorInterface::ABSOLUTE_URL);
39+
$onlineStore->url($this->urlGenerator->generate(name: 'sylius_shop_homepage', referenceType: UrlGeneratorInterface::ABSOLUTE_URL));
3640
} else {
37-
$onlineStore->url = sprintf('https://%s', $hostname);
41+
$onlineStore->url(sprintf('https://%s', $hostname));
3842
}
3943
}
4044
}

src/DataMapper/OnlineStore/OnlineStoreDataMapperInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Setono\SyliusSEOPlugin\DataMapper\OnlineStore;
66

7-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Organization\OnlineBusiness\OnlineStore;
7+
use Spatie\SchemaOrg\OnlineStore;
88
use Sylius\Component\Core\Model\ChannelInterface;
99

1010
interface OnlineStoreDataMapperInterface

src/DataMapper/Product/CompositeProductDataMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\Product;
66

77
use Setono\SyliusSEOPlugin\DataMapper\AbstractDataMapper;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
8+
use Spatie\SchemaOrg\Product;
99
use Sylius\Component\Core\Model\ProductVariantInterface;
1010

1111
/**

src/DataMapper/Product/ImageProductDataMapper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\Product;
66

77
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
8+
use Spatie\SchemaOrg\Product;
99
use Sylius\Component\Core\Model\ImageInterface;
1010
use Sylius\Component\Core\Model\ProductInterface;
1111
use Sylius\Component\Core\Model\ProductVariantInterface;
@@ -20,7 +20,7 @@ public function __construct(
2020

2121
public function map(ProductVariantInterface $productVariant, Product $product): void
2222
{
23-
if (null !== $product->image) {
23+
if ($product->getProperty('image') !== null) {
2424
return;
2525
}
2626

@@ -34,7 +34,7 @@ public function map(ProductVariantInterface $productVariant, Product $product):
3434
return;
3535
}
3636

37-
$product->image = $this->cacheManager->getBrowserPath($path, $this->filter);
37+
$product->image($this->cacheManager->getBrowserPath($path, $this->filter));
3838
}
3939

4040
private static function getImage(ProductVariantInterface $productVariant): ?ImageInterface

src/DataMapper/Product/OffersProductDataMapper.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\Product;
66

77
use function Setono\SyliusSEOPlugin\formatAmount;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Intangible\Enumeration\ItemAvailability;
9-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Intangible\Offer;
10-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Intangible\Offer\AggregateOffer;
11-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
128
use Setono\SyliusSEOPlugin\UrlGenerator\ProductVariantUrlGeneratorInterface;
9+
use Spatie\SchemaOrg\ItemAvailability;
10+
use Spatie\SchemaOrg\Product;
11+
use Spatie\SchemaOrg\Schema;
1312
use Sylius\Component\Channel\Context\ChannelContextInterface;
1413
use Sylius\Component\Core\Model\ChannelInterface;
1514
use Sylius\Component\Core\Model\ProductVariantInterface;
@@ -34,14 +33,13 @@ public function map(ProductVariantInterface $productVariant, Product $product):
3433
return;
3534
}
3635

37-
$aggregateOffer = new AggregateOffer();
38-
$aggregateOffer->addOffer(new Offer([
39-
'url' => $this->productVariantUrlGenerator->generate($productVariant),
40-
'priceCurrency' => $channel->getBaseCurrency()?->getCode(),
41-
'price' => formatAmount($channelPricing->getPrice()),
42-
'availability' => $this->availabilityChecker->isStockAvailable($productVariant) ? ItemAvailability::InStock : ItemAvailability::OutOfStock,
43-
]));
44-
45-
$product->offers = $aggregateOffer;
36+
/** @psalm-suppress InvalidArgument */
37+
$product->offers(Schema::aggregateOffer()->offers(
38+
Schema::offer()
39+
->url($this->productVariantUrlGenerator->generate($productVariant))
40+
->priceCurrency((string) $channel->getBaseCurrency()?->getCode())
41+
->price(formatAmount($channelPricing->getPrice()))
42+
->availability($this->availabilityChecker->isStockAvailable($productVariant) ? ItemAvailability::InStock : ItemAvailability::OutOfStock),
43+
));
4644
}
4745
}

src/DataMapper/Product/ProductDataMapper.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,16 @@
44

55
namespace Setono\SyliusSEOPlugin\DataMapper\Product;
66

7-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
7+
use Spatie\SchemaOrg\Product;
88
use Sylius\Component\Core\Model\ProductVariantInterface;
99

1010
final class ProductDataMapper implements ProductDataMapperInterface
1111
{
1212
public function map(ProductVariantInterface $productVariant, Product $product): void
1313
{
14-
$name = self::getName($productVariant);
15-
$product->name = null === $name ? null : trim(strip_tags($name));
16-
17-
$product->sku = $productVariant->getCode();
18-
}
19-
20-
private static function getName(ProductVariantInterface $productVariant): ?string
21-
{
22-
$name = $productVariant->getName();
23-
if (null !== $name && '' !== $name) {
24-
return $name;
25-
}
26-
27-
return $productVariant->getProduct()?->getName();
14+
$product
15+
->name(trim(strip_tags((string) ($productVariant->getName() ?? $productVariant->getProduct()?->getName()))))
16+
->sku((string) $productVariant->getCode())
17+
;
2818
}
2919
}

src/DataMapper/Product/ProductDataMapperInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Setono\SyliusSEOPlugin\DataMapper\Product;
66

7-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
7+
use Spatie\SchemaOrg\Product;
88
use Sylius\Component\Core\Model\ProductVariantInterface;
99

1010
interface ProductDataMapperInterface

src/DataMapper/ProductGroup/CompositeProductGroupDataMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\ProductGroup;
66

77
use Setono\SyliusSEOPlugin\DataMapper\AbstractDataMapper;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product\ProductGroup;
8+
use Spatie\SchemaOrg\ProductGroup;
99
use Sylius\Component\Core\Model\ProductInterface;
1010

1111
/**

src/DataMapper/ProductGroup/HasVariantProductGroupDataMapper.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Setono\SyliusSEOPlugin\DataMapper\ProductGroup;
66

77
use Setono\SyliusSEOPlugin\DataMapper\Product\ProductDataMapperInterface;
8-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product;
9-
use Setono\SyliusSEOPlugin\StructuredData\Thing\Product\ProductGroup;
8+
use Spatie\SchemaOrg\ProductGroup;
9+
use Spatie\SchemaOrg\Schema;
1010
use Sylius\Component\Core\Model\ProductInterface;
1111
use Sylius\Component\Core\Model\ProductVariantInterface;
1212

@@ -18,12 +18,16 @@ public function __construct(private readonly ProductDataMapperInterface $product
1818

1919
public function map(ProductInterface $product, ProductGroup $productGroup): void
2020
{
21+
$hasVariant = [];
22+
2123
/** @var ProductVariantInterface $variant */
2224
foreach ($product->getEnabledVariants() as $variant) {
23-
$p = new Product();
25+
$p = Schema::product();
2426
$this->productDataMapper->map($variant, $p);
2527

26-
$productGroup->hasVariant[] = $p;
28+
$hasVariant[] = $p;
2729
}
30+
31+
$productGroup->hasVariant($hasVariant);
2832
}
2933
}

0 commit comments

Comments
 (0)