Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Allow returning HAL collections and entities from resources #84

Merged
merged 1 commit into from
Jul 8, 2015

Conversation

weierophinney
Copy link
Member

This patch alters RestController to allow returning ZF\Hal\Entity and/or ZF\Hal\Collection instances from resources, allowing the ability to fully craft the payload before returning.

In particular, and specific to the issue this addresses, the create() method was updated to allow returning a Collection instance (which it didn't handle before).

When an Entity or Collection is returned, they are passed to one of createHalEntity() or createHalCollection():

  • createHalEntity() returns the instance unchanged if it already has a self relational link or does not have an identifier; otherwise, it passes it to the Hal plugin's createEntity() method, which ensures that a self relational link is injected.
  • createHalCollection() passes the instance on to a new method, prepareHalCollection(), which does the following:
    • Injects a self relational link if none is present.
    • Injects the controller-specific metadata (routing information, collection name, page size, and page).

These changes will simplify batch POST operations, as well as allow any method responsible for returning an entity or collection to return a specifically crafted one without requiring also creating the response.

This patch addresses both:

This patch alters `RestController` to allow returning `ZF\Hal\Entity` and/or
`ZF\Hal\Collection` instances from resources, allowing the ability to fully
craft the payload before returning.

In particular, and specific to the issue this addresses, the `create()` method
was updated to allow returning a `Collection` instance (which it didn't handle
before).

When an Entity or Collection is returned, they are passed to one of
`createHalEntity()` or `createHalCollection()`:

- `createHalEntity()` returns the instance unchanged if it already has a `self`
  relational link or does not have an identifier; otherwise, it passes it to the
  `Hal` plugin's `createEntity()` method, which ensures that a `self` relational
  link is injected.
- `createHalCollection()` passes the instance on to a new method,
  `prepareHalCollection()`, which does the following:
  - Injects a `self` relational link if none is present.
  - Injects the controller-specific metadata (routing information, collection
    name, page size, and page).

These changes will simplify batch POST operations, as well as allow any method
responsible for returning an entity or collection to return a specifically
crafted one without requiring also creating the response.
@weierophinney weierophinney added this to the 1.1.1 milestone Jul 8, 2015
weierophinney added a commit that referenced this pull request Jul 8, 2015
@weierophinney weierophinney merged commit c53c39b into zfcampus:master Jul 8, 2015
weierophinney added a commit that referenced this pull request Jul 8, 2015
@weierophinney weierophinney deleted the hotfix/79 branch July 8, 2015 21:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant