Skip to content

Commit

Permalink
add patchUpdateRelation to send a patch request to the server, rather…
Browse files Browse the repository at this point in the history
… than the put normally sent. Fixes #4

Add `self` as the return value of AbstractAdapter::setClient() Fixes #6
  • Loading branch information
Jmeyering committed Aug 30, 2016
1 parent 887ad9c commit 4231187
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 1.3.0
* Add the `patchUpdateRelation` method to the explorer to support partial object
updates.
* Update `AbstractAdapter::setClient()` to return self.

## 1.2.2
* Update the explorer to handle links that are templated but do not contain the
`templated` property.
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ used internally must return PSR7 Message interfaces.
To use the exploration feature of the library we need to think about our
responses and their included `_links` as objects and relationships.

Fetching, Creating, Updating, and Deleting are the primary actions to perform on
a related object. HalExplorer exposes this functionality with the
`getRelation`, `createRelation`, `updateRelation`, and `deleteRelation` methods.
Fetching, Creating, Updating, and Deleting are the primary actions to perform
on a related object. HalExplorer exposes this functionality with the
`getRelation`, `createRelation`, `updateRelation`, `patchUpdateRelation` and
`deleteRelation` methods.

As expected, these methods map to `GET`, `POST`, `PUT`, and `DELETE` HTTP
methods.
As expected, these methods map to the `GET`, `POST`, `PUT`, `PATCH` and
`DELETE` HTTP verbs.

```php
$explorer->createRelation($object, "association");
Expand Down
17 changes: 9 additions & 8 deletions docs/api-exploration.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Api Exploration

HalExplorer is able to traverse links that exist on a PSR-7 Response. HalExplorer
expects the response body to be correctly formatted according to the
[HAL draft][0]. Malformed response bodies will not be traversed correctly.
HalExplorer is able to traverse links that exist on a PSR-7 Response.
HalExplorer expects the response body to be correctly formatted according to
the [HAL draft][0]. Malformed response bodies will not be traversed correctly.

To use the exploration feature of the library we need to think about our
responses and their included `_links`, and `_embedded` as objects and their
relationships.

Fetching, Creating, Updating, and Deleting are the primary actions to perform on
a related object. HalExplorer exposes this functionality with the
`getRelation`, `createRelation`, `updateRelation`, and `deleteRelation` methods.
Fetching, Creating, Updating, and Deleting are the primary actions to perform
on a related object. HalExplorer exposes this functionality with the
`getRelation`, `createRelation`, `updateRelation`, `patchUpdateRelation` and
`deleteRelation` methods.

As expected, these methods map to `GET`, `POST`, `PUT`, and `DELETE` HTTP
methods.
As expected, these methods map to `GET`, `POST`, `PUT`, `PATCH` and `DELETE`
HTTP verbs.

For example, given the following response:

Expand Down
16 changes: 16 additions & 0 deletions spec/ExplorerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ function it_should_be_able_to_update_a_relation_identified_by_a_link(
$this->updateRelation($response, "relation");
}

function it_should_be_able_to_update_a_relation_identified_by_a_link_using_patch(
AdapterInterface $adapter,
ResponseInterface $response
)
{
$this->setBaseUrl($this->baseUrl);;

$this->setAdapter($adapter);
$adapter->patch($this->baseUrl . "/relationship/456", Argument::type("array"))
->shouldBeCalled();

$response->getBody()->willReturn(file_get_contents(__DIR__ . "/fixtures/halResponse.json"));

$this->patchUpdateRelation($response, "relation");
}

function it_should_be_able_to_delete_a_relation_identified_by_a_link(
AdapterInterface $adapter,
ResponseInterface $response
Expand Down
4 changes: 4 additions & 0 deletions src/ClientAdapters/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ abstract class AbstractAdapter
* Sets the concrete client onto the adapter
*
* @param mixed $client
*
* @return self
*/
public function setClient($client)
{
$this->client = $client;

return $this;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/ClientAdapters/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface AdapterInterface
*
* @param mixed
*
* @return void
* @return self
*/
public function setClient($client);

Expand Down Expand Up @@ -70,6 +70,16 @@ public function post($url, array $options = []);
*/
public function put($uri, array $options = []);

/**
* Make a patch request
*
* @param string $uri The uri that the client will hit
* @param array $options All options that should be handled by the client.
*
* @return ResponseInterface
*/
public function patch($uri, array $options = []);

/**
* Make a delete request
*
Expand Down
16 changes: 16 additions & 0 deletions src/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ public function updateRelation(ResponseInterface $response, $id, array $options
return $this->followLink("put", $response, $id, $options);
}

/**
* Follow a link using the "PATCH" method.
*
* @see followLink
*
* @param ResponseInterface $response The response object containing the links
* @param string $id The identifier of the link to follow
* @param array $options Any options to be passed to the adapter
*
* @return ResponseInterface
*/
public function patchUpdateRelation(ResponseInterface $response, $id, array $options = [])
{
return $this->followLink("patch", $response, $id, $options);
}

/**
* Follow a link using the "DELETE" method.
*
Expand Down

0 comments on commit 4231187

Please sign in to comment.