Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace Domain\ValueObject;
- use Domain\Enum\InternetProtocol;
- use InvalidArgumentException;
- final readonly class IPAddress
- {
- public function __construct(
- // Si vraiment on veut du private il faut faire
- // private private(set) InternetProtocol $protocol,
- public InternetProtocol $protocol,
- public string $value,
- ) {
- $this->assertValueWithProtocol();
- }
- public function equals(IPAddress $other): bool
- {
- return $this->value === $other->value;
- }
- public function notEquals(IPAddress $other): bool
- {
- return !$this->equals($other);
- }
- private function assertValueWithProtocol(): void
- {
- if (!$this->protocol->supports($this->value)) {
- throw new InvalidArgumentException("Invalid address '{$this->value}' for protocol '{$this->protocol->value}'");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment