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> */ 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) ?: ''; } }