owner_id < 1 ) { throw new DomainException( 'override.owner_id', 'owner_id must be positive.' ); } if ( $this->is_closed ) { if ( null !== $this->start_local || null !== $this->end_local ) { throw new DomainException( 'override.closed_times', 'Closed overrides must not carry open hours.' ); } return; } if ( null === $this->start_local || null === $this->end_local ) { throw new DomainException( 'override.open_times', 'Open overrides require start_local and end_local.' ); } $this->start_local = self::normalise_time( $this->start_local ); $this->end_local = self::normalise_time( $this->end_local ); if ( $this->end_local <= $this->start_local ) { throw new DomainException( 'override.range', 'end_local must be after start_local.' ); } } /** * Constructor. * * @param string $time Time string. * @return string HH:MM:SS * * @throws DomainException When invalid. */ private static function normalise_time( string $time ): string { $time = trim( $time ); if ( 1 === preg_match( '/^\d{2}:\d{2}$/', $time ) ) { $time .= ':00'; } if ( 1 !== preg_match( '/^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $time ) ) { throw new DomainException( 'override.time_invalid', 'Time must be HH:MM or HH:MM:SS.' ); } return $time; } /** * Database id. * * @since 0.1.0 */ public function id(): ?int { return $this->id; } /** * Owner type. * * @since 0.1.0 */ public function owner_type(): ScheduleOwnerType { return $this->owner_type; } /** * Owner id. * * @since 0.1.0 */ public function owner_id(): int { return $this->owner_id; } /** * Date local. * * @since 0.1.0 */ public function date_local(): DateTimeImmutable { return $this->date_local; } /** * Is closed. * * @since 0.1.0 */ public function is_closed(): bool { return $this->is_closed; } /** * Start local. * * @since 0.1.0 */ public function start_local(): ?string { return $this->start_local; } /** * End local. * * @since 0.1.0 */ public function end_local(): ?string { return $this->end_local; } /** * Reason. * * @since 0.1.0 */ public function reason(): ?string { return $this->reason; } /** * Assign the database id after insert. * * @param int $id New id. * * @throws \Bookline\Domain\Shared\DomainException When id is already set or invalid. * * @since 0.1.0 */ public function assign_id( int $id ): void { if ( null !== $this->id ) { throw new DomainException( 'override.id_assigned', 'Override id is already assigned.' ); } $this->id = $id; } }