init
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model;
|
||||
|
||||
use Magento\Framework\Module\Dir\Reader as ModuleDirReader;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
|
||||
class PrintingParameter
|
||||
{
|
||||
private ?array $data = null;
|
||||
private ?array $groups = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly ModuleDirReader $moduleDirReader,
|
||||
private readonly Json $json
|
||||
) {
|
||||
}
|
||||
|
||||
public function getByProductName(string $productName): array
|
||||
{
|
||||
$data = $this->getData();
|
||||
if (!$data) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$productName = trim($productName);
|
||||
if ($productName === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (isset($data[$productName]) && is_array($data[$productName])) {
|
||||
return $this->withGroupConfig($data[$productName]);
|
||||
}
|
||||
|
||||
$normalizedProductName = $this->normalizeName($productName);
|
||||
foreach ($data as $name => $parameters) {
|
||||
if ($this->normalizeName((string)$name) === $normalizedProductName && is_array($parameters)) {
|
||||
return $this->withGroupConfig($parameters);
|
||||
}
|
||||
}
|
||||
|
||||
uksort($data, static fn (string $a, string $b): int => strlen($b) <=> strlen($a));
|
||||
foreach ($data as $name => $parameters) {
|
||||
$normalizedName = $this->normalizeName((string)$name);
|
||||
if ($normalizedName !== ''
|
||||
&& (str_contains($normalizedProductName, $normalizedName) || str_contains($normalizedName, $normalizedProductName))
|
||||
&& is_array($parameters)
|
||||
) {
|
||||
return $this->withGroupConfig($parameters);
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, array{icon: string, items: array}>>
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->getData() as $name => $parameters) {
|
||||
if (is_array($parameters)) {
|
||||
$result[(string)$name] = $this->withGroupConfig($parameters);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getData(): array
|
||||
{
|
||||
if ($this->data !== null) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$path = $this->moduleDirReader->getModuleDir('', 'Esun_Catalog') . '/data/printing-parameters.json';
|
||||
if (!is_file($path)) {
|
||||
return $this->data = [];
|
||||
}
|
||||
|
||||
try {
|
||||
$decoded = $this->json->unserialize((string)file_get_contents($path));
|
||||
return $this->data = is_array($decoded) ? $decoded : [];
|
||||
} catch (\InvalidArgumentException) {
|
||||
return $this->data = [];
|
||||
}
|
||||
}
|
||||
|
||||
private function getGroups(): array
|
||||
{
|
||||
if ($this->groups !== null) {
|
||||
return $this->groups;
|
||||
}
|
||||
|
||||
$path = $this->moduleDirReader->getModuleDir('', 'Esun_Catalog') . '/data/printing-parameter-groups.json';
|
||||
if (!is_file($path)) {
|
||||
return $this->groups = [];
|
||||
}
|
||||
|
||||
try {
|
||||
$decoded = $this->json->unserialize((string)file_get_contents($path));
|
||||
return $this->groups = is_array($decoded) ? $decoded : [];
|
||||
} catch (\InvalidArgumentException) {
|
||||
return $this->groups = [];
|
||||
}
|
||||
}
|
||||
|
||||
private function withGroupConfig(array $parameters): array
|
||||
{
|
||||
$groups = $this->getGroups();
|
||||
$result = [];
|
||||
foreach ($parameters as $groupName => $items) {
|
||||
if (!is_array($items)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[(string)$groupName] = [
|
||||
'icon' => (string)($groups[$groupName]['icon'] ?? ''),
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function normalizeName(string $name): string
|
||||
{
|
||||
$name = strtolower($name);
|
||||
return preg_replace('/[^a-z0-9]+/', '', $name) ?: '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user