php-commerce/Data/ParsedItem.php
Snider a774f4e285 refactor: migrate namespace from Core\Commerce to Core\Mod\Commerce
Align commerce module with the monorepo module structure by updating
all namespaces to use the Core\Mod\Commerce convention. This change
supports the recent monorepo separation and ensures consistency with
other modules.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:23:12 +00:00

65 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Commerce\Data;
/**
* A parsed SKU item with its options.
*
* Example: LAPTOP-ram~16gb-ssd~512gb becomes ParsedItem('LAPTOP', [ram, ssd options])
*/
readonly class ParsedItem
{
/**
* @param array<SkuOption> $options
*/
public function __construct(
public string $baseSku,
public array $options = [],
) {}
/**
* Build the string representation.
*/
public function toString(): string
{
if (empty($this->options)) {
return $this->baseSku;
}
$optionStrings = array_map(
fn (SkuOption $opt) => $opt->toString(),
$this->options
);
return $this->baseSku.'-'.implode('-', $optionStrings);
}
/**
* Get option by code.
*/
public function getOption(string $code): ?SkuOption
{
foreach ($this->options as $option) {
if (strtolower($option->code) === strtolower($code)) {
return $option;
}
}
return null;
}
/**
* Check if item has a specific option.
*/
public function hasOption(string $code): bool
{
return $this->getOption($code) !== null;
}
public function __toString(): string
{
return $this->toString();
}
}