# phpcfdi/sat-catalogos-populate architecture

> Architecture diagram of the GitHub repository phpcfdi/sat-catalogos-populate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/phpcfdi/sat-catalogos-populate
- Repository: https://github.com/phpcfdi/sat-catalogos-populate

## Overview

1. **What this repository is**
   This is a PHP 8.2+ command-line tool that builds and refreshes a **SQLite3 catalog database** for SAT/CFDI reference data. Its architecture is a pipeline: **discover source origins → fetch/normalize spreadsheet files → convert to CSV → import into SQLite**.

2. **CLI entrypoint and orchestration**
   The main executable is `bin/sat-catalogos-update`, which boots `src/Commands/CliApplication.php`. The command layer is small and centralized, with command classes such as `DumpOrigins`, `UpdateOrigins`, and `UpdateDatabase`, plus a terminal logger. This is the top-level boundary for all user-facing behavior.

3. **Origin management subsystem**
   The `src/Origins` namespace manages where catalog source files come from and how they are described. It includes:
   - origin types for different source styles (`ConstantOrigin`, `ScrapingOrigin`, `HidroPetro10Origin`, `Nomina12eOrigin`)
   - translators that turn origin definitions into runtime objects
   - reviewers that validate or extract information from source pages/files
   - `OriginsIO`, `Upgrader`, and `Reviews` for reading, updating, and comparing origin metadata  
   This subsystem is about **metadata and discovery**, not parsing catalog rows.

4. **Source acquisition boundary**
   `WebResourcesGateway` and `ResourcesGatewayInterface` form the I/O boundary for fetching remote resources. The repo expects SAT-originated spreadsheets or pages to be retrieved from the web, then processed locally. This is the point where the tool crosses from internal processing into external network access.

5. **Conversion layer: spreadsheet to CSV**
   The `src/Converters` namespace normalizes Excel inputs into a filesystem of CSV files. It includes:
   - `XlsToXlsxConverter`
   - `XlsToCsvFolderConverter`
   - `XlsxToCsvFolderConverter`
   - `CsvFolderJoinFiles`
   
   This layer exists because the input formats vary and some catalogs are split across multiple sheets/files. The README explicitly depends on external tools like **LibreOffice Calc** and **xlsx2csv**, so this boundary is operationally important.

6. **Importer family organization**
   The `src/Importers` tree is organized by SAT catalog family and version: `Cfdi`, `Cfdi40`, `Cce`, `Cce20`, `Ccp20`, `Ccp30`, `Ccp31`, `Nomina`, `Ret20`, `Rep`, `HidroPretro10`, plus catalog aggregator classes like `CfdiCatalogs.php` and `Ccp31Catalogs.php`.  
   Each catalog family encapsulates the knowledge of which CSV files belong to that version and how they map into tables.

7. **Injector pattern for per-catalog mapping**
   Under each importer family, the `Injectors` subdirectory contains one class per catalog file/table. These injector classes are the catalog-specific adapters that connect a CSV source to a database table definition. They hide the per-catalog schema details from the generic import machinery.

8. **Generic import base classes**
   Shared importer abstractions such as `AbstractExcelImporter`, `AbstractXlsImporter`, `AbstractXlsxImporter`, and `AbstractXlsOneSheetImporter` provide the common mechanics for reading spreadsheet-derived data. `SourcesImporter` acts as a higher-level coordinator over catalog sources. This keeps the per-catalog classes thin and focused on mapping rather than file handling.

9. **Database model and field transformation layer**
   The `src/Database` namespace is the SQLite mapping layer. It includes:
   - `DataTable` and `DataTableGateway` for table-level persistence
   - `Repository` for access patterns
   - `DataFields` plus field types like `TextDataField`, `IntegerDataField`, `DateDataField`, `BoolDataField`, `FloatDataField`
   - transformation helpers like `PaddingDataField`, `UsePreviousIfEmptyDataField`, `PreprocessDataField`, `NumberFormatDataField`, `ToBeDefinedDataField`
   
   Architecturally, this is where raw CSV values become normalized SQLite rows.

10. **Row/column preprocessing utilities**
   `AbstractCsvInjector`, `AbstractCollection`, and the `src/Utils/ArrayProcessors` classes provide reusable row-cleaning behavior such as trimming, ignoring columns, and handling nulls. These are small but important because the SAT catalog files are not perfectly uniform across versions.

11. **Data flow from input to output**
   The end-to-end path is:
   - a command reads origin definitions
   - origins resolve remote or local source files
   - converters normalize spreadsheet input into CSV folders
   - importer/injector pairs map CSV files to catalog tables
   - the database layer writes the final SQLite3 file  
   In other words, **origins describe what to fetch, converters reshape the files, importers define catalog semantics, and the database layer persists the result**.

12. **Testing and fixtures mirror the architecture**
   The `tests/_files` directory contains representative spreadsheets, split CSV outputs, and origin XML fixtures for each catalog family/version. The test suite is split into unit and feature layers, which closely reflects the production boundaries: conversion, importing, origin handling, and database persistence. This makes the repo’s architecture very explicit and test-driven.

Main entry points: [`src/Commands`](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Commands) (Commands), [`src/Converters`](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Converters) (XLS/XLSX), [`src/Importers/SourcesImporter.php`](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Importers/SourcesImporter.php) (Sources importer), [`tests`](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/tests) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI"]
  node_bin_cli["sat-catalogos-update<br/>entrypoint"]
  node_cli_app["CLI app<br/>[CliApplication.php]"]
  node_commands["Commands<br/>command layer"]
  node_terminal_logger["Logger<br/>terminal output<br/>[TerminalLogger.php]"]
end

subgraph group_origins["Origins"]
  node_origins_io["Origins IO<br/>metadata IO<br/>[OriginsIO.php]"]
  node_origin_translators["Translators<br/>origin mapping"]
  node_origin_reviewers["Reviewers<br/>origin validation"]
  node_web_gateway["Web gateway<br/>network I/O"]
  node_origin_types["Origin types<br/>origin model"]
end

subgraph group_conversion["Conversion"]
  node_xls_converter["XLS/XLSX<br/>spreadsheet converter"]
  node_csv_joiner["CSV joiner<br/>folder merge"]
end

subgraph group_importing["Importing"]
  node_sources_importer["Sources importer<br/>import coordinator"]
  node_catalog_families["Catalog families<br/>family routers"]
  node_injectors["Injectors<br/>csv adapters"]
end

subgraph group_database["Database"]
  node_db_fields["Data fields<br/>field normalization"]
  node_data_table["Data table<br/>table persistence<br/>[DataTable.php]"]
  node_repository["Repository<br/>db access<br/>[Repository.php]"]
end

subgraph group_utils["Utils"]
  node_array_processors["Array processors<br/>row cleaning"]
  node_csv_file["CSV file<br/>filesystem helper<br/>[CsvFile.php]"]
  node_shell_exec["Shell exec<br/>tooling helper<br/>[ShellExec.php]"]
end

subgraph group_tests["Tests"]
  node_feature_tests["Tests<br/>feature/unit suite"]
end

node_bin_cli -->|"boots"| node_cli_app
node_cli_app -->|"dispatches"| node_commands
node_commands -->|"reads origins"| node_origins_io
node_commands -->|"fetches resources"| node_web_gateway
node_commands -->|"runs import"| node_sources_importer
node_commands -->|"logs"| node_terminal_logger
node_origins_io -->|"translates"| node_origin_translators
node_origin_translators -->|"instantiates"| node_origin_types
node_origin_types -->|"reviewed by"| node_origin_reviewers
node_origin_reviewers -->|"inspects remote"| node_web_gateway
node_web_gateway -->|"downloads inputs"| node_xls_converter
node_xls_converter -->|"normalizes"| node_csv_joiner
node_xls_converter -->|"produces csv"| node_sources_importer
node_sources_importer -->|"selects family"| node_catalog_families
node_catalog_families -->|"uses"| node_injectors
node_injectors -->|"cleans rows"| node_array_processors
node_injectors -->|"maps values"| node_db_fields
node_db_fields -->|"persists"| node_data_table
node_data_table -->|"served by"| node_repository
node_sources_importer -->|"writes through"| node_repository
node_csv_file -->|"supports"| node_array_processors
node_shell_exec -->|"invokes tools"| node_xls_converter
node_feature_tests -->|"covers"| node_origins_io
node_feature_tests -->|"covers"| node_xls_converter
node_feature_tests -->|"covers"| node_sources_importer
node_feature_tests -->|"covers"| node_data_table

click node_bin_cli "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/bin/sat-catalogos-update"
click node_cli_app "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Commands/CliApplication.php"
click node_commands "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Commands"
click node_terminal_logger "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Commands/TerminalLogger.php"
click node_origins_io "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Origins/OriginsIO.php"
click node_origin_translators "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/OriginsTranslators"
click node_origin_reviewers "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/Reviewers"
click node_web_gateway "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Origins/WebResourcesGateway.php"
click node_origin_types "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/Types"
click node_xls_converter "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Converters"
click node_csv_joiner "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Converters/CsvFolderJoinFiles.php"
click node_sources_importer "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Importers/SourcesImporter.php"
click node_catalog_families "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Importers"
click node_injectors "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Importers"
click node_db_fields "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Database"
click node_data_table "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Database/DataTable.php"
click node_repository "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Database/Repository.php"
click node_array_processors "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Utils/ArrayProcessors"
click node_csv_file "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Utils/CsvFile.php"
click node_shell_exec "https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Utils/ShellExec.php"
click node_feature_tests "https://github.com/phpcfdi/sat-catalogos-populate/tree/master/tests"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bin_cli,node_cli_app,node_commands,node_terminal_logger,node_feature_tests toneBlue
class node_origins_io,node_origin_translators,node_origin_reviewers,node_web_gateway,node_origin_types toneAmber
class node_xls_converter,node_csv_joiner toneMint
class node_sources_importer,node_catalog_families,node_injectors toneRose
class node_db_fields,node_data_table,node_repository toneIndigo
class node_array_processors,node_csv_file,node_shell_exec toneTeal
```

## Components

### CLI

User-facing commands and application bootstrap

- [sat-catalogos-update](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/bin/sat-catalogos-update): entrypoint · Main executable for the update workflow · `bin/sat-catalogos-update`
- [CLI app](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Commands/CliApplication.php): application · Bootstraps and runs commands · `src/Commands/CliApplication.php`
- [Commands](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Commands): command layer · Central command set for origin dumps and database updates · `src/Commands`
- [Logger](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Commands/TerminalLogger.php): terminal output · Console logging for command execution · `src/Commands/TerminalLogger.php`

### Origins

Origin metadata, review, and remote resource access

- [Origins IO](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Origins/OriginsIO.php): metadata IO · Reads and writes origin definitions · `src/Origins/OriginsIO.php`
- [Translators](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/OriginsTranslators): origin mapping · Converts origin definitions into runtime origin objects · `src/Origins/OriginsTranslators`
- [Reviewers](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/Reviewers): origin validation · Inspects or validates origin sources · `src/Origins/Reviewers`
- [Web gateway](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Origins/WebResourcesGateway.php): network I/O · Fetches remote resources from SAT-originated sources · `src/Origins/WebResourcesGateway.php`
- [Origin types](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Origins/Types): origin model · Constant, scraping, HidroPetro10, and Nomina12e origin definitions · `src/Origins/Types`

### Conversion

Spreadsheet normalization into CSV folders

- [XLS/XLSX](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Converters): spreadsheet converter · Converts spreadsheet inputs to normalized CSV folders · `src/Converters`
- [CSV joiner](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Converters/CsvFolderJoinFiles.php): folder merge · Combines split CSV files when needed · `src/Converters/CsvFolderJoinFiles.php`

### Importing

Catalog family importers and CSV injectors

- [Sources importer](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Importers/SourcesImporter.php): import coordinator · Coordinates source-level catalog imports · `src/Importers/SourcesImporter.php`
- [Catalog families](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Importers): family routers · Versioned import families such as CFDI, Carta Porte, Nómina, RET and Hidro/Petro · `src/Importers`
- [Injectors](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Importers): csv adapters · Per-catalog adapters that map CSV files to tables · `src/Importers`

### Database

SQLite table mapping and value normalization

- [Data fields](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Database): field normalization · Typed and transformed values for SQLite rows · `src/Database`
- [Data table](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Database/DataTable.php): table persistence · Table-level storage and gateway layer · `src/Database/DataTable.php`
- [Repository](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Database/Repository.php): db access · Database access patterns over catalog data · `src/Database/Repository.php`

### Utils

Shared low-level helpers

- [Array processors](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/src/Utils/ArrayProcessors): row cleaning · Trims, ignores, and null-handles CSV rows · `src/Utils/ArrayProcessors`
- [CSV file](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Utils/CsvFile.php): filesystem helper · CSV reading and writing helper · `src/Utils/CsvFile.php`
- [Shell exec](https://github.com/phpcfdi/sat-catalogos-populate/blob/master/src/Utils/ShellExec.php): tooling helper · External command execution support · `src/Utils/ShellExec.php`

### Tests

Feature and unit coverage mirroring the architecture

- [Tests](https://github.com/phpcfdi/sat-catalogos-populate/tree/master/tests): feature/unit suite · Coverage for origins, converters, importers, and database behavior · `tests`

## Connections

- sat-catalogos-update → CLI app: boots · The executable starts the CLI application
- CLI app → Commands: dispatches · Application dispatches the available commands
- Commands → Origins IO: reads origins · Commands load origin definitions and updates
- Commands → Web gateway: fetches resources · Commands cross the network boundary through the resource gateway
- Commands → Sources importer: runs import · Update commands orchestrate the import pipeline
- Commands → Logger: logs · Commands emit progress and diagnostics
- Origins IO → Translators: translates · Stored origin metadata becomes runtime origin objects
- Translators → Origin types: instantiates · Translators produce concrete origin types
- Origin types → Reviewers: reviewed by · Origin types are validated or inspected by reviewers
- Reviewers → Web gateway: inspects remote · Reviewers may parse remote pages or resource links
- Web gateway → XLS/XLSX: downloads inputs · Remote spreadsheet sources are handed to conversion
- XLS/XLSX → CSV joiner: normalizes · Converted outputs can be merged into unified CSV folders
- XLS/XLSX → Sources importer: produces csv · Normalized CSV folders feed the import pipeline
- Sources importer → Catalog families: selects family · Import coordinator chooses the catalog family implementation
- Catalog families → Injectors: uses · Family-level importers delegate table/file mapping to injectors
- Injectors → Array processors: cleans rows · Injectors reuse row cleanup processors during CSV handling
- Injectors → Data fields: maps values · Injectors translate CSV columns into typed database fields
- Data fields → Data table: persists · Normalized field values are written to SQLite tables
- Data table → Repository: served by · Repository provides higher-level database access
- Sources importer → Repository: writes through · Import execution ultimately persists into the database repository
- CSV file → Array processors: supports · CSV helpers feed row-processing utilities
- Shell exec → XLS/XLSX: invokes tools · External conversion tools are used by spreadsheet conversion
- Tests → Origins IO: covers · Tests mirror origin metadata handling
- Tests → XLS/XLSX: covers · Tests mirror spreadsheet conversion
- Tests → Sources importer: covers · Tests exercise end-to-end importing
- Tests → Data table: covers · Tests exercise database persistence behavior
