PHPackages                             budgetdumpster/hal-formatter - 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. [API Development](/categories/api)
4. /
5. budgetdumpster/hal-formatter

ActiveLibrary[API Development](/categories/api)

budgetdumpster/hal-formatter
============================

HAL Output formatting

0.2.2(7y ago)02.6k↓100%MITPHPPHP &gt;=5.5.0

Since Feb 8Pushed 7y agoCompare

[ Source](https://github.com/BD-StrategicTech/HalFormatter)[ Packagist](https://packagist.org/packages/budgetdumpster/hal-formatter)[ Docs](https://github.com/BD-StrategicTech/HalFormatter)[ RSS](/packages/budgetdumpster-hal-formatter/feed)WikiDiscussions master Synced 1mo ago

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

HalFormatter
============

[](#halformatter)

Installation
------------

[](#installation)

`composer require budgetdumpster/hal-formatter:dev-master`

Tests
-----

[](#tests)

Tests can be run using the command `phpunit` in the library root directory

Usage
-----

[](#usage)

### Configuration

[](#configuration)

If you don't have a resource that has embedded resources, the `$embedded` parameter in the `HalFormatter::formatResource`is optional. However, if you do want to incorporate embedded resources into your response, the configuration will help you build out the desired structure.

The library is based on the assumption that related resources, in most ORMs, end up in a property on the model when the relationship is retrieved. What the configuration allows you to do is move those related resources to the embedded section of the response based on the property.

So for example, you might have something like: `Model::person_id` - as the identifier for the related resource `Model::person` - the property the actual Person model is pulled into

and as a configuration you have:

```
$embedded = [
    'person' => [
        'property' => 'person',
        'key' => 'person',
        'uri' => 'person'
    ]
];
```

What this configuration tells us is - we are going to take the data in the `Model::person` property and move it to the embedded resources under the `$key` key and any links for that resource will use the value of `$uri`. The library will work with individual models as well as arrays or collections of models.

### Formatting a resource or collection

[](#formatting-a-resource-or-collection)

The most difficult part of this library is understanding the configuration (and since it's a matrix, you can tack on multiple configurations. Let's use the example configuration above and the assumption that our model will have a property on it called `person`

```
