An open API service indexing awesome lists of open source software.

https://github.com/stratadox/restresource

Hateoas rest resource formatting in json and xml
https://github.com/stratadox/restresource

hateoas hateoas-hal json rest rest-api restful restful-api xml

Last synced: 2 months ago
JSON representation

Hateoas rest resource formatting in json and xml

Awesome Lists containing this project

README

          

# Rest Resource
HATEOAS-compatible Restful Resource descriptions, with formatters to represent
the resources in json- or xml format.

[![Build Status](https://travis-ci.org/Stratadox/RestResource.svg?branch=master)](https://travis-ci.org/Stratadox/RestResource)
[![Coverage Status](https://coveralls.io/repos/github/Stratadox/RestResource/badge.svg?branch=master)](https://coveralls.io/github/Stratadox/RestResource?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Stratadox/RestResource/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Stratadox/RestResource/?branch=master)

## Installation
Install with `composer require stratadox/rest-resource`

## Example (json)
Resources formatted as json output:
```php
'bar'],
Links::provide(
Link::to('foo/1', Type::get('Foo'))
)
);

$this->assertJsonStringEqualsJsonString(
'{
"hateoas-resource": {
"foo": "bar",
"links": [
{
"href": "server\/foo\/1",
"rel": "Foo",
"type": "GET"
}
]
}
}',
$json->from($resource)
);
```

## Example (xml)
The same resource, now formatted as xml output:
```php
'bar'],
Links::provide(
Link::to('foo/1', Type::get('Foo'))
)
);

$this->assertXmlStringEqualsXmlString(
'

bar


server/foo/1
Foo
GET


',
$xml->from($resource)
);
```

## Example (condensed xml)
The same resource again, now formatted as xml with less verbosity:

```php
'bar'],
Links::provide(
Link::to('foo/1', Type::get('Foo'))
)
);

$this->assertXmlStringEqualsXmlString(
'




',
$xml->from($resource)
);
```

## Singularisation
Formatting an xml document based on just an array structure is slightly more
challenging than converting to json.

For example, given the input:
```php
[
'people' => [
[
'id' => 1,
'name' => 'Alice',
],
[
'id' => 2,
'name' => 'Bob',
],
]
];
```

In json, one might have output like this:
```json
{
"people": [
{
"id": 1,
"name": "Alice"
},
{
"id": 2,
"name": "Bob"
}
]
}
```

However, we'd expect from xml something in the genre of:
```xml


1
Alice


2
Bob

```
Or
```xml


```

By default, the xml formatter uses [inflection](https://github.com/ICanBoogie/Inflector)
to transform plurals into singular versions. As such, the aforementioned php
array structure would indeed produce the expected xml.
Any language supported by the inflector can be used, for example:

```php
['foo', 'bar', 'baz']],
Links::none()
);

$this->assertXmlStringEqualsXmlString(
'


foo
bar
baz

',
$xml->from($resource)
);
```

Any singularizer can be used.
If you don't wish to run the risk of using terms that cannot be transformed into
singular versions, the basic singularizer might be an option, although it would
produce xml like the following example:

```php
[
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
]],
Links::none()
);

$this->assertXmlStringEqualsXmlString(
'



1
Alice


2
Bob


',
$xml->from($resource)
);
```
Or, with less verbosity:

```php
[
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
]],
Links::none()
);

$this->assertXmlStringEqualsXmlString(
'





',
$xml->from($resource)
);
```