Skip to content

Commit

Permalink
init 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
soulaimaneyahya committed Nov 9, 2023
0 parents commit 56b2730
Show file tree
Hide file tree
Showing 19 changed files with 702 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress

- name: Validate PHP PSR
run: composer run-script php-psr

- name: Run test suite
run: composer run-script test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor/
/.phpunit.result.cache
/phpunit.xml
/composer.lock
.idea/
.TODO
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Multividas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<div align="center">

<img width="150" height="150" src="./assets/api-responser.svg" alt="Query Option package logo"/>

# API Responser

[![Tests](https://github.com/multividas/api-responser/actions/workflows/tests.yml/badge.svg)](https://github.com/multividas/api-responser/actions/workflows/tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/multividas/api-responser.svg?style=flat-square)](https://packagist.org/packages/multividas/api-responser)
[![License](https://img.shields.io/github/license/multividas/api-responser?style=flat-square)](https://github.com/multividas/api-responser/blob/master/LICENSE.md)

</div>

Composer package to facilitates the process of structuring and generating API responses

## Installation
Require this package with composer. It is recommended to only require the package for development.

```shell
composer require multividas/api-responser --dev
```

## ServiceProvider:

**[Optional]** Adding the **ApiResponserServiceProvider** to the providers array in **config/app.php**

```php
\Multividas\ApiResponser\Providers\ApiResponserServiceProvider::class,
```

**[Optional]** To get **X-Application-Name** http response header, Copy the package config to your local config with the publish command:

```sh
php artisan vendor:publish --tag=api-responser-config
```

## Usage

```php
use \Multividas\ApiResponser\Traits\ApiResponser;

class Controller extends BaseController
{
use ApiResponser;
}
```

### Dependency Injection

PostsController has __construct() method initializes a property apiRepository with an instance of the ApiRepositoryInterface.

`showAll()` method receives **`Collection|JsonResource`** as its param.

`showOne()` method receives **`Model|JsonResource $instance`** as its param.

```php
use \Multividas\ApiResponser\Interfaces\ApiRepositoryInterface;

class PostsController extends Controller
{
public function __construct(
public ApiRepositoryInterface $apiRepository
) {
}

public function index(): JsonResponse
{
return $this->apiRepository->showAll(Post::all());
}

public function show(Post $post): JsonResponse
{
if (!$post instanceof Post) {
return $this->infoResponse('Item Not Found', 404, []);
}

return $this->apiRepository->showOne($post);
}
}
```

### Facades

Using the `ApiResponser` to access the methods of `ApiRepositoryInterface` in your `PostsController`.

```php
use Multividas\ApiResponser\Facades\ApiResponser;

class PostsController extends Controller
{
public function index(): JsonResponse
{
return ApiResponser::showAll(Post::all());
}

public function show(string $postId): JsonResponse
{
$post = Post::find($postId);

if (!$post instanceof Post) {
return $this->infoResponse('Post Not Found', 404, (object)[]);
}

return ApiResponser::showOne($post);
}
}
```

This approach provides a cleaner and more organized way to interact with the `ApiRepositoryInterface` instance in your controller methods.


---

### Run PHPUnit tests

```sh
composer test
```

---

Need helps? Reach us

> Multividas.com Ⓜ️
> Email: [email protected]
🌌 🚀
1 change: 1 addition & 0 deletions assets/api-responser.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "multividas/api-responser",
"description": "composer package to facilitates the process of structuring and generating API responses ",
"keywords": [
"api-responser"
],
"type": "package",
"license": "MIT",
"authors": [
{
"name": "multividas inc",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Multividas\\ApiResponser\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Multividas\\ApiResponser\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit tests/Feature",
"php-psr": [
"./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 --extensions=php src/* tests/*"
]
},
"minimum-stability": "dev",
"require": {
"php": "^7.4 || ^8.0"
},
"require-dev": {
"orchestra/testbench": "dev-develop",
"squizlabs/php_codesniffer": "4.0.x-dev",
"phpunit/phpunit": "9.6.x-dev",
"multividas/query-filters": "dev-main"
},
"config": {
"optimize-autoloader": true
},
"extra": {
"laravel": {
"providers": [
"Multividas\\ApiResponser\\Providers\\ApiResponserServiceProvider"
],
"aliases": {
"ApiResponser": "Multividas\\ApiResponser\\Facades\\ApiResponser"
}
}
},
"repositories": [
{
"type": "path",
"url": "./../query-filters"
}
]
}
13 changes: 13 additions & 0 deletions config/api-responser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* (c) 2023 Multividas inc. All rights reserved.
* Unauthorized use prohibited.
* Website: https://www.multividas.com
*/

use Illuminate\Support\Env;

return [
'app-name' => Env::get('APP_NAME', 'Api-Responser'),
];
20 changes: 20 additions & 0 deletions src/Facades/ApiResponser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* (c) 2023 Multividas inc. All rights reserved.
* Unauthorized use prohibited.
* Website: https://www.multividas.com
*/

namespace Multividas\ApiResponser\Facades;

use Illuminate\Support\Facades\Facade;
use Multividas\ApiResponser\Interfaces\ApiRepositoryInterface;

class ApiResponser extends Facade
{
protected static function getFacadeAccessor(): string
{
return ApiRepositoryInterface::class;
}
}
36 changes: 36 additions & 0 deletions src/Interfaces/ApiRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* (c) 2023 Multividas inc. All rights reserved.
* Unauthorized use prohibited.
* Website: https://www.multividas.com
*/

namespace Multividas\ApiResponser\Interfaces;

use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\Json\JsonResource;

interface ApiRepositoryInterface
{
/**
* @JsonResponse Collection
*/
public function showAll(
Collection|EloquentCollection|JsonResource $collection,
int $code = 200,
array $meta = []
): JsonResponse;

/**
* @JsonResponse instance
*/
public function showOne(
Model|JsonResource $instance,
int $code = 200,
array $meta = []
): JsonResponse;
}
37 changes: 37 additions & 0 deletions src/Providers/ApiResponserServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* (c) 2023 Multividas inc. All rights reserved.
* Unauthorized use prohibited.
* Website: https://www.multividas.com
*/

namespace Multividas\ApiResponser\Providers;

use Illuminate\Support\ServiceProvider;
use Multividas\ApiResponser\Repositories\ApiRepository;
use Multividas\ApiResponser\Interfaces\ApiRepositoryInterface;

class ApiResponserServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->publishes([
$this->basePath('config/api-responser.php') => base_path('config/api-responser.php')
], 'api-responser-config');
}

public function register(): void
{
$this->mergeConfigFrom($this->basePath('config/api-responser.php'), 'api-responser');

$this->app->bind(ApiRepositoryInterface::class, function () {
return new ApiRepository();
});
}

protected function basePath($path = ""): string
{
return __DIR__ . '/../../' . $path;
}
}
Loading

0 comments on commit 56b2730

Please sign in to comment.