Import and Export
The OpenRegister application supports importing and exporting data in multiple formats:
- JSON for configuration data
- Excel (.xlsx) for bulk data
- CSV for simple data
Import
The import functionality is handled by the ImportService class, which provides methods for different file formats.
Import Methods
JSON Import
public function importFromJson(string $jsonData, ?Register $register = null, ?Schema $schema = null): array
Excel Import
public function importFromExcel(string $filePath, ?Register $register = null, ?Schema $schema = null): array
CSV Import
public function importFromCsv(string $filePath, ?Register $register = null, ?Schema $schema = null): array
Import Parameters
filePath(string): Path to the file to importregister(Register|null): Optional register to associate with imported objectsschema(Schema|null): Optional schema to associate with imported objects
File Format Specifications
JSON Format
The import JSON should follow this structure:
{
"schemas": [
{
"name": "string",
"description": "string",
"fields": [
{
"name": "string",
"type": "string",
"required": boolean,
"description": "string"
}
],
"metadata": {
// Additional schema metadata
}
}
],
"objects": [
{
"name": "string",
"schema": "string",
"data": {
// Object data matching schema fields
},
"metadata": {
// Additional object metadata
}
}
]
}
Excel Format
Excel files should follow these guidelines:
- First row contains headers matching schema properties
- Each subsequent row represents an object
- Multiple sheets can be used for different schemas
- Sheet names should match schema names
CSV Format
CSV files should follow these guidelines:
- First row contains headers matching schema properties
- Each subsequent row represents an object
- Uses comma as delimiter
- Uses double quotes for text fields
Import Process
-
File Validation
- Validates file format and structure
- Checks for required fields
- Verifies schema references
-
Data Processing
- Reads data from file
- Maps fields to schema properties
- Validates data types and constraints
-
Object Creation/Update
- Creates new objects or updates existing ones
- Maintains object relationships
- Preserves metadata
Response Format
The import operation returns an array with the following structure:
{
"message": "Import successful",
"summary": {
"created": [ { "action": "created", "timestamp": "...", ... } ], // Log entries for newly created objects
"updated": [ { "action": "updated", "timestamp": "...", ... } ], // Log entries for updated objects
"unchanged": [ { "action": "unchanged", "timestamp": "...", ... } ] // Log entries for unchanged objects
}
}
Each entry in "created", "updated", and "unchanged" is a log array (see "lastLog" property on ObjectEntity). The "lastLog" property is set at runtime and is not persisted in the database.
Error Handling
The import process includes strict validation and will throw exceptions in the following cases:
JsonException: When the provided JSON data is invalidInvalidArgumentException: When the file structure is invalid or required fields are missingRuntimeException: When file processing fails
Export
The export functionality is handled by the ExportService class, which provides methods for different file formats.
Export Methods
JSON Export
public function exportToJson(Register $register, bool $includeObjects = false): string
Excel Export
public function exportToExcel(Register $register, bool $includeObjects = false): Spreadsheet
CSV Export
public function exportToCsv(Register $register, bool $includeObjects = false): string
Export Parameters
register(Register): The register to exportincludeObjects(boolean): Whether to include objects in the export
Export Process
-
Data Collection
- Retrieves register data
- Optionally includes associated objects
- Gathers metadata and relationships
-
Format Conversion
- Converts data to target format
- Maintains data structure
- Preserves relationships
-
File Generation
- Creates file in requested format
- Includes all necessary data
- Maintains data integrity
Error Handling
The export process will throw exceptions in the following cases:
RuntimeException: When file generation failsInvalidArgumentException: When invalid parameters are provided
Best Practices
-
Data Validation
- Validate data before import
- Check file format and structure
- Verify schema compatibility
-
Error Handling
- Implement proper error handling
- Provide meaningful error messages
- Log import/export operations
-
Performance
- Use batch processing for large files
- Implement proper memory management
- Consider using streaming for large exports
-
Security
- Validate file types
- Sanitize input data
- Implement proper access control