Download the PHP package romanko/object-graph without Composer

On this page you can find all versions of the php package romanko/object-graph. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package object-graph

Object Graph

Minimum PHP Version Build Status Code Coverage

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

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:

/v2/user/123:

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.

These are the main components of the ObjectGraph library.

Model

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

A Model class must extend GraphNode and list properties in the class header docblock. This will enabled IDE auto-suggestion as well.

Note that in this release, Model is immutable and an attempt to set or unset a value on the Model will throw an exception.

Your Model class can extend another Model class, also contain API, constants as a normal PHP class, which it really is.

Model properties can be accessed as PHP object properties $user->dateOfBirth or as array elements $article['heraldsun.com.au']->titleOverride.

Model API:

Schema

Next step, we are going to define a schema for each of the payload version. A schema class must extend Schema.

There are a few useful methods in a Schema you can or may wish to override:

Strict Schema

When a schema is strict, then you can only access Model fields defined on a Scheme instance. Trying to access a field which exists on a data source, but not defined on a schema, will give you NULL.

When transforming a Model to an array or an object, the resulting data will contain only fields defined on a strict schema. If schema is not strict then the resulting data will contain a combination of all fields defined on the source object and schema.

By default the Schema is not strict.

Version 1

Version 2

A word on nested objects and field resolvers

The first argument a field resolver function receives is always a parent object the field belongs to.

As an example, there is a source PHP object:

Each nested object can have its own Schema, which can define a custom Model class to use.

Resolver

We can atually start using the above right away:

However, it is not fun, because we still need to decide which schema to use with the data. Let's automate it:

And try now:

Context

Context is used to pass external variables to field resolvers:

Note: Context is designed to contain scalar variables and does not support deep cloning. If there is an object stored in the Context, then when Context is cloned, both cloned and original Contexts will retain references to each other.

There is a JSON with language specific greetings and in our Object Graph Model we'd like to use Australian English where it is possible:

The desired locale can be set on the Context and then accessed in the field resolver:

and within the "sayHi" field resolver:


All versions of object-graph with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
flow/jsonpath Version dev-master
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package romanko/object-graph contains the following files

Loading the files please wait ....