2017 © Pedro Peláez
 

library refresolver

Resolves references to extenral files in objects

image

americanreading/refresolver

Resolves references to extenral files in objects

  • Wednesday, September 17, 2014
  • by AmericanReading
  • Repository
  • 0 Watchers
  • 0 Stars
  • 32 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

RefResolver

Use RefResolver to resolve references or "flatten" objects., (*1)

RefResolver works with any object, but is mostly likely to be useful for working with JSON, particularly JSON Schema documents and Swagger configuration files., (*2)

References

The format of the references follows the syntax used by JSON Schema. However, there is no restriction on where the reference exists inside the structure., (*3)

To create a reference, include a $ref property on an object that points to the path for a file describing an object. The path may be a local file path or an HTTP URI., (*4)

Example

Given this file at http://www.myjsonfile/cats.json, (*5)

{
    "cats": [
        {
            "name": "Molly",
            "color": "Calico"
        },
        {
            "name": "Oscar",
            "color": "Orange"
        }
    ]
}

We can include a reference to this file inside another JSON structure., (*6)

<?php
$json = <<<JSON
{
    "dog": "Bear",
    "\$ref": "http://www.myjsonfile/cats.json"
}
JSON

$obj = json_decode($json);

$resolver new RefResolver();
$resolver->resolve($obj);

After calling $resolver->resolve($obj), $obj will contain a "flattened" structure with the contents of the http://www.myjsonfile/cats.json converted to an object and augmented onto $obj., (*7)

$obj will now look like this (shown as prettyprinted JSON):, (*8)

{
    "dog": "Bear",
    "cats": [
        {
            "name": "Molly",
            "color": "Calico"
        },
        {
            "name": "Oscar",
            "color": "Orange"
        }
    ]
}

Custom resolver function

By default, the RefResolver instance will take the value of a $ref property, pass it to file_get_contents, then pass that result to json_decode, and augment the decoded object., (*9)

You may customize this behavior by passing a callable to the constructor. The callable must expect the value of the $ref property as an argument and return an object or null., (*10)

To parse the contents of the reference as XML instead of JSON, you could use a custom function like this:, (*11)

<?php
$resolverFn = function ($ref) {
    $contents = @file_get_contents($ref);
    if ($contents) {
        return simplexml_load_string($contents);
    }
    return null;
};

Caveats

There is no safeguard in place to check against circular references., (*12)

The Versions

17/09 2014

dev-master

9999999-dev

Resolves references to extenral files in objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by PJ Dietz

17/09 2014

1.0.0

1.0.0.0

Resolves references to extenral files in objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by PJ Dietz