php-commerce/Data/BundleItem.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

77 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Commerce\Data;
/**
* A bundle of items (pipe-separated in SKU string).
*
* Example: LAPTOP-ram~16gb|MOUSE|PAD becomes BundleItem([LAPTOP, MOUSE, PAD items], hash)
*
* The hash is computed from sorted base SKUs (stripping options) for discount lookup.
*/
readonly class BundleItem
{
/**
* @param array<ParsedItem> $items
*/
public function __construct(
public array $items,
public string $hash,
) {}
/**
* Build the string representation (pipe-separated).
*/
public function toString(): string
{
return implode('|', array_map(
fn (ParsedItem $item) => $item->toString(),
$this->items
));
}
/**
* Get just the base SKUs (for display/debugging).
*/
public function getBaseSkus(): array
{
return array_map(
fn (ParsedItem $item) => $item->baseSku,
$this->items
);
}
/**
* Get the sorted base SKU string (what the hash is computed from).
*/
public function getBaseSkuString(): string
{
$skus = $this->getBaseSkus();
sort($skus);
return implode('|', $skus);
}
/**
* Check if bundle contains a specific base SKU.
*/
public function containsSku(string $baseSku): bool
{
return in_array(strtoupper($baseSku), array_map('strtoupper', $this->getBaseSkus()), true);
}
/**
* Count of items in bundle.
*/
public function count(): int
{
return count($this->items);
}
public function __toString(): string
{
return $this->toString();
}
}