-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ product image group url validation (#210)
- Loading branch information
1 parent
c59a8e1
commit 1a2cf32
Showing
12 changed files
with
520 additions
and
190 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...uctImageGroupingProductPageSearch/Business/Expander/ProductImageGroupPageDataExpander.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Expander; | ||
|
||
use FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator\UrlValidatorInterface; | ||
use Generated\Shared\Transfer\ProductPageSearchTransfer; | ||
use Spryker\Shared\ProductPageSearch\ProductPageSearchConfig; | ||
|
||
class ProductImageGroupPageDataExpander implements ProductPageDataExpanderInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected const IMAGE_GROUP_NAME_EMPTY = '*'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected const EXTERNAL_URL_PREFIX = 'external_url'; | ||
|
||
protected UrlValidatorInterface $urlValidator; | ||
|
||
/** | ||
* @param \FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator\UrlValidatorInterface $urlValidator | ||
*/ | ||
public function __construct(UrlValidatorInterface $urlValidator) | ||
{ | ||
$this->urlValidator = $urlValidator; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @api | ||
* | ||
* @param array<string, mixed> $productData | ||
* @param \Generated\Shared\Transfer\ProductPageSearchTransfer $productAbstractPageSearchTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductPageSearchTransfer | ||
*/ | ||
public function expandProductPageData(array $productData, ProductPageSearchTransfer $productAbstractPageSearchTransfer): ProductPageSearchTransfer | ||
{ | ||
/** @var \Generated\Shared\Transfer\ProductPayloadTransfer $productPayloadTransfer */ | ||
$productPayloadTransfer = $productData[ProductPageSearchConfig::PRODUCT_ABSTRACT_PAGE_LOAD_DATA]; | ||
$imageSets = $productPayloadTransfer->getImages(); | ||
/** @var array<\Orm\Zed\ProductImage\Persistence\SpyProductImageSet> $imageSetsByLocale */ | ||
$imageSetsByLocale = $imageSets[$productData['fk_locale']] ?? []; | ||
|
||
$productImages = $productAbstractPageSearchTransfer->getProductImages(); | ||
|
||
$regrouped = []; | ||
|
||
foreach ($imageSetsByLocale as $imageSet) { | ||
foreach ($productImages as $productImage) { | ||
if ($productImage['fk_product_image_set'] === $imageSet->getIdProductImageSet()) { | ||
$key = $imageSet->getName(); | ||
$regrouped[$key === null || $key === '' ? static::IMAGE_GROUP_NAME_EMPTY : $key][] = $this->validateProductImageUrls($productImage); | ||
} | ||
} | ||
} | ||
|
||
return $productAbstractPageSearchTransfer->setGroupedProductImages($regrouped); | ||
} | ||
|
||
/** | ||
* @param array $productImage | ||
* | ||
* @return array | ||
*/ | ||
protected function validateProductImageUrls(array $productImage): array | ||
{ | ||
foreach ($productImage as $key => $data) { | ||
if ($data !== null && str_starts_with($key, static::EXTERNAL_URL_PREFIX)) { | ||
$productImage[$key] = $this->urlValidator->isValid($data) ? $data : null; | ||
} | ||
} | ||
|
||
return $productImage; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ductImageGroupingProductPageSearch/Business/Expander/ProductPageDataExpanderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Expander; | ||
|
||
use Generated\Shared\Transfer\ProductPageSearchTransfer; | ||
|
||
interface ProductPageDataExpanderInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @api | ||
* | ||
* @param array<string, mixed> $productData | ||
* @param \Generated\Shared\Transfer\ProductPageSearchTransfer $productAbstractPageSearchTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductPageSearchTransfer | ||
*/ | ||
public function expandProductPageData(array $productData, ProductPageSearchTransfer $productAbstractPageSearchTransfer): ProductPageSearchTransfer; | ||
} |
28 changes: 28 additions & 0 deletions
28
...oupingProductPageSearch/Business/ProductImageGroupingProductPageSearchBusinessFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business; | ||
|
||
use FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Expander\ProductImageGroupPageDataExpander; | ||
use FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Expander\ProductPageDataExpanderInterface; | ||
use FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator\UrlValidator; | ||
use FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator\UrlValidatorInterface; | ||
use Spryker\Zed\Kernel\Business\AbstractBusinessFactory; | ||
|
||
class ProductImageGroupingProductPageSearchBusinessFactory extends AbstractBusinessFactory | ||
{ | ||
/** | ||
* @return \FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Expander\ProductPageDataExpanderInterface | ||
*/ | ||
public function createProductImageGroupPageDataExpander(): ProductPageDataExpanderInterface | ||
{ | ||
return new ProductImageGroupPageDataExpander($this->createUrlValidator()); | ||
} | ||
|
||
/** | ||
* @return \FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator\UrlValidatorInterface | ||
*/ | ||
public function createUrlValidator(): UrlValidatorInterface | ||
{ | ||
return new UrlValidator(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ctImageGroupingProductPageSearch/Business/ProductImageGroupingProductPageSearchFacade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business; | ||
|
||
use Generated\Shared\Transfer\ProductPageSearchTransfer; | ||
use Spryker\Zed\Kernel\Business\AbstractFacade; | ||
|
||
/** | ||
* @method \FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\ProductImageGroupingProductPageSearchBusinessFactory getFactory() | ||
*/ | ||
class ProductImageGroupingProductPageSearchFacade extends AbstractFacade implements ProductImageGroupingProductPageSearchFacadeInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @api | ||
* | ||
* @param array<string, mixed> $productData | ||
* @param \Generated\Shared\Transfer\ProductPageSearchTransfer $productAbstractPageSearchTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductPageSearchTransfer | ||
*/ | ||
public function groupProductImageData(array $productData, ProductPageSearchTransfer $productAbstractPageSearchTransfer): ProductPageSearchTransfer | ||
{ | ||
return $this->getFactory()->createProductImageGroupPageDataExpander()->expandProductPageData($productData, $productAbstractPageSearchTransfer); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...oupingProductPageSearch/Business/ProductImageGroupingProductPageSearchFacadeInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business; | ||
|
||
use Generated\Shared\Transfer\ProductPageSearchTransfer; | ||
|
||
interface ProductImageGroupingProductPageSearchFacadeInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @api | ||
* | ||
* @param array<string, mixed> $productData | ||
* @param \Generated\Shared\Transfer\ProductPageSearchTransfer $productAbstractPageSearchTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductPageSearchTransfer | ||
*/ | ||
public function groupProductImageData(array $productData, ProductPageSearchTransfer $productAbstractPageSearchTransfer): ProductPageSearchTransfer; | ||
} |
21 changes: 21 additions & 0 deletions
21
...ondOfImpala/Zed/ProductImageGroupingProductPageSearch/Business/Validator/UrlValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator; | ||
|
||
class UrlValidator implements UrlValidatorInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected const PROTOCOL = '@^http|https://@i'; | ||
|
||
/** | ||
* @param string $url | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid(string $url): bool | ||
{ | ||
return (preg_match(static::PROTOCOL, $url)) && (filter_var($url, FILTER_VALIDATE_URL) !== false); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...la/Zed/ProductImageGroupingProductPageSearch/Business/Validator/UrlValidatorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace FondOfImpala\Zed\ProductImageGroupingProductPageSearch\Business\Validator; | ||
|
||
interface UrlValidatorInterface | ||
{ | ||
/** | ||
* @param string $url | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid(string $url): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.