Skip to content

Commit 19e8026

Browse files
authored
Merge pull request #8 from uxmansarwar/uxman_dev
v4.0.0 init commit - i am just about to new release of this package.
2 parents 372ac0c + b925934 commit 19e8026

File tree

5 files changed

+487
-192
lines changed

5 files changed

+487
-192
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 113 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,120 @@
1-
# Changelog
1+
# πŸ“¦ CHANGELOG for uxmansarwar/response
22

3-
## [3.0.0] - 2025-03-11
4-
### Added
5-
- **PHPStan Compliant Developer Comments**: Added detailed PHPStan-compliant comments to improve code readability and maintainability.
6-
- **`collection()` Method**: Introduced a new method to centralize response data collection, reducing redundancy in `json()` and `array()`.
7-
- **Static Response Key Names**: Defined `$result_key_text`, `$error_key_text`, and `$input_key_text` as static class properties for better maintainability.
3+
This is the official changelog for the `uxmansarwar/response` Composer package.
4+
This PHP package helps developers return structured API responses in a simple, clean, and fluent way.
5+
Maintained by [Uxman Sarwar](https://github.com/uxmansarwar) β€” a professional full-stack PHP developer since 2013.
86

9-
### Changed
10-
- **Singleton Handling**: Improved `singleton()` method for better reusability instead of initializing in multiple places.
11-
- **Constructor Reset Behavior**: Adjusted constructor logic to ensure that each instantiation resets response states correctly.
12-
- **JSON Parsing Validation**: Enhanced JSON request body parsing by verifying `json_last_error()`.
13-
- **Better Result & Error Storage**: Updated result and error storage to always ensure correct structuring when using keys.
7+
> Install via Composer:
8+
> `composer require uxmansarwar/response`
149
15-
### Fixed
16-
- **Incorrect Singleton Reinitialization**: Ensured `singleton()` and `init()` methods correctly maintain a single instance.
17-
- **Input Handling Edge Cases**: Addressed cases where JSON input merging could fail under certain request formats.
18-
- **Return Consistency**: Standardized method return values to always return `self` where applicable for method chaining.
10+
---
11+
12+
## πŸ“Œ Version 4.0.0 – Major Rewrite & Powerful Improvements (May 2025)
13+
14+
This version is a **major upgrade** from `v3.x` to `v4.0.0`.
15+
It is a complete rewrite with **new features**, **cleaner code**, and **more flexibility** for real-world APIs and production apps.
16+
17+
### βœ… What's New
18+
19+
- πŸ” **Total Refactor to Singleton-based Fluent Class**
20+
- Keeps only one instance in memory. Cleaner, faster, and easier to use.
21+
22+
- πŸ—‚οΈ **New `index()` Method**
23+
- Add secondary keys under each primary `key()`. Useful for nesting grouped data.
24+
25+
- 🧠 **New `ttl()` Support**
26+
- You can now attach a **TTL (time to live)** value in the response. Useful for cache-aware APIs or auto-expiring resources.
27+
28+
- πŸ” **New `query()` Method**
29+
- Attach queries(Like what this response about), filter info, or any debug context directly to the response.
30+
31+
- 🧩 **Clearer Constants**
32+
- All internal keys are now constants like `RESULT_KEY_TXT`, `ERROR_KEY_TXT`, `INPUT_KEY_TXT`, etc.
33+
- Easier to maintain and debug in large apps.
34+
35+
- πŸ”’ **Private Constructor with Auto Reset**
36+
- Every call to `init()` fully resets internal states (`results`, `errors`, `keys`, `ttl`, etc.).
37+
38+
- 🌐 **Smarter Input Collection**
39+
- Automatically merges `$_GET`, `$_POST`, and raw JSON from `php://input`.
40+
- No need to manually parse input again.
41+
42+
- πŸ§ͺ **Fluent Interface**
43+
- You can now write responses like:
44+
```php
45+
Response::key('user')->index('profile')->result(['name' => 'John']);
46+
```
47+
48+
- 🧼 **Improved Readability and Simplicity**
49+
- All methods return `$this` or `self::singleton()` for smooth chaining.
50+
- Code is clean, short, and easy for junior and senior developers to follow.
51+
52+
---
53+
54+
## 🧯 Version 3.0.0 – Stable & Basic (Early 2024)
55+
56+
This was the original **stable release** of the Response class.
57+
It provided basic JSON response handling in a singleton-style package.
58+
59+
### Features Included:
60+
- βœ… Add results via `result($value)`
61+
- ❌ Add errors via `error($message)`
62+
- πŸ” Group responses using `key($name)`
63+
- πŸ“₯ Optionally include user input with `input(true)`
64+
- 🧾 Response as `array()` or `json()`
65+
- πŸ”„ Automatic input parsing from `$_GET`, `$_POST`, and `php://input`
66+
- 🧼 Singleton structure with private constructor and `init()` method
1967

2068
---
2169

22-
## [2.0.1] - Previous Version
23-
### Initial Features
24-
- Implemented Singleton pattern.
25-
- Added methods for storing results (`result()`) and errors (`error()`).
26-
- Supported response output in both JSON (`json()`) and array (`array()`) formats.
27-
- Allowed toggling of user input inclusion in responses via `input()` method.
70+
## πŸ’‘ Why Use This PHP Package?
71+
72+
- Built by a real-world **senior PHP developer** with over **12 years of experience**
73+
- Ideal for **Laravel**, **RESTful APIs**, **microservices**, **modular monoliths**, and more
74+
- Easy to use, even for beginners
75+
- Clean output format for **mobile apps**, **front-end frameworks**, and **automated testing**
76+
77+
---
78+
79+
## πŸ” SEO & GitHub Keywords
80+
81+
> _To help other developers find this package, the following terms are relevant:_
82+
83+
- PHP JSON Response Composer Package
84+
- Laravel API Response Helper
85+
- PHP Singleton Response Class
86+
- Return structured JSON response in PHP
87+
- Fluent PHP API response builder
88+
- Composer PHP package by [Uxman Sarwar](https://github.com/uxmansarwar)
89+
- PHP developer tools 2025
90+
- PHP API response formatter
91+
- Best Composer packages for Laravel APIs
92+
93+
---
94+
95+
## πŸ§‘β€πŸ’» About the Author
96+
97+
This package is built and maintained by [Uxman Sarwar](https://github.com/uxmansarwar),
98+
a **senior PHP developer since 2013**, with deep experience in:
99+
100+
- Laravel | PHP 8+ | Tailwind CSS
101+
- JavaScript | Vue.js | REST APIs
102+
- SaaS Architecture | DevOps | Clean Code
103+
104+
πŸ“§ Reach me: [uxmansrwr@gmail.com](mailto:uxmansrwr@gmail.com)
105+
πŸ”— Connect on [LinkedIn](https://www.linkedin.com/in/uxmansarwar)
106+
πŸ™ Star & follow my work on [GitHub](https://github.com/uxmansarwar)
107+
108+
---
109+
110+
## ⬇️ Install This Package
111+
112+
composer require uxmansarwar/response
113+
114+
Then use it like this:
115+
116+
```php
117+
use UxmanSarwar\Response;
28118

119+
echo Response::key('user')->result(['name' => 'John'])->json();
120+
```

β€ŽREADME.mdβ€Ž

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,107 @@
1-
# Response - PHP API Response Handler
1+
# πŸ“¦ Response – Elegant PHP API Response Handler
22

33
[![Packagist](https://img.shields.io/packagist/v/uxmansarwar/response)](https://packagist.org/packages/uxmansarwar/response)
4-
[![PHP Version](https://img.shields.io/badge/php-%3E%3D7.2-blue)](https://www.php.net/)
4+
[![PHP Version](https://img.shields.io/badge/php-%3E%3D8.0-blue)](https://www.php.net/)
55
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
6+
[![GitHub Stars](https://img.shields.io/github/stars/uxmansarwar/response?style=social)](https://github.com/uxmansarwar/response)
67

7-
## Overview
8+
## πŸ” Overview
89

9-
`Response` is a **lightweight PHP library** designed to streamline **API response handling**. It follows a **singleton-based pattern** to store and retrieve results, errors, and user input efficiently. The package is **PSR-4 compliant** and fully compatible with **PHP 7.2+ and PHP 8.2**.
10+
**`uxmansarwar/response`** is a powerful yet lightweight PHP package that makes it easy to manage structured API responses. Built with modern PHP practices, it follows a clean singleton pattern and supports result grouping, TTL, debug queries, and more. Ideal for Laravel, Symfony, WordPress, CodeIgniter, or raw PHP projects.
1011

11-
### **Key Features**
12-
- βœ… **Singleton pattern** to prevent redundant object creation.
13-
- βœ… **Structured response handling** for JSON and array outputs.
14-
- βœ… **Collect API results & errors dynamically**.
15-
- βœ… **Retrieve user input** from `$_GET`, `$_POST`, and JSON payloads.
16-
- βœ… **Flexible data retrieval** (JSON, array, collection format).
17-
- βœ… **Works with Laravel, Symfony, CodeIgniter, WordPress, and Core PHP**.
12+
Developed and maintained by [Uxman Sarwar](https://github.com/uxmansarwar), a senior PHP developer since 2013.
13+
14+
## βœ… Features
15+
16+
- Singleton-based fluent API
17+
- Add results, errors, queries, TTL, and input metadata
18+
- Auto-collects `$_GET`, `$_POST`, and raw JSON input
19+
- Customizable result/error key groups with `key()` and `index()`
20+
- Get structured responses as JSON or array
21+
- Great for APIs, microservices, and AJAX handlers
1822

1923
---
2024

21-
## Installation
25+
## βš™οΈ Installation
2226

23-
### **Install via Composer**
24-
You can install this package using Composer:
25-
```sh
26-
composer require uxmansarwar/response
27-
```
27+
### Via Composer
2828

29-
### **Manual Installation**
30-
Alternatively, clone this repository and include it in your project:
31-
```sh
32-
git clone https://github.com/uxmansarwar/Response.git
33-
```
34-
Then, include the autoloader:
35-
```php
36-
require 'vendor/autoload.php';
29+
```bash
30+
composer require uxmansarwar/response
3731
```
3832

3933
---
4034

41-
## **Usage Examples**
35+
## πŸš€ Quick Start
36+
37+
### Initialize
4238

43-
### **1. Initializing the Response Handler**
4439
```php
4540
use UxmanSarwar\Response;
4641

4742
Response::init();
4843
```
4944

50-
### **2. Storing Results**
45+
### Add Result
46+
5147
```php
52-
Response::result("User created successfully");
53-
Response::result(["id" => 1, "name" => "John Doe"]);
48+
Response::result(['id' => 1, 'name' => 'Alice']);
5449
```
5550

56-
### **3. Handling Errors**
51+
### Add Error
52+
5753
```php
58-
Response::error("Invalid API request");
59-
Response::error("Failed to connect to the database");
54+
Response::error('Invalid request type');
6055
```
6156

62-
### **4. Retrieving Response Data**
63-
#### JSON Output:
57+
### Grouped Result with Key/Index
58+
6459
```php
65-
echo Response::json();
66-
```
67-
**Example Output:**
68-
```json
69-
{
70-
"result": [
71-
"User created successfully",
72-
{ "id": 1, "name": "John Doe" }
73-
],
74-
"error": [
75-
"Invalid API request",
76-
"Failed to connect to the database"
77-
]
78-
}
60+
Response::key('user')->index('info')->result(['email' => 'user@example.com']);
7961
```
8062

81-
#### Array Output:
63+
### Set Time-To-Live (TTL)
64+
8265
```php
83-
$response_array = Response::collection();
84-
print_r($response_array);
66+
Response::ttl(60);
8567
```
8668

87-
---
88-
89-
## **Advanced Features**
69+
### Attach Debug Query
9070

91-
### **5. Grouping Responses with a Key**
9271
```php
93-
Response::key("user")->result(["id" => 2, "name" => "Jane Doe"]);
94-
echo Response::json();
72+
Response::query('SELECT * FROM users WHERE id = 1');
9573
```
9674

97-
### **6. Include User Input in Response**
75+
### Include Input Data
76+
9877
```php
9978
Response::input(true);
100-
echo Response::json();
101-
```
102-
If a request is made with:
103-
```sh
104-
GET /api.php?id=10&name=Alice
10579
```
106-
The output will include:
107-
```json
108-
"input": {
109-
"id": "10",
110-
"name": "Alice"
111-
}
80+
81+
### Output as JSON
82+
83+
```php
84+
echo Response::json();
11285
```
11386

114-
### **7. Custom Error & Result Keys**
115-
You can define custom keys for results and errors:
87+
### Output as Array
88+
11689
```php
117-
Response::key("dns");
90+
print_r(Response::array());
11891
```
11992

12093
---
12194

122-
## **Use Case Example: API Response Handling**
95+
## πŸ’‘ Example Use Case: API Endpoint
96+
12397
```php
12498
Response::init();
12599

126100
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
127101
Response::error("Only POST requests are allowed");
128102
} else {
129103
$data = Response::$_INPUT;
130-
if (!isset($data['username']) || empty($data['username'])) {
104+
if (empty($data['username'])) {
131105
Response::error("Username is required");
132106
} else {
133107
Response::result("User registered successfully");
@@ -139,20 +113,48 @@ echo Response::json();
139113

140114
---
141115

142-
## **Why Use This Package?**
143-
### βœ… **For Laravel & Symfony Developers**: Use it as a service-based response handler.
144-
### βœ… **For WordPress Developers**: Improve structured AJAX responses.
145-
### βœ… **For REST API Development**: Optimize API response handling with minimal effort.
146-
### βœ… **For Microservices**: Centralize error handling and response formatting.
116+
## πŸ§ͺ Testing
147117

148-
---
149-
150-
## **Testing the Package**
151-
To install and run tests:
152-
```sh
118+
```bash
153119
composer install
154120
vendor/bin/phpstan analyse src --level=max
155121
vendor/bin/pest
156122
```
157123

124+
---
125+
126+
## 🌐 Use Cases
127+
128+
- Laravel: Replace default `response()->json()` with a fluent helper
129+
- WordPress: Handle AJAX with structured output
130+
- Symfony: Wrap controller responses with grouped structure
131+
- REST APIs: Make consistent error/result formatting
132+
- Microservices: Inject debug info and TTL for downstream caching
133+
134+
---
135+
136+
## πŸ‘¨β€πŸ’» About the Author
137+
138+
This package is created by [Uxman Sarwar](https://github.com/uxmansarwar), a full-stack PHP Laravel developer.
139+
140+
- GitHub: [@uxmansarwar](https://github.com/uxmansarwar)
141+
- LinkedIn: [Uxman Sarwar](https://linkedin.com/in/uxmansarwar)
142+
- Email: [uxmansrwr@gmail.com](mailto:uxmansrwr@gmail.com)
143+
144+
If you found this package useful, consider ⭐ starring the repo and sharing it with other developers.
145+
146+
---
147+
148+
## πŸ”— SEO & GitHub Keywords
149+
150+
PHP API response library, structured API output PHP, response formatter, Laravel response helper, singleton response PHP, uxmansarwar response composer, REST API output PHP, error handler class PHP, api response json PHP, PHP response class Laravel
151+
152+
---
153+
154+
## πŸ“₯ Composer Install Reminder
155+
156+
```
157+
composer require uxmansarwar/response
158+
```
158159

160+
Happy coding! πŸš€

0 commit comments

Comments
Β (0)