Skip to content

[Release] KaririCode Transformer v1.0.0 🚀 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 26, 2024
Merged

[Release] KaririCode Transformer v1.0.0 🚀 #1

merged 8 commits into from
Oct 26, 2024

Conversation

walmir-silva
Copy link
Contributor

Overview

This PR introduces the first stable release of KaririCode Transformer, a robust and flexible data transformation component for PHP applications. The component provides a comprehensive set of transformers for common data manipulation needs, with a focus on type safety and ease of use.

What's Included

Core Components

  1. Base Infrastructure

    • Transform attribute for property-based transformations
    • Transformer service with registry integration
    • Abstract transformer processor with validation support
    • Comprehensive error handling and reporting
  2. String Transformers

    • CaseTransformer (camel, snake, pascal, kebab)
    • MaskTransformer (phone, CPF, CNPJ)
    • SlugTransformer (URL-friendly strings)
    • TemplateTransformer (variable substitution)
  3. Data Transformers

    • DateTransformer (format and timezone conversion)
    • NumberTransformer (locale-aware formatting)
    • JsonTransformer (encoding/decoding)
  4. Array Transformers

    • ArrayFlattenTransformer (nested array flattening)
    • ArrayGroupTransformer (key-based grouping)
    • ArrayKeyTransformer (key case transformation)
    • ArrayMapTransformer (key mapping)
  5. Composite Transformers

    • ChainTransformer (sequential transformations)
    • ConditionalTransformer (conditional processing)

Documentation

  • Comprehensive README in English and Brazilian Portuguese
  • Detailed usage examples with output samples
  • Configuration guides for each transformer
  • Real-world application examples
  • Integration guides with other KaririCode components

Tests

  • Unit tests for all transformers
  • Integration tests for complex scenarios
  • Type validation tests
  • Error handling tests

Technical Details

Dependencies

  • PHP 8.3+
  • ext-mbstring
  • ext-json
  • kariricode/contract
  • kariricode/processor-pipeline
  • kariricode/property-inspector

Breaking Changes

  • N/A (Initial Release)

Type Safety

  • Strict type declarations throughout
  • Property type validation
  • Return type validation
  • Configuration option validation

Testing Instructions

  1. Install dependencies:
composer install
  1. Run test suite:
make test
  1. Check test coverage:
make coverage

Example Usage

class DataFormatter
{
    #[Transform(
        processors: ['date' => ['inputFormat' => 'd/m/Y', 'outputFormat' => 'Y-m-d']]
    )]
    private string $date = '25/12/2024';

    #[Transform(
        processors: ['number' => ['decimals' => 2, 'decimalPoint' => ',', 'thousandsSeparator' => '.']]
    )]
    private float $price = 1234.56;

    #[Transform(
        processors: ['mask' => ['type' => 'phone']]
    )]
    private string $phone = '11999887766';
}

- Created initial project directory structure with placeholder files for core components
- Added `TransformationResult.php` under `src/Result` for result handling.
- Added traits for reusable transformation functions
- Created `src/Transformer.php` as the main transformer entry point.

These files and folders establish the foundation for the project’s structure without implementation details.
…rmer

- Implemented `Transform` attribute in `src/Attribute/Transform.php`:
  - Extends `BaseProcessorAttribute` to mark properties for transformation processing.

- Defined `TransformationResult` interface in `src/Contract/TransformationResult.php`:
  - Provides contract methods for result validation, error handling, and transformed data retrieval.

- Created `TransformerException` in `src/Exception/TransformerException.php`:
  - Extends `AbstractException` to handle transformer-specific errors.
  - Added methods `invalidInput`, `invalidFormat`, and `invalidType` with detailed error codes and messages for input type, format, and type validation.
…ality

- Updated `AbstractTransformerProcessor` with validation logic:
  - Added `isValid` and `getErrorKey` methods for validation handling.
  - Included `guardAgainstInvalidType` method to enforce input type checks.

- Enhanced array processors:
  - `ArrayFlattenTransformer`: added configurable `depth` and `separator` for flattening nested arrays.
  - `ArrayGroupTransformer`: added grouping logic with `groupBy` and `preserveKeys` options.
  - `ArrayKeyTransformer`: supports transforming array keys to different cases (`snake`, `camel`, `pascal`, `kebab`) with optional recursion.
  - `ArrayMapTransformer`: introduced `mapping`, `removeUnmapped`, and `recursive` options for flexible array mapping.

- Extended `ArrayTransformerTrait` with case transformation methods:
  - Added `toCamelCase`, `toPascalCase`, `toSnakeCase`, and `toKebabCase` for consistent key formatting.

- Updated `StringTransformerTrait` with additional string manipulation methods:
  - Added transformations for `toLowerCase`, `toUpperCase`, `toTitleCase`, `toSentenceCase`, and various case conversions.
…unctionality

- Updated `DateTransformer`:
  - Added `inputFormat`, `outputFormat`, `inputTimezone`, and `outputTimezone` options for flexible date transformation.

- Enhanced `JsonTransformer`:
  - Configurable options `assoc`, `depth`, and `encodeOptions` for JSON encoding and decoding with error handling.

- Improved `NumberTransformer`:
  - Added options for decimal control, multipliers, rounding, and separators for number formatting.

- Updated string processors:
  - `CaseTransformer`: Supports multiple case transformations, including lower, upper, title, sentence, camel, pascal, snake, and kebab cases.
  - `MaskTransformer`: Allows masking strings with custom patterns and pre-defined types (e.g., phone, CPF, CNPJ).
  - `SlugTransformer`: Generates slugs with custom separators, lowercase option, and locale-based transliteration.
  - `TemplateTransformer`: Template replacement with configurable placeholders and support for missing value handlers.

- Updated `TransformationResult`:
  - Implements `TransformationResult` contract with methods for validation, error retrieval, and transformed data handling.
- Added `Transformer` contract in `src/Contract/Transformer.php`:
  - Defines the core interface for all transformers, ensuring consistency across implementations.

- Updated `ChainTransformer`:
  - Enhanced chaining mechanism for sequential processing of transformations.
  - Improved error handling and data validation during transformation chain execution.

- Improved `ConditionalTransformer`:
  - Added conditional checks to apply transformations based on configurable criteria.
  - Enhanced logic for validating conditions before executing transformations.

- Modified main `Transformer` class in `src/Transformer.php`:
  - Integrated `Transformer` contract to standardize transformation behavior.
  - Streamlined method signatures and added additional validation for consistent data processing.
- Updated `ConditionalTransformer`:
- Updated `TemplateTransformer`
This commit demonstrates the usage of various transformers in the KaririCode Framework.
The demo showcases different transformation capabilities:

- Data Formats:
  * Date format conversion (d/m/Y to Y-m-d)
  * Number formatting with locale-specific separators
  * JSON handling with pretty print

- String Manipulation:
  * Phone number masking ((##) #####-####)
  * Case transformations (camelCase, snake_case)
  * URL slug generation
  * Template rendering with variable substitution

- Array Operations:
  * Array key case transformation
  * Array flattening with dot notation
  * Array grouping by field
…uguese

This commit adds detailed documentation for the KaririCode Transformer component in both English and Brazilian Portuguese. The documentation includes:

- Complete usage examples with output samples
- Detailed configuration options for each transformer
- Real-world examples with practical applications
- Code samples for common transformation scenarios

Documentation highlights:
- String transformation (case, mask, slug)
- Data formatting (dates, numbers, currency)
- Array manipulation (flattening, grouping, key mapping)
- Template processing with examples
- Integration examples with other KaririCode components

The documentation is structured to serve both international and Brazilian developers, with:
- Localized examples
- Regional formatting patterns
- Culturally appropriate use cases

Files added:
- README.md (English version)
- README.pt-BR.md (Portuguese version)
@walmir-silva walmir-silva merged commit 2747ecb into main Oct 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant