71 lines
2 KiB
PHP
71 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\Entities;
|
|
|
|
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
|
use Domain\Models\ValueObjects\ProcessingCode;
|
|
use Domain\Models\ValueObjects\Stock\StockAdjustment;
|
|
use Domain\Models\ValueObjects\Stock\StockAmount;
|
|
use Domain\Models\ValueObjects\Stock\StockId;
|
|
use Shared\DDD\Models\AggregateRoot;
|
|
use Shared\DDD\Models\HasTimestampIdentifier;
|
|
|
|
/**
|
|
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\Stock\StockId>
|
|
*/
|
|
final readonly class Stock implements AggregateRoot, HasTimestampIdentifier
|
|
{
|
|
private function __construct(
|
|
public StockId $id,
|
|
public StockAmount $stockAmount,
|
|
public CampaignCode $campaignCode,
|
|
public ProcessingCode $processingCode,
|
|
/** @var array<int, StockAdjustment> $stockAdjustment */
|
|
public array $stockAdjustment,
|
|
public ?\DateTimeInterface $updatedAt = null,
|
|
) {
|
|
foreach ($this->stockAdjustment as $adjustment) {
|
|
if (!$adjustment instanceof StockAdjustment) {
|
|
throw new \DomainException('Stock adjustment must be StockAdjustment instances.');
|
|
}
|
|
}
|
|
}
|
|
|
|
public function id(): StockId
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function createdAt(): \DateTimeInterface
|
|
{
|
|
return $this->id->toDateTime();
|
|
}
|
|
|
|
public function updatedAt(): ?\DateTimeInterface
|
|
{
|
|
return $this->updatedAt;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, StockAdjustment> $stockAdjustment
|
|
*/
|
|
public static function hydrate(
|
|
StockId $id,
|
|
StockAmount $stockAmount,
|
|
CampaignCode $campaignCode,
|
|
ProcessingCode $processingCode,
|
|
array $stockAdjustment = [],
|
|
?\DateTimeInterface $updatedAt = null,
|
|
): self {
|
|
return new self(
|
|
id: $id,
|
|
stockAmount: $stockAmount,
|
|
campaignCode: $campaignCode,
|
|
processingCode: $processingCode,
|
|
stockAdjustment: $stockAdjustment,
|
|
updatedAt: $updatedAt,
|
|
);
|
|
}
|
|
}
|