Skip to content

Commit f0d194a

Browse files
committed
chore:init migrate from okriiza
0 parents  commit f0d194a

File tree

9 files changed

+755
-0
lines changed

9 files changed

+755
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.github/workflows/main.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: true
14+
matrix:
15+
os: [ubuntu-latest, windows-latest]
16+
php: [8.0, 8.1]
17+
laravel: [9.*, 10.*]
18+
stability: [prefer-lowest, prefer-stable]
19+
include:
20+
- laravel: 10.*
21+
testbench: 8.*
22+
- laravel: 9.*
23+
testbench: 7.*
24+
exclude:
25+
- laravel: 10.*
26+
php: 8.0
27+
28+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
29+
30+
steps:
31+
- name: Checkout code
32+
uses: actions/checkout@v2
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php }}
38+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
39+
coverage: none
40+
41+
- name: Setup problem matchers
42+
run: |
43+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
44+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
45+
- name: Install dependencies
46+
run: |
47+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
48+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
49+
- name: Execute tests
50+
run: vendor/bin/phpunit

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Etiquette
8+
9+
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10+
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11+
extremely unfair for them to suffer abuse or anger for their hard work.
12+
13+
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14+
world that developers are civilized and selfless people.
15+
16+
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17+
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18+
19+
## Viability
20+
21+
When requesting or submitting new features, first consider whether it might be useful to others. Open
22+
source projects are used by many developers, who may have entirely different needs to your own. Think about
23+
whether or not your feature is likely to be used by other users of the project.
24+
25+
## Procedure
26+
27+
Before filing an issue:
28+
29+
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30+
- Check to make sure your feature suggestion isn't already present within the project.
31+
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32+
- Check the pull requests tab to ensure that the feature isn't already in progress.
33+
34+
Before submitting a pull request:
35+
36+
- Check the codebase to ensure that your feature doesn't already exist.
37+
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38+
39+
## Requirements
40+
41+
If the project maintainer has any additional requirements, you will find them listed here.
42+
43+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44+
45+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46+
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48+
49+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50+
51+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52+
53+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54+
55+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Rendi Okriza
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
![](https://banners.beyondco.de/Laravel%20API%20Response%20Formatter.png?theme=light&packageManager=composer+require&packageName=okriiza%2Flaravel-api-response-formatter&pattern=plus&style=style_1&description=generate+consistent%2C+well-structured+JSON+responses+in+your+Laravel+Application.&md=1&showWatermark=0&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg)
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/okriiza/laravel-api-response-formatter.svg?style=flat-square)](https://packagist.org/packages/okriiza/laravel-api-response-formatter)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/okriiza/laravel-api-response-formatter.svg?style=flat-square)](https://packagist.org/packages/okriiza/laravel-api-response-formatter)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
6+
7+
# Laravel API Response Formatter
8+
9+
`Laravel API Response Formatter` is a class that provides methods for formatting API responses in a standardized format. It simplifies the process of creating consistent and well-structured JSON responses in your API.
10+
11+
## Requirements
12+
13+
- PHP `^8.1 | ^8.2 | ^8.3`
14+
- Laravel 8, 9, 10, 11 or 12
15+
16+
## Installation
17+
18+
You can install the package via composer:
19+
20+
```bash
21+
composer require okriiza/laravel-api-response-formatter
22+
```
23+
24+
The package will automatically register itself.
25+
26+
## Function List
27+
28+
The `Laravel API Response Formatter` class provides the following functions:
29+
30+
| Function | Description |
31+
| -------------------- | ----------------------------------------------------------------------------------------- |
32+
| `success()` | Formats a success response with optional data, message, status, and HTTP code. |
33+
| `created()` | Formats a created response with optional data, message, status, and HTTP code. |
34+
| `noContent()` | Formats a no content response with optional data, message, status, and HTTP code. |
35+
| `error()` | Formats an error response with optional data, message, status, and HTTP code. |
36+
| `unAuthenticated()` | Formats an unauthenticated response with optional data, message, status, and HTTP code. |
37+
| `forbidden()` | Formats a forbidden response with optional data, message, status, and HTTP code. |
38+
| `notFound()` | Formats a not found response with optional data, message, status, and HTTP code. |
39+
| `methodNotAllowed()` | Formats a method not allowed response with optional data, message, status, and HTTP code. |
40+
| `failedValidation()` | Formats a failed validation response with optional data, message, status, and HTTP code. |
41+
42+
## Parameters
43+
44+
The functions in the `Laravel API Response Formatter` class accept the following parameters:
45+
46+
- `$data` (optional): The data to be included in the response. It can be of any type.
47+
- `$message` (optional): The message to be included in the response. If not provided, a default message will be used.
48+
- `$status` (optional): The success status of the response. Defaults to `true` for success responses and `false` for error responses.
49+
- `$httpCode` (optional): The HTTP response code to be returned. It defaults to the corresponding HTTP status code for each response type.
50+
51+
## Example Usage
52+
53+
Here's an example of how you can use the `Laravel API Response Formatter` class in a user controller:
54+
55+
```php
56+
<?php
57+
58+
use Okriiza\ApiResponseFormatter\ApiResponse;
59+
60+
class UserController extends Controller
61+
{
62+
public function show($id): JsonResponse
63+
{
64+
$user = User::find($id);
65+
66+
if ($user) {
67+
return ApiResponse::success($user);
68+
} else {
69+
return ApiResponse::notFound(null, 'User not found');
70+
}
71+
}
72+
73+
public function create(Request $request): JsonResponse
74+
{
75+
// Validation logic
76+
77+
if ($validationFails) {
78+
return ApiResponse::failedValidation($validationErrors);
79+
}
80+
81+
$user = User::create($request->all());
82+
83+
return ApiResponse::created($user);
84+
}
85+
}
86+
```
87+
88+
In the above example, the `show()` method fetches a user by ID and returns a success response if the user exists. If the user is not found, it returns a not found response. The `create()` method performs validation and creates a new user. If the validation fails, it returns a failed validation response. Otherwise, it returns a created response with the created user.
89+
90+
```json
91+
{
92+
"meta": {
93+
"code": 200,
94+
"success": true,
95+
"message": "OK"
96+
},
97+
"result": {
98+
"id": 1,
99+
"name": "John Doe",
100+
"email": "john@example.com"
101+
}
102+
}
103+
```
104+
105+
And for an error case:
106+
107+
```json
108+
{
109+
"meta": {
110+
"code": 404,
111+
"success": false,
112+
"message": "User not found"
113+
},
114+
"result": null
115+
}
116+
```
117+
118+
The `meta` object contains information about the response, such as the response code, status, and message. The `result` object holds the actual response data.
119+
120+
Note: The examples provided are simplified and may require modifications to fit your specific use case
121+
122+
## Contributing
123+
124+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
125+
126+
### Security
127+
128+
If you discover any security related issues, please email okriizaa@gmail.com instead of using the issue tracker.
129+
130+
## Credits
131+
132+
This package was created by [Rendi Okriza](https://github.com/okriiza)
133+
134+
- [All Contributors](../../contributors)
135+
136+
## License
137+
138+
The Laravel API Response Formatter package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "renslabs/laravel-api-response-formatter",
3+
"description": "a simple package Format API responses throughout your Laravel application",
4+
"keywords": [
5+
"renslabs",
6+
"api-response-formatter",
7+
"laravel api response formatter",
8+
"laravel api response formatter helpers"
9+
],
10+
"homepage": "https://github.com/renslabs/laravel-api-response-formatter",
11+
"license": "MIT",
12+
"type": "library",
13+
"authors": [
14+
{
15+
"name": "RensLabs",
16+
"email": "renslabsa@gmail.com",
17+
"homepage": "https://renslabs.my.id",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"php": "^8.1|^8.2|^8.3",
23+
"illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0"
24+
},
25+
"require-dev": {
26+
"orchestra/testbench": "^8.0|^9.0| ^10.0",
27+
"phpunit/phpunit": "^7.0|^8.0|^9.0|^9.5|^10.0|^11.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"renslabs\\ApiResponseFormatter\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"renslabs\\ApiResponseFormatter\\Tests\\": "tests/"
37+
}
38+
},
39+
"config": {
40+
"sort-packages": true,
41+
"optimize-autoloader": true
42+
},
43+
"extra": {
44+
"laravel": {
45+
"providers": [
46+
"renslabs\\ApiResponseFormatter\\ApiResponseServiceProvider"
47+
]
48+
}
49+
},
50+
"scripts": {
51+
"test": "./vendor/bin/phpunit"
52+
},
53+
"minimum-stability": "dev",
54+
"prefer-stable": true
55+
}

0 commit comments

Comments
 (0)