| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Domain\ValueObjects; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Value Object representing a WordPress attachment/image ID. |
| 9 |
* |
| 10 |
* This immutable object ensures that image IDs are always valid |
| 11 |
* (positive integers) throughout the application. |
| 12 |
*/ |
| 13 |
final class ImageId |
| 14 |
{ |
| 15 |
/** |
| 16 |
* The image ID value |
| 17 |
* |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
private $value; |
| 21 |
|
| 22 |
/** |
| 23 |
* Private constructor to enforce factory method usage. |
| 24 |
* |
| 25 |
* @param int $value The image ID |
| 26 |
* @throws InvalidArgumentException If value is not positive |
| 27 |
*/ |
| 28 |
private function __construct(int $value) |
| 29 |
{ |
| 30 |
if ($value <= 0) { |
| 31 |
throw new InvalidArgumentException( |
| 32 |
sprintf('Image ID must be a positive integer, got: %d', $value) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
$this->value = $value; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Create an ImageId from an integer value. |
| 41 |
* |
| 42 |
* @param int $value The image ID |
| 43 |
* @return self |
| 44 |
* @throws InvalidArgumentException If value is not positive |
| 45 |
*/ |
| 46 |
public static function fromInt(int $value): self |
| 47 |
{ |
| 48 |
return new self($value); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the integer value of the image ID. |
| 53 |
* |
| 54 |
* @return int |
| 55 |
*/ |
| 56 |
public function toInt(): int |
| 57 |
{ |
| 58 |
return $this->value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Check if this ImageId equals another ImageId. |
| 63 |
* |
| 64 |
* @param ImageId $other The other ImageId to compare |
| 65 |
* @return bool True if equal, false otherwise |
| 66 |
*/ |
| 67 |
public function equals(ImageId $other): bool |
| 68 |
{ |
| 69 |
return $this->value === $other->value; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* String representation for debugging. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function __toString(): string |
| 78 |
{ |
| 79 |
return (string) $this->value; |
| 80 |
} |
| 81 |
} |
| 82 |
|