Skip to content

Instantly share code, notes, and snippets.

@vishwac09
Created March 22, 2022 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishwac09/c320e1dbaca370e7dc54cd39500abdeb to your computer and use it in GitHub Desktop.
Save vishwac09/c320e1dbaca370e7dc54cd39500abdeb to your computer and use it in GitHub Desktop.
PHP Pseudo code showing resource handler implementation for PATCH requests.
<?php
class Invitation {
/**
* Callback handler for HTTP Patch request.
* @param int|null $id
* @param Request|null $request
*/
public function patch(int $id = NULL, Request $request = NULL) {
// Array of actions and their corresponding action Resolvers.
$actionResolver = [
'accept' => 'acceptInvitation',
'reject' => 'rejectInvitation',
'request_change' => 'requestChangeInvitation'
];
$content = $request->getContent();
$action = $content->action ?? '' ;
$actionArguments = $content->actionArguments ?? null;
// Resolve and execute the relative function associated with that action.
if (isset($actionResolver[$action])) {
// On Success, respond with HTTP 204.
return $actionResolver[$action]($actionArguments);
}
else {
// Throw Exception HTTP 400 Bad request for invalid action name.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment