phpgt / dataobject
Structured, type-safe, immutable data transfer.
Fund package maintenance!
phpgt
Installs: 1 533
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 3
Requires
- php: >=8.0
- ext-json: *
- phpgt/typesafegetter: ^1.3
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-12-13 12:50:50 UTC
README
A Data Transfer Objects (DTO) in a programming language is a design pattern that facilitates transfer of data between different layers of an application. This library introduces the DataObject
class which can be built from an existing associative array or standard object, using the DataObjectBuilder
class.
A DataObject
has the following features:
- It is immutable, meaning that code can't modify the data it represents
- It provides type-safe getters to the contained data
- It can be nested within other
DataObject
s - It can be converted to and from associative arrays and standard objects
Usage example
Load an object into a DataObject
, then pass to a third party library for processing.
Due to the immutability of the DataObject
class, there is no risk of the third party library making changes to the contents of the data.
use Gt\DataObject\DataObjectBuilder; // Create a new Builder and build the DataObject from an associative array. // For example, data loaded from another remote data source. $sourceData = [ "id" => 105, "name" => "Edgar Scolmore", "address" => [ "street" => "32 Trestles Lane", "town" => "Lensworth", "county" => "Scamperingshire", "postcode" => "SC41 8PN" ], ]; $builder = new DataObjectBuilder(); $data = $builder->fromAssociativeArray($sourceData); // Pass the data to a third party to process it. ThirdParty::processData($data); // Now we can use the data ourselves for whatever purpose: Database::store( id: $data->getInt("id"), refname: $data->getString("name"), );
Working with JSON data
A JSON data structure is almost identical in scope to the DataObject introduced in this repository, with one key difference: JSON data can represent a primitive data type, not always key-value-pairs. Because of this, PHP.Gt/Json is maintained separately to provide structured, type-safe, immutable JSON objects as an extension to this DataObject repository.