2017 © Pedro Peláez
 

library dto

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

image

dto/dto

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  • Thursday, April 19, 2018
  • by fireproofsocks
  • Repository
  • 3 Watchers
  • 28 Stars
  • 13,098 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 5 Open issues
  • 23 Versions
  • 31 % Grown

The README.md

Data Transfer Object (DTO)

Build Status codecov, (*1)

This package provides a quick way to define structured objects (DTOs) using the JSON Schema standard., (*2)

A Data Transfer Object (DTO) is an object used to pass typed data between layers in your application, similiar in concept to Structs in C, Martin Fowler's Transfer Objects, or Value Objects., (*3)

DTOs are a helpful counterpart to the Data Accessor Object (DAO) or Repository patterns., (*4)

The functionality is similar to what is offered by the shape package., (*5)

Example

Define a PHP object using JSON Schema syntax:, (*6)

<?php
class ExampleObject extends Dto\Dto
{
    protected $schema = [
        'type' => 'object',
        'properties' => [
            'a' => ['type' => 'string'],
            'b' => ['type' => 'integer']
        ],
        'additionalProperties' => false
    ];
}

Now you can instantiate and use your object:, (*7)

<?php
// Assume you have included vendor/autoload.php and the above ExampleObject class
$obj = new ExampleObject();
$obj->a = "Some String";
$obj->b = 123; // this is defined as an integer, so values WILL be type-cast to integer!
$obj->c = 'Whoops, this will throw an Exception because additionalProperties are not allowed';

Or reference a JSON Schema directly:, (*8)

<?php
class ExampleObject extends Dto\Dto
{
    protected $schema = [
        '$ref' => 'http://example.com/some/schema.json'
    ];
}

Possible Uses

  • In APIs: Consume a JSON Schema API at runtime without needing to parse data formats.
  • In Views: Instead of passing your view layer just any array, you can define a DTO and use type-hinting to ensure that your views will always have the data attributes they need.
  • For Caching: Instead of relying on your own haphazard convention, you can store and retrieve a specific DTO class from cache without having to guess which attributes or array keys are available.
  • Service Classes: when your service class expects to operate on a specific type of data.
  • Result Sets: instead of returning an array of stdClass objects or associative arrays from a database lookup, a DTO can describe the result set as well as the individual records.

Read more in the DTO Wiki, (*9)

TODO:

  • property dependencies https://spacetelescope.github.io/understanding-json-schema/reference/object.html
  • schema dependencies (extend the schema)
  • room for other versions JSON Schema (v5 is coming!)

Version History

3.2.7

  • Fixed validation issue when PHP classes were referenced in schemas using the $ref keyword.

3.2.6

  • Removed unused dependency on webmozart/json

3.2.4

  • Performance boost by eliminating redundant validation.

3.2.3

  • strval() conversion inside __toString() method: makes for easier UX when checking object properties.

3.2.2

  • Support for nested relative schema paths when reading JSON schemas.

3.2.1

  • Tests added for serialize method.

3.2.0

  • Includes the $baseDir and getBaseDir() methods in order to support relative paths to JSON schemas as used by the $ref keyword.

3.1.0

  • Includes proper support for in-line definitions and inheritance of them.

3.0.0

  • Integration of the JSON Schema 4 specification to drive all structure and type definitions.

2.0.0

We don't speak of version 2., (*10)

1.0.8

  • Fixes for anonymous hashes of DTOs injected into the constructor.
  • Corrected namespaces in tests.

1.0.7

Fixes for arrays of DTOs injected into the constructor as arrays., (*11)

1.0.6

Fixes for hierarchical data injected into the constructor., (*12)

1.0.5

Initial release with some grooming as documentation was added., (*13)

The Versions

19/04 2018

dev-master

9999999-dev

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

19/04 2018

3.2.7

3.2.7.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

19/04 2018

dev-bugfix/issue-13-dto-references

dev-bugfix/issue-13-dto-references

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

21/06 2017

3.2.6

3.2.6.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

21/06 2017

3.2.5

3.2.5.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2017

3.2.4

3.2.4.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

18/04 2017

3.2.3

3.2.3.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

18/04 2017

3.2.2

3.2.2.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

24/03 2017

3.2.1

3.2.1.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

24/03 2017

3.2.0

3.2.0.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

03/03 2017

3.1.0

3.1.0.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

22/02 2017

v3.x-dev

3.9999999.9999999.9999999-dev

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

22/02 2017

3.0.0

3.0.0.0

Use the JSON Schema spec to define Data Transfer Objects (DTOs)

  Sources   Download

MIT

The Requires

 

The Development Requires

20/01 2017

v2.x-dev

2.9999999.9999999.9999999-dev

Data Transfer Object: define object structures using the JSON Schema spec to hold structured data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

14/12 2016

1.0.8

1.0.8.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

06/12 2016

1.0.7

1.0.7.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

06/12 2016

1.0.6

1.0.6.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

05/10 2016

1.0.5

1.0.5.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

02/10 2016

1.0.3

1.0.3.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

02/10 2016

1.0.4

1.0.4.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

27/09 2016

1.0.2

1.0.2.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

24/09 2016

1.0.1

1.0.1.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

30/08 2016

1.0.0

1.0.0.0

Data Transfer Object: quickly create structured objects to hold loosely typed data

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires