Skip to content

Commit

Permalink
Merge DataTypeGuesser and DataTypeToString into one to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinai committed Nov 25, 2020
1 parent 032b93e commit e6b4ae6
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 133 deletions.
8 changes: 8 additions & 0 deletions Api/DataTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Hyva\Admin\Api;

interface DataTypeInterface extends DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
{

}
8 changes: 4 additions & 4 deletions Model/DataType/ArrayDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;

use Hyva\Admin\Api\DataTypeInterface;
use function array_map as map;
use function array_merge as merge;
use function array_slice as slice;

class ArrayDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ArrayDataType implements DataTypeInterface
{
const TYPE_ARRAY = 'array';
const LIMIT = 10;

private DataTypeToStringConverterLocator $toStringConverterLocator;
private DataTypeToStringConverterLocatorInterface $toStringConverterLocator;

private DataTypeGuesserInterface $dataTypeGuesser;

public function __construct(
DataTypeToStringConverterLocator $toStringConverterLocator,
DataTypeToStringConverterLocatorInterface $toStringConverterLocator,
DataTypeGuesserInterface $dataTypeGuesser
) {
$this->toStringConverterLocator = $toStringConverterLocator;
Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/CategoryLinkDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Catalog\Api\Data\CategoryLinkInterface;

class CategoryLinkDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class CategoryLinkDataType implements DataTypeInterface
{
const TYPE_MAGENTO_CATEGORY_LINK = 'magento_category_link';

Expand Down
34 changes: 0 additions & 34 deletions Model/DataType/CompositeDataTypeGuesser.php

This file was deleted.

5 changes: 2 additions & 3 deletions Model/DataType/CustomerAddressDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Customer\Api\Data\AddressInterface;

use function array_filter as filter;

class CustomerAddressDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class CustomerAddressDataType implements DataTypeInterface
{
const TYPE_MAGENTO_CUSTOMER_ADDRESS = 'magento_customer_address';

Expand Down
48 changes: 48 additions & 0 deletions Model/DataType/DataTypeFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface as TypeGuesser;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;

use function array_reduce as reduce;

class DataTypeFacade implements TypeGuesser, DataTypeToStringConverterLocatorInterface
{
private array $dataTypeClassMap;

private DataTypeGuesserFactory $dataTypeGuesserFactory;

private DataTypeValueToStringConverterFactory $dataTypeValueToStringConverterFactory;

public function __construct(
array $dataTypeClassMap,
DataTypeGuesserFactory $dataTypeGuesserFactory,
DataTypeValueToStringConverterFactory $dataTypeValueToStringConverterFactory
) {
$this->dataTypeClassMap = $dataTypeClassMap;
$this->dataTypeGuesserFactory = $dataTypeGuesserFactory;
$this->dataTypeValueToStringConverterFactory = $dataTypeValueToStringConverterFactory;
}

public function valueToTypeCode($value): ?string
{
return reduce($this->dataTypeClassMap, function (?string $type, string $class) use ($value): ?string {
return $type ?? $this->dataTypeGuesserFactory->get($class)->valueToTypeCode($value);
}, null);
}

public function typeToTypeCode(string $type): ?string
{
return reduce($this->dataTypeClassMap, function (?string $typeCode, string $class) use ($type): ?string {
return $typeCode ?? $this->dataTypeGuesserFactory->get($class)->typeToTypeCode($type);
}, null);
}

public function forTypeCode(string $typeCode): ?DataTypeValueToStringConverterInterface
{
return isset($this->dataTypeClassMap[$typeCode])
? $this->dataTypeValueToStringConverterFactory->get($this->dataTypeClassMap[$typeCode])
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Hyva\Admin\Api\DataTypeGuesserInterface;
use Magento\Framework\ObjectManagerInterface;

class DataTypeGuesserPool
class DataTypeGuesserFactory
{
private ObjectManagerInterface $objectManager;

Expand Down
33 changes: 0 additions & 33 deletions Model/DataType/DataTypeToStringConverterLocator.php

This file was deleted.

10 changes: 10 additions & 0 deletions Model/DataType/DataTypeToStringConverterLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;

interface DataTypeToStringConverterLocatorInterface
{
public function forTypeCode(string $typeCode): ?DataTypeValueToStringConverterInterface;
}
5 changes: 2 additions & 3 deletions Model/DataType/DateTimeDataTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;

class DateTimeDataTypeConverter implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class DateTimeDataTypeConverter implements DataTypeInterface
{
const TYPE_DATETIME = 'datetime';

Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/ProductDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Catalog\Api\Data\ProductInterface;

class ProductDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ProductDataType implements DataTypeInterface
{
const MAGENTO_PRODUCT = 'magento_product';

Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/ProductGalleryDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Catalog\Model\Product\Image\UrlBuilder as ImageUrlBuilder;

use function array_map as map;

class ProductGalleryDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ProductGalleryDataType implements DataTypeInterface
{
const TYPE_MAGENTO_PRODUCT_GALLERY = 'magento_product_gallery';

Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/ProductGalleryEntryDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
use Magento\Catalog\Model\Product\Image\UrlBuilder as ImageUrlBuilder;

class ProductGalleryEntryDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ProductGalleryEntryDataType implements DataTypeInterface
{
const TYPE_MAGENTO_PRODUCT_GALLERY_ENTRY = 'magento_product_gallery_entry';

Expand Down
9 changes: 4 additions & 5 deletions Model/DataType/ProductLinkDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\Catalog\Api\Data\ProductLinkInterface;
use Magento\Framework\Reflection\DataObjectProcessor;

class ProductLinkDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ProductLinkDataType implements DataTypeInterface
{
const TYPE_MAGENTO_PRODUCT_LINK = 'magento_product_link';

private DataObjectProcessor $dataObjectProcessor;

private DataTypeToStringConverterLocator $toStringConverterLocator;
private DataTypeToStringConverterLocatorInterface $toStringConverterLocator;

public function __construct(
DataObjectProcessor $dataObjectProcessor,
DataTypeToStringConverterLocator $toStringConverterLocator
DataTypeToStringConverterLocatorInterface $toStringConverterLocator
) {
$this->dataObjectProcessor = $dataObjectProcessor;
$this->toStringConverterLocator = $toStringConverterLocator;
Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/ScalarAndNullDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;

class ScalarAndNullDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class ScalarAndNullDataType implements DataTypeInterface
{
const TYPE_SCALAR_NULL = 'scalar_null';

Expand Down
9 changes: 4 additions & 5 deletions Model/DataType/StockItemDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\Reflection\DataObjectProcessor;

class StockItemDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class StockItemDataType implements DataTypeInterface
{
const TYPE_MAGENTO_STOCK_ITEM = 'magento_stock_item';

private DataObjectProcessor $dataObjectProcessor;

private DataTypeToStringConverterLocator $toStringConverterLocator;
private DataTypeToStringConverterLocatorInterface $toStringConverterLocator;

public function __construct(
DataObjectProcessor $dataObjectProcessor,
DataTypeToStringConverterLocator $toStringConverterLocator
DataTypeToStringConverterLocatorInterface $toStringConverterLocator
) {
$this->dataObjectProcessor = $dataObjectProcessor;
$this->toStringConverterLocator = $toStringConverterLocator;
Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/TextDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;

class TextDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class TextDataType implements DataTypeInterface
{
const TYPE_TEXT = 'text';
const LONG_TEXT_MIN_LENGTH = 200;
Expand Down
5 changes: 2 additions & 3 deletions Model/DataType/UnknownDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Hyva\Admin\Model\DataType;

use Hyva\Admin\Api\DataTypeGuesserInterface;
use Hyva\Admin\Api\DataTypeValueToStringConverterInterface;
use Hyva\Admin\Api\DataTypeInterface;
use Hyva\Admin\Exception\UnableToCastToStringException;

class UnknownDataType implements DataTypeGuesserInterface, DataTypeValueToStringConverterInterface
class UnknownDataType implements DataTypeInterface
{
const TYPE_UNKNOWN = 'unknown';

Expand Down
6 changes: 3 additions & 3 deletions ViewModel/HyvaGrid/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Hyva\Admin\ViewModel\HyvaGrid;

use Hyva\Admin\Exception\UnableToCastToStringException;
use Hyva\Admin\Model\DataType\DataTypeToStringConverterLocator;
use Hyva\Admin\Model\DataType\DataTypeToStringConverterLocatorInterface;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template;
Expand All @@ -18,7 +18,7 @@ class Cell implements CellInterface

private ColumnDefinitionInterface $columnDefinition;

private DataTypeToStringConverterLocator $dataTypeToStringConverterLocator;
private DataTypeToStringConverterLocatorInterface $dataTypeToStringConverterLocator;

private LayoutInterface $layout;

Expand All @@ -30,7 +30,7 @@ class Cell implements CellInterface
public function __construct(
$value,
ColumnDefinitionInterface $columnDefinition,
DataTypeToStringConverterLocator $dataTypeToStringConverter,
DataTypeToStringConverterLocatorInterface $dataTypeToStringConverter,
LayoutInterface $layout,
Escaper $escaper
) {
Expand Down
Loading

0 comments on commit e6b4ae6

Please sign in to comment.