From 3d72ce1260420c20c614f9101344fed09de15ce4 Mon Sep 17 00:00:00 2001 From: DevT045T Date: Fri, 15 Nov 2024 21:14:58 +0100 Subject: [PATCH] Added composer files for github workflow and adjust README --- .gitignore | 3 ++- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++--- composer.json | 17 +++++++++++++ src/Api.php | 1 + 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 composer.json diff --git a/.gitignore b/.gitignore index d298be1..7c27609 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -public/ \ No newline at end of file +public/ +/vendor/ diff --git a/README.md b/README.md index 7ede876..b60a002 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,62 @@ This framework can be easily integrated into any PHP project using Composer. - **Meta Data**: The response includes metadata such as response code, server host, number of results, and script execution time. - **Data Type Management**: Support for defining and managing data types for each parameter. +## Installation via Composer + +To use the **PHP-Api Framework** in your project, follow these steps: + +### 1. Install Composer + +Ensure that [Composer](https://getcomposer.org/) is installed on your system. If it's not installed yet, you can install it using the following command: + +```bash +curl -sS https://getcomposer.org/installer | php +``` + +### 2. Add the Framework to Your Project + +To install the framework from GitHub, specify the `develop` branch. You can do this by running the following command: + +```bash +composer require devt045t/php-api:develop +``` + +Alternatively, if the package is not listed on Packagist, you can add the GitHub repository manually in your `composer.json` under the `"repositories"` section: + +```json +{ + "repositories": [ + { + "type": "git", + "url": "https://github.com/DevT045T/php-api.git" + } + ], + "require": { + "devt045t/php-api": "develop" + } +} +``` + +Then run: + +```bash +composer install +``` + +This will install the framework from the `develop` branch and download all necessary files. + +### 3. Enable Autoloading + +Ensure that Composer's autoloader is included in your project. Add this line at the beginning of your PHP file: + +```php +require 'vendor/autoload.php'; +``` + +### 4. Use the Framework + +You can now use the framework in your project. For more information on how to use it, refer to the [Usage](#Usage) section of this documentation. + ## Usage ### Example: Define Allowed Parameters and Handle API Request @@ -24,19 +80,20 @@ use devt045t\Api; use devt045t\ApiParameter; use devt045t\DataTypes; use devt045t\HttpStatusCodes; +use devt045t\HttpMethods; $api = new Api(); // Define allowed parameters $param1 = new ApiParameter(); $param1 - ->setName('username') + ->name('username') ->required(true) ->type(DataTypes::STRING); $param2 = new ApiParameter(); $param2 - ->setName('password') + ->name('password') ->required(true) ->type(DataTypes::INT); @@ -45,6 +102,9 @@ $api ->addParameter($param1) ->addParameter($param2); +// Set the allowed request methods +$api->setAllowedRequestMethod([HttpMethods::OPTIONS, HttpMethods::POST]); + // Validate parameters $api->validate(); @@ -151,7 +211,7 @@ use devt045t\ApiParameter; use devt045t\DataTypes; $param = new ApiParameter(); -$param->setName('username')->required(true)->type(DataTypes::STRING); +$param->name('username')->required(true)->type(DataTypes::STRING); ``` ### Supported Data Types diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dc11a62 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "t045t/php-api", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "devt045t\\": "src/" + } + }, + "authors": [ + { + "name": "DevT045T", + "email": "info@t045t.dev" + } + ], + "require": {} +} diff --git a/src/Api.php b/src/Api.php index f0988ee..ecd48c7 100644 --- a/src/Api.php +++ b/src/Api.php @@ -147,6 +147,7 @@ public function __construct() $this->scriptStartTime = microtime(true); $this->requestMethod = $_SERVER["REQUEST_METHOD"]; + $this->allowedRequestMethods = []; } /**