https://github.com/phpgt/dataobject
Structured, type-safe, immutable data transfer.
https://github.com/phpgt/dataobject
data-transfer-object dto dto-pattern immutable immutable-datastructures type-safety
Last synced: about 1 year ago
JSON representation
Structured, type-safe, immutable data transfer.
- Host: GitHub
- URL: https://github.com/phpgt/dataobject
- Owner: phpgt
- Created: 2021-01-16T17:50:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-02-14T16:47:07.000Z (about 1 year ago)
- Last Synced: 2025-04-12T06:00:02.877Z (about 1 year ago)
- Topics: data-transfer-object, dto, dto-pattern, immutable, immutable-datastructures, type-safety
- Language: PHP
- Homepage:
- Size: 109 KB
- Stars: 0
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
Structured, type-safe, immutable data transfer.
===============================================
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.
```php
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][json] as an extension to this DataObject repository.
[json]: https://php.gt/json