# spatie/schema-org architecture

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

- Interactive diagram: https://gitdiagram.com/spatie/schema-org
- Repository: https://github.com/spatie/schema-org

## Overview

##### Main Components

a. Core Components:
- Schema Factory
- Type System
- Graph System
- Multi-Typed Entity System
- Generator System

b. Supporting Components:
- Contract System
- Exception Handling
- Type Collection
- Property Management

**┌─────────────────────────────────────────────────────┐**
- Client Layer                       │
- ┘
- ↓
**┌─────────────────────────────────────────────────────┐**
- Schema Factory                     │
- (Schema::localBusiness(), Schema::person(), etc.)  │
- ┘
- ↓
**┌─────────────────────────────────────────────────────┐**
- Type Management Layer                 │
- ┬──────────────────┬──────────────────┤
- BaseType   │    Graph         │  MultiTypedEntity │
- ┴──────────────────┴──────────────────┘
- ↓
**┌─────────────────────────────────────────────────────┐**
- Property Management Layer              │
- ┬──────────────────┬──────────────────┤
- Properties  │   Type Collection │    Contracts     │
- ┴──────────────────┴──────────────────┘
- ↓
**┌─────────────────────────────────────────────────────┐**
- Generator Layer                     │
- ┬──────────────────┬──────────────────┤
- Parser     │     Writer       │    Templates     │
- ┴──────────────────┴──────────────────┘

##### Important Relationships

a. Schema Factory:
- Creates instances of all Schema.org types
- Connects to BaseType for type instantiation
- Provides access to Graph and MultiTypedEntity

b. Type System:
- BaseType as the foundation
- Inheritance relationships between types
- Contract implementation

c. Property Management:
- Property storage and retrieval
- Type validation
- Contract enforcement

d. Generator System:
- Parser components for Schema.org definitions
- Template system for code generation
- Writer system for output

##### Additional Elements

- Notes about key features like fluent interface and JSON-LD output
- Error handling flow with exception classes

##### Implementation Details

- The fluent builder pattern implementation
- JSON-LD output generation
- Type safety through contracts
- Graph and MultiTypedEntity functionality
- Property management and validation

##### Key Workflows

Main workflows:
##### Multi-Typed Entity → Type Combination → Property Merging

- How the different components interact
- The flow of data through the system
- Where to extend or modify the system
- How type safety is maintained
- The generation process for Schema.org types

Remember to maintain a balance between detail and clarity, focusing on the architectural decisions that make this library effective for generating Schema.org structured data.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CL[Client Application]
    end

    subgraph SchemaFactory
        SF[Schema Factory]:::factory
    end

    subgraph TypeManagement
        BT[BaseType]:::type
        GR[Graph]:::type
        MT[MultiTypedEntity]:::type
    end

    subgraph PropertyManagement
        PR[Properties]:::property
        TC[Type Collection]:::property
        CN[Contracts]:::property
    end

    subgraph Generator
        PS[Parser]:::generator
        WR[Writer]:::generator
        TP[Templates]:::generator
    end

    subgraph ExceptionHandling
        EX[Exceptions]:::error
    end

    %% Relationships
    CL --> SF
    SF --> BT
    SF --> GR
    SF --> MT
    
    BT --> PR
    BT --> TC
    BT --> CN
    
    GR --> PR
    GR --> TC
    
    MT --> PR
    MT --> TC
    
    PR --> PS
    TC --> WR
    CN --> TP

    %% Error Handling
    BT -.-> EX
    GR -.-> EX
    MT -.-> EX
    PR -.-> EX

    %% Styling
    classDef factory fill:#3498db
    classDef type fill:#2ecc71
    classDef property fill:#e67e22
    classDef generator fill:#f1c40f
    classDef error fill:#95a5a6

    %% Click Events
    click SF "https://github.com/spatie/schema-org/blob/main/src/Schema.php"
    click BT "https://github.com/spatie/schema-org/blob/main/src/BaseType.php"
    click GR "https://github.com/spatie/schema-org/blob/main/src/Graph.php"
    click MT "https://github.com/spatie/schema-org/blob/main/src/MultiTypedEntity.php"
    click PS "https://github.com/spatie/schema-org/tree/main/generator/Parser"
    click WR "https://github.com/spatie/schema-org/tree/main/generator/Writer"
    click TP "https://github.com/spatie/schema-org/tree/main/generator/templates"
    click CN "https://github.com/spatie/schema-org/tree/main/src/Contracts"
    click EX "https://github.com/spatie/schema-org/tree/main/src/Exceptions"

    %% Legend
    subgraph Legend
        L1[Factory Components]:::factory
        L2[Type Components]:::type
        L3[Property Components]:::property
        L4[Generator Components]:::generator
        L5[Error Handling]:::error
    end
```
