Skip to content

Commit 35b2eaa

Browse files
committed
patch support
1 parent 37a0b7a commit 35b2eaa

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/Adapters/BaseEntityAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract protected function getPayloadForEntity($entity, $resultList = false);
1616

1717
abstract protected function getEntityForPayload($payload, $id = null);
1818

19+
abstract protected function updateEntityWithPayload($entity, $payload);
20+
1921
abstract protected function storeEntity($entity);
2022

2123
abstract protected function getEntityList(
@@ -59,6 +61,14 @@ final public function get(Request $request, Response $response, $id): Response
5961
return $response->withJson($this->getPayloadForEntity($this->getEntityForId($id)));
6062
}
6163

64+
final public function patch(Request $request, Response $response, $id): Response
65+
{
66+
$entity = $this->getEntityForId($id);
67+
$this->updateEntityWithPayload($entity, $request->getParsedBody());
68+
$this->storeEntity($entity);
69+
return $response->withStatus(204, 'No Content');
70+
}
71+
6272
final public function put(Request $request, Response $response, $id): Response
6373
{
6474
$entity = $this->getEntityForPayload($request->getParsedBody(), $id);

src/Adapters/EntityAdapterRouterFactory.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class EntityAdapterRouterFactory
1010
const ITEM_GET = 2;
1111
const ITEM_POST = 4;
1212
const ITEM_PUT = 8;
13-
const ITEM_DELETE = 16;
14-
const ALL = 31;
13+
const ITEM_PATCH = 16;
14+
const ITEM_DELETE = 32;
15+
const ALL = 63;
1516

1617
/**
1718
* @param App $app
@@ -28,14 +29,30 @@ public static function crud(
2829
): callable {
2930
return function () use ($entityAdapter, $app, $allowed, $additional) {
3031
$entityAdapter = new $entityAdapter();
31-
$allowed & self::LIST && $app->get('/', self::entityAdapterHandler($entityAdapter, 'list'));
32-
$allowed & self::ITEM_POST && $app->post('/', self::entityAdapterHandler($entityAdapter, 'post'));
33-
$allowed & self::ITEM_GET
34-
&& $app->get('/{id}/', self::entityAdapterWithRouteIDHandler($entityAdapter, 'get'));
35-
$allowed & self::ITEM_PUT
36-
&& $app->put('/{id}/', self::entityAdapterWithRouteIDHandler($entityAdapter, 'put'));
37-
$allowed & self::ITEM_DELETE
38-
&& $app->delete('/{id}/', self::entityAdapterWithRouteIDHandler($entityAdapter, 'delete'));
32+
$allowed & self::LIST && $app->get(
33+
'/',
34+
self::entityAdapterHandler($entityAdapter, 'list')
35+
);
36+
$allowed & self::ITEM_POST && $app->post(
37+
'/',
38+
self::entityAdapterHandler($entityAdapter, 'post')
39+
);
40+
$allowed & self::ITEM_GET && $app->get(
41+
'/{id}/',
42+
self::entityAdapterWithRouteIDHandler($entityAdapter, 'get')
43+
);
44+
$allowed & self::ITEM_PUT && $app->put(
45+
'/{id}/',
46+
self::entityAdapterWithRouteIDHandler($entityAdapter, 'put')
47+
);
48+
$allowed & self::ITEM_PATCH && $app->patch(
49+
'/{id}/',
50+
self::entityAdapterWithRouteIDHandler($entityAdapter, 'patch')
51+
);
52+
$allowed & self::ITEM_DELETE && $app->delete(
53+
'/{id}/',
54+
self::entityAdapterWithRouteIDHandler($entityAdapter, 'delete')
55+
);
3956
if ($additional !== null) {
4057
$additional($app, $entityAdapter);
4158
}

src/Adapters/Stem/LegacyStemEntityAdapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ protected function getEntityForPayload($payload, $id = null)
5656
return $model;
5757
}
5858

59+
/**
60+
* @param Model $entity
61+
* @param array $payload
62+
*/
63+
protected function updateEntityWithPayload($entity, $payload)
64+
{
65+
$entity->importData($payload);
66+
}
67+
5968
/**
6069
* @param Model $entity
6170
* @throws \Rhubarb\Stem\Exceptions\DeleteModelException

0 commit comments

Comments
 (0)