text( $stored, 'run_state' ) ); return new ScanState( run_state: $run_state ?? ScanRunState::Idle, phase: ScanPhase::tryFrom( $this->text( $stored, 'phase' ) ), started_at: $this->optional_number( $stored, 'started_at' ), last_read_post_id: $this->number( $stored, 'last_read_post_id' ), posts_read: $this->number( $stored, 'posts_read' ), finished_at: $this->optional_number( $stored, 'finished_at' ), posts_count: $this->number( $stored, 'posts_count' ), first_run: false !== ( $stored['first_run'] ?? true ) ); } public function save( ScanState $state ): void { update_option( self::OPTION, $state->to_array(), false ); } /** @param array $stored */ private function text( array $stored, string $key ): string { $value = $stored[ $key ] ?? ''; return is_string( $value ) ? $value : ''; } /** @param array $stored */ private function optional_number( array $stored, string $key ): int|null { $value = $stored[ $key ] ?? null; return is_int( $value ) ? $value : null; } /** @param array $stored */ private function number( array $stored, string $key ): int { return $this->optional_number( $stored, $key ) ?? 0; } }