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
- Host: GitHub
- URL: https://github.com/stratadox/restresource
- Owner: Stratadox
- License: mit
- Created: 2020-01-27T22:13:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-02T21:15:47.000Z (over 6 years ago)
- Last Synced: 2025-07-13T15:18:25.210Z (12 months ago)
- Topics: hateoas, hateoas-hal, json, rest, rest-api, restful, restful-api, xml
- Language: PHP
- Homepage:
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rest Resource
HATEOAS-compatible Restful Resource descriptions, with formatters to represent
the resources in json- or xml format.
[](https://travis-ci.org/Stratadox/RestResource)
[](https://coveralls.io/github/Stratadox/RestResource?branch=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)
);
```