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:
DataObject
sLoad 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"),
);
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.