2017 © Pedro Peláez
 

library jsonmapping

Library for mapping PHP objects to JSON structures

image

mittwald/jsonmapping

Library for mapping PHP objects to JSON structures

  • Wednesday, May 23, 2018
  • by mittwald-typo3
  • Repository
  • 5 Watchers
  • 4 Stars
  • 4,265 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

Object-to-JSON mapping framework for PHP

Build Status, (*1)

This package contains a framework for mapping PHP objects into arbitrary JSON structures., (*2)

Installation

Install this package via Composer:, (*3)

$ composer require mittwald/php-jsonmapping

Usage

Mapping objects

The basic interface provided by this package is the interface Mw\JsonMapping\MappingInterface. It models a basic mapping from one value to another., (*4)

interface MappingInterface
{
  public funcion map($value)
}

The most powerful implementation of this interface is the Mw\JsonMapping\ObjectMapping. The ObjectMapping is used to map PHP objects into an array structure (which can then be used for JSON serialization):, (*5)

$customerMapping = new ObjectMapping([
  'customerNumber' => new ObjectGetterMapping('getCustomernumber'),
  'firstName' => new ObjectGetterMapping('getFirstName')
]);
$customerJson = $customerMapping->map($customer);

Alternatively, use the Mw\JsonMapping\MappingBuilder for more concise expressions:, (*6)

$m = new MappingBuilder();

$customerMapping = $m->struct([
  'customerNumber' => $m->getter('getCustomernumber'),
  'firstName'      => $m->getter('getFirstName'),
]);

On the first glance, this code is similar to the following, (*7)

$customerJson = [
  'customerNumber' => $customer->getCustomernumber(),
  'firstName'      => $customer->getFirstName()
];

However, the ObjectMapping does more than simply calling getter methods and building an array from them. The ObjectMapping also handles null objects or getter methods not being available. So all in all, the following code is a much better equivalent for the example:, (*8)

$customerJson = $customer != null ? [
  'customerNumber' => is_callable([$customer, 'getCustomernumber']) ? $customer->getCustomernumber() : null,
  'firstName' => is_callable([$customer, 'getFirstName']) ? $customer->getFirstName() : null,
] : null;

Chaining mappings

Mappings can also be chained together:, (*9)

$customerMapping = new ObjectMapping([
  'customerNumber' => (new ObjectGetterMapping('getCustomernumber'))->then(new IntegerMapping()),
]);

This can also be used to map sub-objects:, (*10)

$customerMapping = new ObjectMapping([
  'customerNumber' => (new ObjectGetterMapping('getCustomernumber'))->then(new IntegerMapping()),
  'address'        => (new ObjectGetterMapping('getAddress'))->then(new ObjectMapping([
    'street'      => new ObjectGetterMapping('getAddress'),
    'housenumber' => new ObjectGetterMapping('getHouseNumber'),
    'country'     => new ObjectGetterMapping('getCountry')
  ]))
]);

Alternatively, using the MappingBuilder:, (*11)

$customerMapping = $m->struct([
  'customerNumber' => $m->getter('getCustomernumber')->then($m->toInteger()),
  'address'        => $m->getterAndStruct('getAddress', [
    'street'      => $m->getter('getAddress'),
    'housenumber' => $m->getter('getHouseNumber'),
    'country'     => $m->getter('getCountry')
  ])
]);

Filtering

Object mappings can be filtered for specific properties:, (*12)

$customerMapping = new ObjectMapping([
  'customerNumber' => new ObjectGetterMapping('getCustomernumber'),
  'firstName'      => new ObjectGetterMapping('getFirstName')
]);

$filteredCustomerMapping = $customerMapping->filter('firstName');

Filters can also be nested, using the FilterSet class:, (*13)

$filter = new FilterSet(
  'customerNumber',
  'address.country'
);

$customerMapping = $m->struct([
  'customerNumber' => $m->getter('getCustomernumber')->then($m->toInteger()),
  'address'        => $m->getterAndStruct('getAddress', [
    'street'      => $m->getter('getAddress'),
    'housenumber' => $m->getter('getHouseNumber'),
    'country'     => $m->getter('getCountry')
  ])->filter($filter->subFilter('address'))
])->filter($filter);

Merging

Also, object mappings can be merged together:, (*14)

$customerMapping = new ObjectMapping([
  'customerNumber' => new ObjectGetterMapping('getCustomernumber'),
  'firstName'      => new ObjectGetterMapping('getFirstName')
]);

$advancedCustomerMapping = new ObjectMapping([
  'address' => (new ObjectGetterMapping('getAddress'))->then(new ObjectMapping([
    'street'      => new ObjectGetterMapping('getAddress'),
    'housenumber' => new ObjectGetterMapping('getHouseNumber'),
    'country'     => new ObjectGetterMapping('getCountry')
  ]))
]);

$mergedCustomerMapping = $customerMapping->merge($advancedCustomerMapping);
$mergedCustomerJson = $mergedCustomerMapping->map($customer);

Putting it all together

Find a complete example of all available mappings below; also, the examples/ folder contains more examples:., (*15)

$m = new MappingBuilder();
$customerMapping = $m->struct([
  'customerNumber' => $m->getter('getCustomernumber')->then($m->toInteger()),
  'firstName'      => $m->getter('getFirstName'),
  'lastName'       => $m->getter('getLastName'),
  'invoices'       => $m->getter('getInvoices')->then($m->listing($m->struct([
    'invoiceNumber' => $m->getter('getInvoiceNumber')->then($m->toInteger()),
    'price'         => $m->getter('getPrice')->then($m->toInteger())
  ])))
]);

$addressCustomerMapping = $m->struct([
  'address' => $m->getterAndStruct('getAddress', [
    'street'      => $m->getter('getAddress'),
    'housenumber' => $m->getter('getHouseNumber'),
    'country'     => $m->getter('getCountry')
  ])
]);

$customerJson = $customerMapping
  ->merge($addressCustomerMapping)
  ->filter($userFilter)
  ->map($customer);

The Versions

23/05 2018

dev-master

9999999-dev

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

23/05 2018

v1.4.0

1.4.0.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

21/02 2018

v1.3.2

1.3.2.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

19/07 2016

v1.3.1

1.3.1.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

02/03 2016

v1.3.0

1.3.0.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

01/03 2016

v1.2.1

1.2.1.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

29/02 2016

v1.2.0

1.2.0.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

29/02 2016

v1.1.0

1.1.0.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann

23/02 2016

v1.0.0

1.0.0.0

Library for mapping PHP objects to JSON structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sebastian Gieselmann