Architecture
This document provides an overview of the architecture and design principles behind Derafu Spreadsheet.
Core Components
Derafu Spreadsheet is built around several key components that work together to provide a unified approach to spreadsheet processing:
Key Components
-
Loader
- Handles loading spreadsheet data from files or strings.
- Uses Factory to create appropriate Format Handlers.
- Uses Caster to convert raw data to appropriate PHP types.
-
Dumper
- Handles saving spreadsheet data to files or strings.
- Uses Factory to create appropriate Format Handlers.
- Uses Caster to convert PHP types to format-appropriate representations.
-
Factory
- Creates and manages Format Handlers based on file extension or specified format.
- Allows registering custom Format Handlers.
- Provides format detection capabilities.
-
Caster
- Converts raw data values to appropriate PHP types when reading.
- Converts PHP types to appropriate string representations when writing.
- Handles intelligent date, boolean, numeric, and JSON detection.
-
Format Handlers
- Format-specific implementations that know how to read/write a particular format.
- All implement a common FormatHandlerInterface.
- Includes handlers for XLSX, XLS, CSV, ODS, JSON, XML, YAML, HTML, PDF.
-
Data Model
- Spreadsheet: Top-level container for all data.
- Sheet: Container for rows and cells with a name.
- Both implement interfaces for consistent interaction.
Design Principles
Derafu Spreadsheet was designed with several key principles in mind:
-
Unified API
- Consistent interface across all supported formats.
- Same code works regardless of source or target format.
-
Type Intelligence
- Automatic conversion between string values and appropriate PHP types.
- No manual type casting required in application code.
-
Separation of Concerns
- Format handling is separated from data model.
- Type conversion is separated from data loading/saving.
- Each component has a single, clear responsibility.
-
Interface-Based Design
- All components define and implement interfaces.
- Allows for custom implementations and extensions.
-
Minimal Dependencies
- Core functionality has minimal dependencies.
- Format-specific dependencies only required for formats you use.
Data Flow
When working with Derafu Spreadsheet, data flows through the components as follows:
Loading Process:
- Loader receives a file or string.
- Factory creates appropriate Format Handler based on format.
- Format Handler reads raw data into internal Spreadsheet structure.
- Caster converts raw values to appropriate PHP types.
- Typed Spreadsheet object is returned to application.
Saving Process:
- Dumper receives a Spreadsheet object and target format.
- Caster converts PHP values to appropriate string representations.
- Factory creates appropriate Format Handler for target format.
- Format Handler writes structured data to file or string.
- File path or string content is returned to application.
Extensibility
Derafu Spreadsheet is designed to be extensible:
- Custom Format Handlers: Create handlers for proprietary or custom formats.
- Custom Casters: Implement specialized type conversion logic.
- HTTP Integration: Generate responses with downloadable spreadsheets.
- Framework Integration: Easy to integrate with popular PHP frameworks.
Directory Structure
src/
├── Abstract/
│ └── AbstractPhpSpreadsheetFormatHandler.php
├── Contract/
│ ├── SpreadsheetCasterInterface.php
│ ├── SpreadsheetDumperInterface.php
│ ├── FactoryInterface.php
│ ├── FormatHandlerInterface.php
│ ├── SpreadsheetLoaderInterface.php
│ ├── SheetInterface.php
│ ├── SpreadsheetInterface.php
│ └── Http/
│ └── SpreadsheetHttpResponseGeneratorInterface.php
├── Exception/
│ ├── SpreadsheetDumpException.php
│ ├── SpreadsheetFileNotFoundException.php
│ ├── SpreadsheetFormatNotSupportedException.php
│ └── SpreadsheetLoadException.php
├── Format/
│ ├── CsvLeagueHandler.php
│ ├── CsvPhpSpreadsheetHandler.php
│ ├── HtmlHandler.php
│ ├── JsonHandler.php
│ ├── OdsHandler.php
│ ├── PdfHandler.php
│ ├── XlsHandler.php
│ ├── XlsxHandler.php
│ ├── XmlHandler.php
│ └── YamlHandler.php
├── Http/
│ └── NyholmSpreadsheetHttpResponseGenerator.php
├── Caster.php
├── Dumper.php
├── Factory.php
├── Loader.php
├── Sheet.php
└── Spreadsheet.php
On this page
Last updated on 27/07/2025
by Anonymous