Skip to content

Commit 213873f

Browse files
committed
Improved validation of parameters and renamed Http and Api
1 parent 09aa75d commit 213873f

File tree

6 files changed

+315
-81
lines changed

6 files changed

+315
-81
lines changed

Diff for: README.md

+47-33
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
# PHP-API
1+
# PHP-Api
22

33
## Description
44

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.
66

77
This framework can be easily integrated into any PHP project using Composer.
88

99
## Features
1010

1111
- **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.
1213
- **Custom Response Wrapper**: Define a custom response structure using a flexible wrapper.
1314
- **Flexible HTTP Method Support**: Supports GET, POST, PUT, DELETE HTTP methods.
1415
- **Meta Data**: The response includes metadata such as response code, server host, number of results, and script execution time.
1516
- **Data Type Management**: Support for defining and managing data types for each parameter.
1617

1718
## Usage
1819

19-
Here is how you can use the `API` class along with `APIParameter` in your project.
20-
2120
### Example: Define Allowed Parameters and Handle API Request
2221

2322
```php
24-
use devt045t\API;
25-
use devt045t\APIParameter;
23+
use devt045t\Api;
24+
use devt045t\ApiParameter;
2625
use devt045t\DataTypes;
27-
use devt045t\HTTPStatusCodes;
26+
use devt045t\HttpStatusCodes;
2827

29-
$api = new API();
28+
$api = new Api();
3029

3130
// Define allowed parameters
32-
$param1 = new APIParameter();
31+
$param1 = new ApiParameter();
3332
$param1
3433
->setName('username')
3534
->required(true)
3635
->type(DataTypes::STRING);
3736

38-
$param2 = new APIParameter();
37+
$param2 = new ApiParameter();
3938
$param2
4039
->setName('password')
4140
->required(true)
@@ -46,14 +45,14 @@ $api
4645
->addParameter($param1)
4746
->addParameter($param2);
4847

49-
// Retrieve all parameters
50-
$parameters = $api->getAllParameters();
48+
// Validate parameters
49+
$api->validate();
5150

5251
// Set the response data
5352
$api->setResponse(['message' => 'Parameters received successfully']);
5453

5554
// Set the custom response code (optional)
56-
$api->setResponseCode(HTTPStatusCodes::OK);
55+
$api->setResponseCode(HttpStatusCodes::OK);
5756

5857
// Send the response
5958
$api->send();
@@ -111,39 +110,54 @@ The response will be wrapped in the custom format:
111110
}
112111
```
113112

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.
129138

130139
## API Parameter Management
131140

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:
133142

134143
- **Name**: The name of the parameter (e.g., `username`, `age`).
135144
- **Required**: Whether the parameter is required for the request. Default is `false`.
136145
- **Data Type**: The data type for the parameter (e.g., `string`, `integer`, `boolean`).
137146

138-
### Example of `APIParameter`:
147+
### Example of `ApiParameter`:
139148

140149
```php
141-
use devt045t\APIParameter;
150+
use devt045t\ApiParameter;
151+
use devt045t\DataTypes;
142152

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);
145155
```
146156

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+
147161
## Configuration Options
148162

149163
- **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

Comments
 (0)