PHPackages                             romanko/object-graph - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. romanko/object-graph

AbandonedArchivedLibrary[Parsing &amp; Serialization](/categories/parsing)

romanko/object-graph
====================

A data mapper to be used with JSON objects, which allows to shape different versions of the payload to a value object with predefined set of properties

v1.0.0(8y ago)26MITPHPPHP ^7.0

Since May 28Pushed 8y ago1 watchersCompare

[ Source](https://github.com/roman-kulish/object-graph)[ Packagist](https://packagist.org/packages/romanko/object-graph)[ Docs](https://github.com/roman-kulish/object-graph)[ RSS](/packages/romanko-object-graph/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (3)Versions (2)Used By (0)

Object Graph
============

[](#object-graph)

[![Minimum PHP Version](https://camo.githubusercontent.com/5c3072425e67297c8ef63d17acd2c86a0d2ef324f19249f2280bd7de902f63a2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e302d3838393242462e737667)](https://php.net/)[![Build Status](https://camo.githubusercontent.com/f8a18abdd0bd7482a449e00f513a2ac74fd1511c2a4adcc23824f9ee2b971b4d/68747470733a2f2f7472617669732d63692e6f72672f726f6d616e2d6b756c6973682f6f626a6563742d67726170682e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/roman-kulish/object-graph)[![Code Coverage](https://camo.githubusercontent.com/5c24feb41aaab5640594d84a06ac86f64f3c365c5f4daada16fa4de7cbc0dfae/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f6d616e2d6b756c6973682f6f626a6563742d67726170682e737667)](https://codecov.io/gh/roman-kulish/object-graph)

Introduction
------------

[](#introduction)

Object Graph wraps a plain PHP object (e.g., JSON object) and exposes a value object (a GraphNode instance) with a predefined set of properties.

Object Graph can also be used to introduce compatibility between different versions of a JSON payload and produce a GraphNode with the common set of properties which suits both payloads.

> This library a is written-from-the-ground version of a project initially developed for the NewsCorp Australia. Kudos to Juan Zapata (@juankk), Salvatore Balzano (@salvo1404) and Michael Chan (@michaelChanNews) for their time and contribution to this library.

Table of Contents
-----------------

[](#table-of-contents)

- [Walk through](#walk-through)
    - [Model](#model)
    - [Schema](#schema)
        - [Strict Schema](#strict-schema)
        - [A word on nested objects and field resolvers](#a-word-on-nested-objects-and-field-resolvers)
    - [Resolver](#resolver)
    - [Context](#context)

### Walk through

[](#walk-through)

Let's jump straight to the business. Imaging that we have two versions of a payload from our User API:

**/v1/user/123:**

```
{
  "userName": "Arnold Schwarzenegger",
  "dob": "1947-07-30",
  "emailAddress": "arnold.schwarzenegger@gov.ca.gov"
}
```

**/v2/user/123:**

```
{
  "firstName": "Arnold",
  "lastName": "Schwarzenegger",
  "dateOfBirth": "1947-07-30",
  "email": "arnold.schwarzenegger@gov.ca.gov"
}
```

Let's create a User model with this set of properties, build a Schema for each payload version and, finally, build a resolver which will automatically apply corresponding Schema to a particular payload version and produce us a User mode.

- **Model**: a instance of a GraphNode, that represents a plain PHP object, where the object fields are defined as Model properties;
- **Schema**: contains a list of Model properties / fields; resolvers, default values, PHP type casting configuration for each field and more;
- **Resolver**: performs magic and builds a Model from a plain PHP object;
- **Context**: allows to share variables between feilds resolvers.

These are the main components of the ObjectGraph library.

#### Model

[](#model)

First we start with modeling what properties our Model should have:

```
