# tstache/randexp architecture

> Architecture diagram of the GitHub repository tstache/randexp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tstache/randexp
- Repository: https://github.com/tstache/randexp

## Overview

##### Main Components
- RandExp Class: This is the primary interface for users. It accepts a regex (plus optional flags) and exposes the generate() method.
- RegexTokenizer: Responsible for breaking down the regex string into tokens for further processing.
- Range & RangeSet: These handle character ranges and sets, allowing the generator to pick valid random characters within specified bounds. Note that RangeSet also appears in the tests, indicating its importance in managing sets of ranges.
- Exception Handling: RegexException and RandGenerationException provide error management for issues arising during regex parsing or string generation.
- Tests: Although not part of runtime architecture, the tests (such as RandExpTest, RangeSetTest, and RegexTokenizerTest) validate functionality and can be mentioned as part of the overall quality assurance process.

##### Relationships and Interactions
- The RandExp class orchestrates the process. When generate() is called, it uses the RegexTokenizer to parse the provided regular expression.
- The parsing process produces tokens that are then processed using Range and RangeSet classes to form valid character sets, especially for tokens like ‘.’ or defined custom sets (e.g., [a-z_]).
- Any errors during tokenizing or generation may trigger the associated exceptions.

##### Architectural Patterns/Design Principles
- Layered Design: The system is modular. Input processing (through RandExp) is separated into parsing (RegexTokenizer) and token evaluation (Range, RangeSet).
- Exception Handling: Specific exceptions are used to clearly indicate different error scenarios.
- Reusability: The library is designed to be used via composer (as seen in composer.json), implying easy integration and reuse in various PHP projects.

##### Key Technologies and Frameworks
- PHP itself is the primary language.
- Composer manages dependencies.
- PHPUnit (configured via phpunit.xml) is used for testing.
- No external services are involved, but note how the inspiration from the JS library influenced the structure.

## Diagram (Mermaid)

```mermaid
flowchart TD
    H["User Input: Regex String"]:::core
    A["RandExp (API Entry)"]:::core
    B["RegexTokenizer (Parser)"]:::core
    C["Range (Character Range)"]:::core
    D["RangeSet (Character Set)"]:::core
    E["Generated Random String"]:::core
    F["RegexException (Parser Error)"]:::exception
    G["RandGenerationException (Generation Error)"]:::exception

    H -->|"calls_generate()"| A
    A -->|"parses_regex"| B
    B -->|"token_evaluation"| C
    B -->|"token_evaluation"| D
    C --> A
    D --> A
    A -->|"returns"| E
    B -.->|"error"| F
    A -.->|"error"| G

    subgraph "Unit Testing"
        I["RandExpTest"]:::test
        J["RegexTokenizerTest"]:::test
        K["RangeSetTest"]:::test
    end

    I -.->|"tests"| A
    J -.->|"tests"| B
    K -.->|"tests"| D

    click A "https://github.com/tstache/randexp/blob/main/src/RandExp.php"
    click B "https://github.com/tstache/randexp/blob/main/src/RegexTokenizer.php"
    click C "https://github.com/tstache/randexp/blob/main/src/Range.php"
    click D "https://github.com/tstache/randexp/blob/main/src/RangeSet.php"
    click F "https://github.com/tstache/randexp/blob/main/src/RegexException.php"
    click G "https://github.com/tstache/randexp/blob/main/src/RandGenerationException.php"
    click I "https://github.com/tstache/randexp/blob/main/tests/RandExpTest.php"
    click J "https://github.com/tstache/randexp/blob/main/tests/RegexTokenizerTest.php"
    click K "https://github.com/tstache/randexp/blob/main/tests/RangeSetTest.php"

    classDef core fill:#CCF,stroke:#333,stroke-width:2px;
    classDef exception fill:#FAA,stroke:#F00,stroke-width:2px;
    classDef test fill:#DFD,stroke:#080,stroke-width:2px;
```
