Four-Phase Data Processing Library
A PHP library designed to process data through four distinct phases: casting, transformation, sanitization and validation.
What Makes it Different?
Unlike traditional validation libraries that focus solely on validation, Derafu Data Processor offers:
- Clear Phase Separation: Each processing phase (cast → transform → sanitize → validate) has its own purpose and runs in a specific order.
- Independent Transformations: A dedicated transformation layer for complex data conversions, separate from basic type casting.
- Type-Safe Operations: Strong typing and clear error handling in each phase.
- Extensible Rule System: Each type of rule (caster, transformer, sanitizer, validator) follows its own interface.
- Minimal Dependencies:
intlextension for specific format rules,Carbonfor dates, andderafu/translationfor translatable error messages (see Translations).
Features
-
🎯 Type-Safe Casting: Convert between data types safely.
- Basic types (string, int, float, bool), strict: a value that is not clearly a boolean or an integer is an error, it is not guessed.
- Date and time handling, with absolute ISO 8601 dates (a relative text like
tomorrowis an error).
-
🔄 Data Transformations: Complex data conversions.
- Base64 encoding/decoding.
- JSON processing.
- Slug generation.
- Character transliteration.
-
🧹 Rich Sanitization Rules: Clean and normalize input data.
- String sanitization (trim, strip_tags, substring, etc.).
- Regex-based cleaning (regex_remove, regex_keep).
- HTML entity handling (htmlspecialchars, htmlentities, etc.).
- Character set manipulation (remove_non_printable, remove_chars, etc.).
-
✅ Comprehensive Validation: Various validation rules.
- String validations (required, min_length, max_length, etc.).
- Number validations (range, gte, lte, etc.).
- Date validations (after, before, between, etc.).
- Format validations (email, URL, UUID, IBAN, hostname, host, etc.).
- Values of HTML inputs (color, month, week, time, datetime_local).
- File validations (file, image, mimetype).
- Array validations (min_items, max_items, contains, allowed_keys, dependent_required, unique, unique_loose, etc.).
-
🫙 Optional Fields: A
null,''or[]value isnullwithout running the rules, unless the field isrequired.
Installation
composer require derafu/data-processor
Basic Usage
use Derafu\DataProcessor\Processor;
use Derafu\DataProcessor\Registrar\DefaultRuleRegistrar;
use Derafu\DataProcessor\RuleParser;
use Derafu\DataProcessor\RuleRegistry;
use Derafu\DataProcessor\RuleResolver;
$registry = new RuleRegistry();
(new DefaultRuleRegistrar())->register($registry);
$processor = new Processor(new RuleResolver($registry), new RuleParser());
// Simple validation.
$result = $processor->process('[email protected]', [
'validate' => ['email'],
]);
// Multi-phase processing.
$result = $processor->process(' [email protected] ', [
'transform' => ['lowercase'],
'sanitize' => ['trim'],
'validate' => ['email', 'max_length:255'],
]);
// Array validation.
$result = $processor->process([1, 2, 2, 3], [
'validate' => ['unique', 'max_items:5'],
]);
// File validation.
$result = $processor->process($_FILES['upload'], [
'validate' => ['file:2M', 'mimetype:application/pdf,image/*']
]);
Dependency Injection
Applications using Symfony DependencyInjection can import the package configuration:
imports:
- { resource: '../vendor/derafu/data-processor/resources/config/data-processor-services.yaml' }
Inject Derafu\DataProcessor\Contract\ProcessorInterface into your application services.
The configured processor uses the container’s RuleResolverInterface and
RuleParserInterface. The resolver uses the same RuleRegistryInterface registered
in the container.
The registry is initialized by RuleRegistrarInterface, implemented by
CompositeRuleRegistrar. It collects services tagged with
derafu_data_processor.rule_registrar. DefaultRuleRegistrar has priority 1000;
additional registrars normally use priority 0 and run afterward, allowing them
to add rules or replace existing names. Higher priorities run first; among equal
priorities, registration order is preserved. See Custom Registrar
and Custom Parser.
The YAML supplies the processor and its rules. Configuring an application translator is a separate choice; see Translations.