Oihana PHP

parseSteps.php

Table of Contents

Functions

parseSteps()  : array<string|int, int>
Parses a step-range expression into a sorted, deduplicated list of integer steps in `[1, $maxStep]`.

Functions

parseSteps()

Parses a step-range expression into a sorted, deduplicated list of integer steps in `[1, $maxStep]`.

parseSteps(string|null $input, int $maxStep) : array<string|int, int>

Designed for CLI commands that need to selectively run a subset of their internal steps, e.g. integration test commands with an --step=N option.

Accepted syntax:

  • null or '' (empty) → all steps [1..maxStep] (default).
  • 'all' or '*' → same as above (explicit "all").
  • 'N'[N] (single step).
  • 'N1-N2'[N1, N1+1, …, N2] (closed range, inclusive).
  • '-N'[1, 2, …, N] (open-left half range).
  • 'N-'[N, N+1, …, maxStep] (open-right half range).
  • 'A,B,C-D,E' → comma-separated tokens, each parsed independently and merged. Order is irrelevant ; the result is always sorted with duplicates removed.

Whitespace around tokens / hyphens is allowed and stripped.

Invalid inputs raise InvalidArgumentException. The helper rejects:

  • empty tokens (e.g. '1,,3'),
  • non-numeric tokens (e.g. 'abc'),
  • bare '-' (missing both bounds),
  • inverted ranges ('6-4'),
  • out-of-range numbers (< 1 or > $maxStep).
Parameters
$input : string|null

The step expression. null and '' mean "all".

$maxStep : int

The maximum step number (inclusive). Used to expand all / * and open-right ranges (N-), and to validate upper bounds.

Tags
throws
InvalidArgumentException

When the input cannot be parsed or contains out-of-range / inverted / malformed tokens.

author

Marc Alcaraz (ekameleon)

since
1.0.8
example
parseSteps( null      , 6 ) ; // [1, 2, 3, 4, 5, 6]
parseSteps( 'all'     , 6 ) ; // [1, 2, 3, 4, 5, 6]
parseSteps( '*'       , 6 ) ; // [1, 2, 3, 4, 5, 6]
parseSteps( '4'       , 6 ) ; // [4]
parseSteps( '4-6'     , 6 ) ; // [4, 5, 6]
parseSteps( '-4'      , 6 ) ; // [1, 2, 3, 4]
parseSteps( '5-'      , 6 ) ; // [5, 6]
parseSteps( '1,3,5-6' , 6 ) ; // [1, 3, 5, 6]
Return values
array<string|int, int>

Sorted, deduplicated list of step numbers, all in [1, $maxStep].

On this page

Search results