-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace webfiori\http; | ||
|
||
/** | ||
* A class which is used to hold names of request methods. | ||
* | ||
* @author Ibrahim | ||
*/ | ||
class RequestMethod { | ||
/** | ||
* A constant which is used to represent 'GET' request method. | ||
* | ||
* @var string | ||
*/ | ||
const GET = 'GET'; | ||
/** | ||
* A constant which is used to represent 'GET' request method. | ||
* | ||
* @var string | ||
*/ | ||
const POST = 'POST'; | ||
/** | ||
* A constant which is used to represent 'PUT' request method. | ||
* | ||
* @var string | ||
*/ | ||
const PUT = 'PUT'; | ||
/** | ||
* A constant which is used to represent 'head' request method. | ||
* | ||
* @var string | ||
*/ | ||
const HEAD = 'HEAD'; | ||
/** | ||
* A constant which is used to represent 'patch' request method. | ||
* | ||
* @var string | ||
*/ | ||
const PATCH = 'PATCH'; | ||
/** | ||
* A constant which is used to represent 'delete' request method. | ||
* | ||
* @var string | ||
*/ | ||
const DELETE = 'DELETE'; | ||
/** | ||
* A constant which is used to represent 'trace' request method. | ||
* | ||
* @var string | ||
*/ | ||
const TRACE = 'TRACE'; | ||
/** | ||
* A constant which is used to represent 'options' request method. | ||
* | ||
* @var string | ||
*/ | ||
const OPTIONS = 'OPTIONS'; | ||
/** | ||
* A constant which is used to represent 'connect' request method. | ||
* | ||
* @var string | ||
*/ | ||
const CONNECT = 'CONNECT'; | ||
/** | ||
* Returns an array that holds request methods in upper case. | ||
* | ||
* The returned array will contains the following strings: | ||
* <ul> | ||
* <li>GET</li> | ||
* <li>HEAD</li> | ||
* <li>POST</li> | ||
* <li>PUT</li> | ||
* <li>DELETE</li> | ||
* <li>TRACE</li> | ||
* <li>OPTIONS</li> | ||
* <li>PATCH</li> | ||
* <li>CONNECT</li> | ||
* </ul> | ||
* | ||
* @return array An array of request methods names. | ||
*/ | ||
public static function getAll() : array { | ||
return [ | ||
self::CONNECT, | ||
self::DELETE, | ||
self::GET, | ||
self::HEAD, | ||
self::OPTIONS, | ||
self::POST, | ||
self::PUT, | ||
self::TRACE | ||
]; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace webfiori\http; | ||
|
||
/** | ||
* A class which is used to keep track of values of default response messages. | ||
* | ||
* The library supports following default status messages. | ||
* <ul> | ||
* <li>'401': 'Not Authorized.'</li> | ||
* <li>'404-1': 'The following parameter(s) has invalid values: '</li> | ||
* <li>'404-2': 'The following required parameter(s) where missing from the request body: '</li> | ||
* <li>'404-3': 'Service name is not set.'</li> | ||
* <li>'404-4': 'Service not implemented.'</li> | ||
* <li>'404-5': 'Service not supported.'</li> | ||
* <li>'404-6': 'Request method(s) of the service are not set in code.'</li> | ||
* <li>'405': 'Method Not Allowed.'</li> | ||
* <li>'415': 'Content type not supported.'</li> | ||
* | ||
* @author Ibrahim | ||
*/ | ||
class ResponseMessages { | ||
private $messages; | ||
private static $inst; | ||
/** | ||
* | ||
* @return ResponseMessages | ||
*/ | ||
private static function getInstance() : ResponseMessages { | ||
if (self::$inst === null) { | ||
self::$inst = new ResponseMessages(); | ||
} | ||
return self::$inst; | ||
} | ||
/** | ||
* Sets a custom HTTP response message for specific error code. | ||
* | ||
* This method is used to customize the message that will be sent back to | ||
* the client in case of using a method such as WebServicesManager::notAuth(). | ||
* Also, this method can be used to add custom code with error message. | ||
* | ||
* @param string $code A string that represent the error code. By default, | ||
* the class has the following error codes pre-defined: | ||
* <ul> | ||
* <li>'401': 'Not Authorized.'</li> | ||
* <li>'404-1': 'The following parameter(s) has invalid values: '</li> | ||
* <li>'404-2': 'The following required parameter(s) where missing from the request body: '</li> | ||
* <li>'404-3': 'Service name is not set.'</li> | ||
* <li>'404-4': 'Service not implemented.'</li> | ||
* <li>'404-5': 'Service not supported.'</li> | ||
* <li>'404-6': 'Request method(s) of the service are not set in code.'</li> | ||
* <li>'405': 'Method Not Allowed.'</li> | ||
* <li>'415': 'Content type not supported.'</li> | ||
* </ul> | ||
* | ||
* @param string $message A string that represents the error message. | ||
*/ | ||
public static function set(string $code, string $message) { | ||
self::getInstance()->messages[trim($code)] = $message; | ||
} | ||
/** | ||
* Returns the value of response message given its code. | ||
* | ||
* @param string $code The code of the message such as 415. | ||
* | ||
* @return string If the code has an error message set, the method will | ||
* return it. Other than that, the string '-' is returned. | ||
*/ | ||
public static function get(string $code) : string { | ||
return isset(self::getInstance()->messages[trim($code)]) ?? '-'; | ||
} | ||
private function __construct() { | ||
$this->messages = [ | ||
'401' => 'Not Authorized.', | ||
'404-1' => 'The following parameter(s) has invalid values: ', | ||
'404-2' => 'The following required parameter(s) where missing from the request body: ', | ||
'404-3' => 'Service name is not set.', | ||
'404-4' => 'Service not implemented.', | ||
'404-5' => 'Service not supported.', | ||
'404-6' => 'Request methods of the service are not set in code.', | ||
'405' => 'Method Not Allowed.', | ||
'415' => 'Content type not supported.' | ||
]; | ||
} | ||
} |
Oops, something went wrong.