Created
March 22, 2022 11:10
-
-
Save vishwac09/c320e1dbaca370e7dc54cd39500abdeb to your computer and use it in GitHub Desktop.
PHP Pseudo code showing resource handler implementation for PATCH requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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