# brick/math architecture

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

- Interactive diagram: https://gitdiagram.com/brick/math
- Repository: https://github.com/brick/math

## Overview

##### Main Components

a) Public API Layer:
- Main public classes (BigNumber, BigInteger, BigDecimal, BigRational)
- RoundingMode enum
- Exception hierarchy

b) Internal Calculator Layer:
- Calculator interface/abstract class
- Three calculator implementations (BcMath, GMP, Native)

##### Relationships and Component Organization

a) Class Hierarchy:
- BigNumber as the parent class at the top
- BigInteger, BigDecimal, and BigRational inheriting from BigNumber

b) Exception Hierarchy:
- MathException as the base exception
- Specific exceptions inheriting from it
- Exceptions visually in a separate section

c) Calculator Strategy Pattern:
- Calculator as an abstract class/interface
- Three calculator implementations

##### Specific Design Elements

Use these visual elements:

- Blue: Public API classes
- Green: Calculator implementations
- Red: Exceptions
- Gray: Internal/Abstract components

- Rectangles: Concrete classes
- Rounded rectangles: Abstract classes/interfaces
- Hexagons: Enums (for RoundingMode)

##### Important Relationships to Highlight

- How BigNumber classes use the Calculator implementations
- Factory method pattern with 'of()' methods
- Exception throwing relationships

##### Implementation Details

Annotations for:
- Required PHP version (8.1+)
- Optional extensions (GMP, BCMath)
- Immutability guarantees
- Type conversion rules

This architecture represents a well-structured mathematical library following the Strategy pattern for different calculator implementations, with a clear separation between public API and internal implementation details.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Public_API
        BN["BigNumber (Abstract)"]:::abstract
        BI["BigInteger"]:::public
        BD["BigDecimal"]:::public
        BR["BigRational"]:::public
        RM["RoundingMode (Enum)"]:::enum
        
        BN --> BI
        BN --> BD
        BN --> BR
    end
    
    subgraph Calculator_Layer
        C["Calculator (Abstract)"]:::abstract
        BC["BCMath Calculator"]:::calculator
        GC["GMP Calculator"]:::calculator
        NC["Native Calculator"]:::calculator
        
        C --> BC
        C --> GC
        C --> NC
    end
    
    subgraph Exception_Layer
        ME["MathException"]:::exception
        DBZ["DivisionByZeroException"]:::exception
        IOE["IntegerOverflowException"]:::exception
        NN["NegativeNumberException"]:::exception
        NF["NumberFormatException"]:::exception
        RN["RoundingNecessaryException"]:::exception
        
        ME --> DBZ
        ME --> IOE
        ME --> NN
        ME --> NF
        ME --> RN
    end
    
    BN -.-> C
    BI -.-> C
    BD -.-> C
    BR -.-> C
    
    classDef public fill:#3498db,stroke:#2980b9,color:white
    classDef abstract fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef calculator fill:#2ecc71,stroke:#27ae60,color:white
    classDef exception fill:#e74c3c,stroke:#c0392b,color:white
    classDef enum fill:#9b59b6,stroke:#8e44ad,color:white
    
    click BN "https://github.com/brick/math/blob/master/src/BigNumber.php"
    click BI "https://github.com/brick/math/blob/master/src/BigInteger.php"
    click BD "https://github.com/brick/math/blob/master/src/BigDecimal.php"
    click BR "https://github.com/brick/math/blob/master/src/BigRational.php"
    click RM "https://github.com/brick/math/blob/master/src/RoundingMode.php"
    click C "https://github.com/brick/math/blob/master/src/Internal/Calculator.php"
    click BC "https://github.com/brick/math/blob/master/src/Internal/Calculator/BcMathCalculator.php"
    click GC "https://github.com/brick/math/blob/master/src/Internal/Calculator/GmpCalculator.php"
    click NC "https://github.com/brick/math/blob/master/src/Internal/Calculator/NativeCalculator.php"
    click ME "https://github.com/brick/math/blob/master/src/Exception/MathException.php"
    click DBZ "https://github.com/brick/math/blob/master/src/Exception/DivisionByZeroException.php"
    click IOE "https://github.com/brick/math/blob/master/src/Exception/IntegerOverflowException.php"
    click NN "https://github.com/brick/math/blob/master/src/Exception/NegativeNumberException.php"
    click NF "https://github.com/brick/math/blob/master/src/Exception/NumberFormatException.php"
    click RN "https://github.com/brick/math/blob/master/src/Exception/RoundingNecessaryException.php"
```
