/
AleksWork
/
diary_plants
Обзор
Документация
Войти
/
AleksWork
/
diary_plants
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/Domain/ValueObject/UserMessage/MessageRecipient.php
57 строк
2 KB
AlexeySerov
Add user message repository decorator service and model
29 май 2026, 16:48
29 май 2026, 16:48
06bc68c
Код
Авторство
О чём код?
<?php // группирует поля идентификатор пользователя и идентификатор группы не могут одновременно иметь одинаковые значения namespace App\Domain\ValueObject\UserMessage; use Webmozart\Assert\Assert; use Doctrine\ORM\Mapping as ORM; #[ORM\Embeddable] final class MessageRecipient { // NULL, если сообщение отправлено конкретной ГРУППЕ #[ORM\Column(name: 'user_id', type: 'integer', nullable: true)] private ?int $userId = null; // NULL, если сообщение отправлено конкретному ПОЛЬЗОВАТЕЛЮ #[ORM\Column(name: 'group_id', type: 'integer', nullable: true)] private ?int $groupId = null; private function __construct() {} public static function fromUser(int $userId): self { Assert::greaterThan($userId, 0, 'User ID must be greater than 0.'); $recipient = new self(); $recipient->userId = $userId; return $recipient; } public static function fromGroup(int $groupId): self { Assert::greaterThan($groupId, 0, 'Group ID must be greater than 0.'); $recipient = new self(); $recipient->groupId = $groupId; return $recipient; } // Валидация инварианта при реальном обращении (опционально, для подстраховки) public function validate(): void { Assert::true( ($this->userId !== null && $this->groupId === null) || ($this->userId === null && $this->groupId !== null), 'Message recipient state is invalid.' ); } public function getUserId(): ?int { return $this->userId; } public function getGroupId(): ?int { return $this->groupId; } }