Download the PHP package hedgehoglab-engineering/php-declared-data without Composer
On this page you can find all versions of the php package hedgehoglab-engineering/php-declared-data. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hedgehoglab-engineering/php-declared-data
More information about hedgehoglab-engineering/php-declared-data
Files in hedgehoglab-engineering/php-declared-data
Package php-declared-data
Short Description A set of data tools for PHP.
License MIT
Informations about the package php-declared-data
PHP Declared Data
This package provides attributes, contracts, and behaviors that simplify the creation and management of data classes in PHP applications.
In particular, it enables the use of sparse objects that can be hydrated from optional data submitted via PATCH requests in a RESTful API, allowing missing or nullable fields to be handled appropriately.
Table of Contents
- Installation
- Usage
- Defining Data Classes
- Interfaces
- ResolvableData
- LenientData
- SparseData
- Traits
- ArraysData
- CollectsData
- CreatesData
- DeclaresData
- Attributes
- CollectionOf
- DateTimeFromFormat
- JsonDecode
- MapArgumentName
-
Examples
- Using Attributes in Data Classes
- Testing
- Code Formatting
- Code Analysing
- Contributing
Installation
Install the package via Composer:
Usage
Defining Data Classes
The easiest way to start to define a declared data object is to extend the AbstractDeclaredData class. Alternatively, you can compose your own using the provided contracts and traits. Then just define your properties in the constructor.
Interfaces
Interfaces define behaviors that can be implemented by data classes for different functionality.
ResolvableData
Implementing the ResolvableData interface allows a class to resolve its properties using the specified property type or a PHP 8 attribute.
LenientData
The LenientData interface allows the data object to ignore extra properties that are not defined in the class.
SparseData
The SparseData interface allows the data object to be instantiated without all required properties. The primary use case for this is transforming PATCH request data where missing properties without defaults may remain unset - i.e.: indicating that the field's value is not being modified.
Traits
Traits provide reusable functionality that can be applied to your data classes.
ArraysData
The ArraysData trait provides a toArray method to recursively convert a data instance into an array.
CollectsData
The CollectsData trait provides a static collect method which can convert an iterable value into a Illuminate\Support\Collection of instances of the defined class.
CreatesData
The CreatesData trait provides a static create method which can convert data into an instance of the defined class.
DeclaresData
The DeclaresData trait provides has, missing, only and except methods, which are useful for handling instances of declared data.
Attributes
The use of PHP attributes provides a mechanism for hinting how properties should be resolved.
CollectionOf
Transforms an array into an instance of Illuminate\Support\Collection containing instances of the specified class.
DateTimeFromFormat
Parses a date string into a DateTime object using a specified format and/or timezone.
JsonDecode
Decodes a JSON string into a PHP array or object.
MapArgumentName
Maps an input field name to a different property name in the data object.
Examples
Using Attributes in Data Classes
Here's an example of a data class using various attributes:
Testing
Code Formatting
Code Analysing
Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.