Download the PHP package diego-ninja/granite without Composer
On this page you can find all versions of the php package diego-ninja/granite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download diego-ninja/granite
More information about diego-ninja/granite
Files in diego-ninja/granite
Package granite
Short Description A lightweight zero-dependency PHP library for building immutable, serializable objects with validation capabilities.
License MIT
Informations about the package granite
๐ชจ Granite
A powerful, zero-dependency PHP library for building immutable, serializable objects with validation and mapping capabilities. Perfect for DTOs, Value Objects, API responses, and domain modeling.
This documentation has been generated almost in its entirety using ๐ฆ Claude 4 Sonnet based on source code analysis. Some sections may be incomplete, outdated or may contain documentation for planned or not-released features. For the most accurate information, please refer to the source code or open an issue on the package repository.
โจ Features
๐ Immutable Objects
- Read-only DTOs and Value Objects
- Thread-safe by design
- Functional programming friendly
โ Comprehensive Validation
- 25+ built-in validation rules
- Attribute-based validation (PHP 8+)
- Custom validation rules and callbacks
- Conditional and nested validation
๐ Powerful ObjectMapper
- Automatic property mapping between objects
- Convention-based mapping with multiple naming conventions
- Custom transformations and collection mapping
- Bidirectional mapping support
๐ฆ Smart Serialization
- JSON/Array serialization with custom property names
- Hide sensitive properties automatically
- DateTime and Enum handling
- Nested object serialization
โก Performance Optimized
- Reflection caching for improved performance
- Memory-efficient object creation
- Lazy loading support
๐ Quick Start
Installation
Basic Usage
๐ Documentation
Core Concepts
- Validation - Comprehensive validation system with 25+ built-in rules
- Serialization - Control how objects are converted to/from arrays and JSON
- ObjectMapper - Powerful object-to-object mapping with conventions
- Advanced Usage - Patterns for complex applications
- API Reference - Complete API documentation
Guides
- Migration Guide - Migrate from arrays, stdClass, Doctrine, Laravel
- Troubleshooting - Common issues and solutions
๐ฏ Use Cases
API Development
Domain Modeling
Object Mapping with ObjectMapper
๐ฅ Advanced Features
Convention-Based Mapping
Advanced ObjectMapper Configuration
Custom Mapping Profiles
Complex Validation
Global ObjectMapper Configuration
๐ Validation Rules
Built-in Rules
Rule | Description | Example |
---|---|---|
#[Required] |
Field must not be null | #[Required('Name is required')] |
#[Email] |
Valid email format | #[Email('Invalid email')] |
#[Min(5)] |
Minimum length/value | #[Min(5, 'Too short')] |
#[Max(100)] |
Maximum length/value | #[Max(100, 'Too long')] |
#[Regex('/pattern/')] |
Regular expression | #[Regex('/^\d+$/', 'Numbers only')] |
#[In(['a', 'b'])] |
Value in list | #[In(['active', 'inactive'])] |
#[Url] |
Valid URL format | #[Url('Invalid URL')] |
#[IpAddress] |
Valid IP address | #[IpAddress('Invalid IP')] |
#[StringType] |
Must be string | #[StringType] |
#[IntegerType] |
Must be integer | #[IntegerType] |
#[NumberType] |
Must be number | #[NumberType] |
#[BooleanType] |
Must be boolean | #[BooleanType] |
#[ArrayType] |
Must be array | #[ArrayType] |
#[EnumType] |
Valid enum value | #[EnumType(Status::class)] |
#[Each(...)] |
Validate array items | #[Each(new Email())] |
#[When(...)] |
Conditional validation | #[When($condition, $rule)] |
๐ Performance
Granite is optimized for performance with:
- Reflection caching - Class metadata cached automatically
- Mapping cache - ObjectMapper configurations cached
- Memory efficiency - Immutable objects reduce memory overhead
- Lazy loading - Load related data only when needed
- Specialized components - Refactored architecture with focused responsibilities
๐งช Testing
Granite objects are perfect for testing due to their immutability and validation:
๐ง Requirements
- PHP 8.3+ - Takes advantage of modern PHP features
- No dependencies - Zero external dependencies for maximum compatibility
๐ฆ Installation & Setup
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
๐ License
This package is open-sourced software licensed under the MIT license.
๐ Credits
This project is developed and maintained by ๐ฅท Diego Rin in his free time.
If you find this project useful, please consider:
- โญ Starring the repository
- ๐ Reporting bugs and issues
- ๐ก Suggesting new features
- ๐ง Contributing code improvements
Made with โค๏ธ for the PHP community