$serviceUnitIds * @param list $counters */ private function __construct( private ?int $id, private MachineName $name, private MachineType $type, private ?string $description, private array $serviceUnitIds, private ?int $responsibleUserId, private array $counters, private bool $active, ) { if ($serviceUnitIds === []) { throw new MachineNeedsServiceUnit; } } /** * @param list $serviceUnitIds * @param list $counters * * @throws MachineNeedsServiceUnit */ public static function create(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): self { return new self(null, $name, $type, self::text($description), array_values(array_unique($serviceUnitIds)), $responsibleUserId, $counters, true); } /** * @param list $serviceUnitIds * @param list $counters */ public static function reconstitute(int $id, MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters, bool $active): self { return new self($id, $name, $type, $description, $serviceUnitIds, $responsibleUserId, $counters, $active); } /** * @param list $serviceUnitIds * @param list $counters * * @throws MachineNeedsServiceUnit */ public function change(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): void { if ($serviceUnitIds === []) { throw new MachineNeedsServiceUnit; } $this->name = $name; $this->type = $type; $this->description = self::text($description); $this->serviceUnitIds = array_values(array_unique($serviceUnitIds)); $this->responsibleUserId = $responsibleUserId; $this->counters = $counters; } /** * Whether the machine serves any of the service units. * * @param list $serviceUnitIds */ public function servesAnyOf(array $serviceUnitIds): bool { return array_intersect($this->serviceUnitIds, $serviceUnitIds) !== []; } /** * @throws MachineInactive */ public function ensureActive(): void { if (! $this->active) { throw new MachineInactive; } } public function activate(): void { $this->active = true; } public function deactivate(): void { $this->active = false; } public function id(): ?int { return $this->id; } public function name(): MachineName { return $this->name; } public function type(): MachineType { return $this->type; } public function description(): ?string { return $this->description; } /** * @return list */ public function serviceUnitIds(): array { return $this->serviceUnitIds; } public function responsibleUserId(): ?int { return $this->responsibleUserId; } /** * @return list */ public function counters(): array { return $this->counters; } public function isActive(): bool { return $this->active; } private static function text(?string $value): ?string { $value = $value === null ? '' : trim($value); return $value === '' ? null : mb_substr($value, 0, 5000); } }