*/ private array $steps = []; /** * Register a step. * * @param Step $step Step. * @param int $priority Lower runs earlier in default order (10, 20, …). */ public function register( Step $step, int $priority = 50 ): void { $this->steps[ $step->name() ] = [ 'step' => $step, 'priority' => $priority, ]; } /** * Whether a step is registered. * * @param string $name Name. */ public function has( string $name ): bool { return isset( $this->steps[ $name ] ); } /** * Resolve a step by name. * * @param string $name Name. * * @throws DomainException When unknown. */ public function get( string $name ): Step { if ( ! isset( $this->steps[ $name ] ) ) { throw new DomainException( 'steps.unknown', 'Unknown step: ' . htmlspecialchars( $name, ENT_QUOTES, 'UTF-8' ) ); } return $this->steps[ $name ]['step']; } /** * Default ordered names by registration priority. * * @return list */ public function ordered_names(): array { $entries = array_values( $this->steps ); usort( $entries, static function ( array $a, array $b ): int { return $a['priority'] <=> $b['priority']; } ); $names = []; foreach ( $entries as $entry ) { $names[] = $entry['step']->name(); } return $names; } /** * Resolve an ordered list of step instances for a form configuration. * * @param FormConfiguration $form Form. * @return list * * @throws DomainException When a configured name is unknown. */ public function for_form( FormConfiguration $form ): array { $out = []; foreach ( $form->step_names() as $name ) { $out[] = $this->get( $name ); } return $out; } }