forked from hyva-themes/magento2-hyva-admin
-
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.
Properly determine custom attribute types
- Loading branch information
Showing
26 changed files
with
675 additions
and
75 deletions.
There are no files selected for viewing
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
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,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\DataType; | ||
|
||
use Hyva\Admin\Api\DataTypeGuesserInterface; | ||
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface; | ||
use Magento\Catalog\Api\Data\CategoryLinkInterface; | ||
|
||
class CategoryLinkDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface | ||
{ | ||
const TYPE_MAGENTO_CATEGORY_LINK = 'magento_category_link'; | ||
|
||
public function valueToTypeCode($value): ?string | ||
{ | ||
return $this->isCategoryLinkInstance($value) | ||
? self::TYPE_MAGENTO_CATEGORY_LINK | ||
: null; | ||
} | ||
|
||
public function typeToTypeCode(string $type): ?string | ||
{ | ||
return $this->isCategoryLinkClassName($type) | ||
? self::TYPE_MAGENTO_CATEGORY_LINK | ||
: null; | ||
} | ||
|
||
private function isCategoryLinkInstance($value): bool | ||
{ | ||
return is_object($value) && $value instanceof CategoryLinkInterface; | ||
} | ||
|
||
private function isCategoryLinkClassName($value): bool | ||
{ | ||
return is_string($value) && is_subclass_of($value, CategoryLinkInterface::class); | ||
} | ||
|
||
/** | ||
* @param CategoryLinkInterface|mixed $value | ||
* @return string|null | ||
*/ | ||
public function toString($value): ?string | ||
{ | ||
return $this->valueToTypeCode($value) | ||
? $this->formatCategoryLink($value) | ||
: null; | ||
} | ||
|
||
public function toStringRecursive($value, $maxRecursionDepth = self::UNLIMITED_RECURSION): ?string | ||
{ | ||
return $this->toString($value); | ||
} | ||
|
||
private function formatCategoryLink(CategoryLinkInterface $categoryLink): string | ||
{ | ||
return sprintf('cat_%d', $categoryLink->getCategoryId()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\DataType; | ||
|
||
use Hyva\Admin\Api\DataTypeGuesserInterface; | ||
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface; | ||
use Magento\Customer\Api\Data\AddressInterface; | ||
|
||
use function array_filter as filter; | ||
|
||
class CustomerAddressDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface | ||
{ | ||
const TYPE_MAGENTO_CUSTOMER_ADDRESS = 'magento_customer_address'; | ||
|
||
public function valueToTypeCode($value): ?string | ||
{ | ||
return $this->isAddressInstance($value) | ||
? self::TYPE_MAGENTO_CUSTOMER_ADDRESS | ||
: null; | ||
} | ||
|
||
public function typeToTypeCode(string $type): ?string | ||
{ | ||
return $this->isAddressClassName($type) | ||
? self::TYPE_MAGENTO_CUSTOMER_ADDRESS | ||
: null; | ||
} | ||
|
||
private function isAddressInstance($value): bool | ||
{ | ||
return is_object($value) && $value instanceof AddressInterface; | ||
} | ||
|
||
private function isAddressClassName($value): bool | ||
{ | ||
return is_string($value) && is_subclass_of($value, AddressInterface::class); | ||
} | ||
|
||
/** | ||
* @param AddressInterface|null $value | ||
* @return string|null | ||
*/ | ||
public function toString($value): ?string | ||
{ | ||
return $this->valueToTypeCode($value) | ||
? $this->renderAddress($value) | ||
: null; | ||
} | ||
|
||
public function toStringRecursive($value, $maxRecursionDepth = self::UNLIMITED_RECURSION): ?string | ||
{ | ||
return $this->toString($value); | ||
} | ||
|
||
private function renderAddress(?AddressInterface $value): string | ||
{ | ||
$parts = [ | ||
$value->getFirstname() . ' ' . $value->getLastname(), | ||
implode(', ', filter($value->getStreet() ?? [])), | ||
$value->getCity(), | ||
$value->getPostcode(), | ||
$value->getCountryId(), | ||
]; | ||
return implode(', ', filter($parts)); | ||
} | ||
} |
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
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,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\DataType; | ||
|
||
use Hyva\Admin\Api\DataTypeGuesserInterface; | ||
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface; | ||
use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface; | ||
|
||
class DateTimeDataTypeConverter implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface | ||
{ | ||
const TYPE_DATETIME = 'datetime'; | ||
|
||
private DateTimeFormatterInterface $dateTimeFormatter; | ||
|
||
public function __construct(DateTimeFormatterInterface $dateTimeFormatter) | ||
{ | ||
$this->dateTimeFormatter = $dateTimeFormatter; | ||
} | ||
|
||
public function valueToTypeCode($value): ?string | ||
{ | ||
return is_string($value) && preg_match('/^\d{4}-\d\d-\d\d[ T]\d\d:\d\d:\d\d$/', $value) | ||
? self::TYPE_DATETIME | ||
: null; | ||
} | ||
|
||
public function typeToTypeCode(string $type): ?string | ||
{ | ||
return $type === self::TYPE_DATETIME ? self::TYPE_DATETIME : null; | ||
} | ||
|
||
public function toString($value): ?string | ||
{ | ||
return $this->valueToTypeCode($value) | ||
? $this->dateTimeFormatter->formatObject(new \DateTimeImmutable($value)) | ||
: null; | ||
} | ||
|
||
public function toStringRecursive($value, $maxRecursionDepth = self::UNLIMITED_RECURSION): ?string | ||
{ | ||
return $this->toString($value); | ||
} | ||
} |
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,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\DataType; | ||
|
||
use Hyva\Admin\Api\DataTypeGuesserInterface; | ||
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface; | ||
use Magento\Catalog\Model\Product\Image\UrlBuilder as ImageUrlBuilder; | ||
|
||
use function array_map as map; | ||
|
||
class GalleryDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface | ||
{ | ||
const TYPE_MAGENTO_PRODUCT_GALLERY = 'magento_product_gallery'; | ||
|
||
private ImageUrlBuilder $imageUrlBuilder; | ||
|
||
public function __construct(ImageUrlBuilder $imageUrlBuilder) | ||
{ | ||
$this->imageUrlBuilder = $imageUrlBuilder; | ||
} | ||
|
||
public function valueToTypeCode($value): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function typeToTypeCode(string $type): ?string | ||
{ | ||
return $type === 'gallery' | ||
? self::TYPE_MAGENTO_PRODUCT_GALLERY | ||
: null; | ||
} | ||
|
||
public function toString($value): ?string | ||
{ | ||
return implode('', map([$this, 'buildImageTag'], $value['images'] ?? [])); | ||
} | ||
|
||
public function toStringRecursive($value, $maxRecursionDepth = self::UNLIMITED_RECURSION): ?string | ||
{ | ||
return $this->toString($value); | ||
} | ||
|
||
private function buildImageTag(array $galleryImage): string | ||
{ | ||
$file = $galleryImage['file'] ?? ''; | ||
return $file | ||
? sprintf('<img src="%s" alt="Product Gallery Value"/>', $this->imageUrlBuilder->getUrl($file, 'thumbnail')) | ||
: ''; | ||
} | ||
} |
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.