*/ private array $conditions = array(); /** * Registers a condition type. * * @param ConditionInterface $condition Condition to register. * @return void * * @throws ConditionException When a condition with the same id is already registered. */ public function register( ConditionInterface $condition ): void { $id = $condition->id(); if ( $this->has( $id ) ) { throw new ConditionException( esc_html( "Condition [$id] is already registered." ) ); } $this->conditions[ $id ] = $condition; } /** * Gets a registered condition by id. * * @param string $id Condition id. * @return ConditionInterface * * @throws ConditionException When the id is not registered. */ public function get( string $id ): ConditionInterface { if ( ! isset( $this->conditions[ $id ] ) ) { throw new ConditionException( esc_html( "Condition [$id] is not registered." ) ); } return $this->conditions[ $id ]; } /** * Reports whether a condition is registered. * * @param string $id Condition id. * @return bool */ public function has( string $id ): bool { return isset( $this->conditions[ $id ] ); } /** * Gets all registered conditions keyed by id. * * @return array */ public function all(): array { return $this->conditions; } /** * Counts registered conditions. * * @return int */ public function count(): int { return count( $this->conditions ); } }