|
1 |
| -# PHP-API |
| 1 | +# PHP-Api |
2 | 2 |
|
3 | 3 | ## Description
|
4 | 4 |
|
5 |
| -This is a lightweight and extendable **PHP-API Framework** designed to help developers quickly build RESTful APIs. It provides easy management of API parameters (GET, POST) with support for defining allowed parameters, their required status, and data types. It also allows for flexible response formatting, making it easy to create custom APIs. |
| 5 | +This is a lightweight and extendable **PHP-Api Framework** designed to help developers quickly build RESTful APIs. It provides easy management of API parameters (GET, POST) with support for defining allowed parameters, their required status, and data types. It also allows for flexible response formatting, making it easy to create custom APIs. |
6 | 6 |
|
7 | 7 | This framework can be easily integrated into any PHP project using Composer.
|
8 | 8 |
|
9 | 9 | ## Features
|
10 | 10 |
|
11 | 11 | - **Standardized API Parameter Handling**: Easily manage allowed API parameters, including data types and required status.
|
| 12 | +- **Flexible Parameter Validation**: Validate that only expected parameters are included, check for required parameters, and validate parameter data types. |
12 | 13 | - **Custom Response Wrapper**: Define a custom response structure using a flexible wrapper.
|
13 | 14 | - **Flexible HTTP Method Support**: Supports GET, POST, PUT, DELETE HTTP methods.
|
14 | 15 | - **Meta Data**: The response includes metadata such as response code, server host, number of results, and script execution time.
|
15 | 16 | - **Data Type Management**: Support for defining and managing data types for each parameter.
|
16 | 17 |
|
17 | 18 | ## Usage
|
18 | 19 |
|
19 |
| -Here is how you can use the `API` class along with `APIParameter` in your project. |
20 |
| - |
21 | 20 | ### Example: Define Allowed Parameters and Handle API Request
|
22 | 21 |
|
23 | 22 | ```php
|
24 |
| -use devt045t\API; |
25 |
| -use devt045t\APIParameter; |
| 23 | +use devt045t\Api; |
| 24 | +use devt045t\ApiParameter; |
26 | 25 | use devt045t\DataTypes;
|
27 |
| -use devt045t\HTTPStatusCodes; |
| 26 | +use devt045t\HttpStatusCodes; |
28 | 27 |
|
29 |
| -$api = new API(); |
| 28 | +$api = new Api(); |
30 | 29 |
|
31 | 30 | // Define allowed parameters
|
32 |
| -$param1 = new APIParameter(); |
| 31 | +$param1 = new ApiParameter(); |
33 | 32 | $param1
|
34 | 33 | ->setName('username')
|
35 | 34 | ->required(true)
|
36 | 35 | ->type(DataTypes::STRING);
|
37 | 36 |
|
38 |
| -$param2 = new APIParameter(); |
| 37 | +$param2 = new ApiParameter(); |
39 | 38 | $param2
|
40 | 39 | ->setName('password')
|
41 | 40 | ->required(true)
|
|
46 | 45 | ->addParameter($param1)
|
47 | 46 | ->addParameter($param2);
|
48 | 47 |
|
49 |
| -// Retrieve all parameters |
50 |
| -$parameters = $api->getAllParameters(); |
| 48 | +// Validate parameters |
| 49 | +$api->validate(); |
51 | 50 |
|
52 | 51 | // Set the response data
|
53 | 52 | $api->setResponse(['message' => 'Parameters received successfully']);
|
54 | 53 |
|
55 | 54 | // Set the custom response code (optional)
|
56 |
| -$api->setResponseCode(HTTPStatusCodes::OK); |
| 55 | +$api->setResponseCode(HttpStatusCodes::OK); |
57 | 56 |
|
58 | 57 | // Send the response
|
59 | 58 | $api->send();
|
@@ -111,39 +110,54 @@ The response will be wrapped in the custom format:
|
111 | 110 | }
|
112 | 111 | ```
|
113 | 112 |
|
114 |
| -### Available Methods |
115 |
| - |
116 |
| -- `setResponse($data)`: Sets the response data. The data can be any type (array, object, string, etc.). |
117 |
| -- `setResponseCode(int $code)`: Sets the HTTP response code. Defaults to `200`. |
118 |
| -- `send()`: Sends the response to the client. Automatically formats the response in JSON. |
119 |
| -- `setCustomWrapper(array $wrapper)`: Sets a custom wrapper for the response. If no custom wrapper is provided, the default one is used. |
120 |
| -- `getRequestMethod()`: Returns the HTTP request method (GET, POST, PUT, DELETE). |
121 |
| -- `prepareResponseWrapper()`: Prepares the response based on the current wrapper settings. |
122 |
| -- `getAllParameters()`: Returns all given GET and POST parameters as an array. |
123 |
| -- `getGETParameters()`: Returns all given GET parameters as an array. |
124 |
| -- `getPOSTParameters()`: Returns all given POST parameters as an array. |
125 |
| -- `__get(string $property)`: ✨Magic✨ getter method for accessing properties dynamically through the `$parameters` array. Returns the property’s value or null if not found. |
126 |
| -- `addParameter(APIParameter $parameter)`: Adds an allowed API parameter to the API instance. This defines the name, required status, and data type of the parameter. |
127 |
| -- `validate()`: Validates that all required data is correct and in the expected format. If any errors are found, an appropriate error message is returned. |
128 |
| -- `sendErrorResponse(int $responseCode, string $errorMessage, array $errorDetails = [])`: Sends a generic error response to the client, including a status code, an error message, and optional additional details about the error cause. |
| 113 | +## Available Methods |
| 114 | + |
| 115 | +- **Parameter Management**: |
| 116 | + - `addParameter(ApiParameter $parameter)`: Adds an allowed API parameter to the API instance, defining its name, required status, and data type. |
| 117 | + - `getAllParameters()`: Returns all given GET and POST parameters as an array. |
| 118 | + - `getGETParameters()`: Returns all given GET parameters as an array. |
| 119 | + - `getPOSTParameters()`: Returns all given POST parameters as an array. |
| 120 | + |
| 121 | +- **Validation**: |
| 122 | + - `validate()`: Validates that all required data is correct and in the expected format. It checks: |
| 123 | + - If only allowed parameters are present. |
| 124 | + - If all required parameters are provided. |
| 125 | + - If parameter data types match the expected types. |
| 126 | + - `sendErrorResponse(int $responseCode, string $errorMessage, array $errorDetails = [])`: Sends an error response if validation fails, with the status code, error message, and details of the error. |
| 127 | + |
| 128 | +- **Response Management**: |
| 129 | + - `setResponse($data)`: Sets the response data. The data can be any type (array, object, string, etc.). |
| 130 | + - `setResponseCode(int $code)`: Sets the HTTP response code. Defaults to `200`. |
| 131 | + - `send()`: Sends the response to the client. Automatically formats the response in JSON. |
| 132 | + - `setCustomWrapper(array $wrapper)`: Sets a custom wrapper for the response. If no custom wrapper is provided, the default one is used. |
| 133 | + - `prepareResponseWrapper()`: Prepares the response based on the current wrapper settings. |
| 134 | + |
| 135 | +- **Request Information**: |
| 136 | + - `getRequestMethod()`: Returns the HTTP request method (GET, POST, PUT, DELETE). |
| 137 | + - `__get(string $property)`: ✨Magic✨ getter method for accessing properties dynamically through the `$parameters` array. Returns the property’s value or null if not found. |
129 | 138 |
|
130 | 139 | ## API Parameter Management
|
131 | 140 |
|
132 |
| -The `APIParameter` class helps you define the parameters for your API. Each parameter can be configured with the following properties: |
| 141 | +The `ApiParameter` class helps you define the parameters for your API. Each parameter can be configured with the following properties: |
133 | 142 |
|
134 | 143 | - **Name**: The name of the parameter (e.g., `username`, `age`).
|
135 | 144 | - **Required**: Whether the parameter is required for the request. Default is `false`.
|
136 | 145 | - **Data Type**: The data type for the parameter (e.g., `string`, `integer`, `boolean`).
|
137 | 146 |
|
138 |
| -### Example of `APIParameter`: |
| 147 | +### Example of `ApiParameter`: |
139 | 148 |
|
140 | 149 | ```php
|
141 |
| -use devt045t\APIParameter; |
| 150 | +use devt045t\ApiParameter; |
| 151 | +use devt045t\DataTypes; |
142 | 152 |
|
143 |
| -$param = new APIParameter(); |
144 |
| -$param->setName('username')->required(true)->type('string'); |
| 153 | +$param = new ApiParameter(); |
| 154 | +$param->setName('username')->required(true)->type(DataTypes::STRING); |
145 | 155 | ```
|
146 | 156 |
|
| 157 | +### Supported Data Types |
| 158 | + |
| 159 | +- The framework provides data type validation using `DataTypes::isType($value, $dataType)`, ensuring that each parameter's value matches its defined type. |
| 160 | + |
147 | 161 | ## Configuration Options
|
148 | 162 |
|
149 | 163 | - **Custom Wrapper**: Allows you to define the format of the API response (metadata, count, host, runtime, etc.). This is ideal for adjusting the structure of the response to match your front-end or external service requirements.
|
|
0 commit comments