PluginProbe
Auto Alt Text / trunk
Auto Alt Text vtrunk
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Domain / ValueObjects / ImageId.php

ImageId.php in Auto Alt Text trunk, at src/App/Domain/ValueObjects/ImageId.php

82 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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