Skip to content

Commit 87e00f5

Browse files
Merge pull request #165 from MarcinOrlowski/dev
Release 9.0.1
2 parents 32b24a0 + 5f47a86 commit 87e00f5

File tree

5 files changed

+21
-56
lines changed

5 files changed

+21
-56
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ before doing major upgrade!
88

99
## CHANGE LOG ##
1010

11+
* v9.0.1 (2020-10-22)
12+
* Fixed failing `ServiceProvider` (reported by Efriandika Pratama).
13+
* Corrected documentation and usage examples.
14+
1115
* v9.0.0 (2020-10-17)
1216
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
1317
* [RB-156] Added logic to deal with directly returned objects or arrays.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume Laravel REST API.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "9.0.0",
5+
"version": "9.0.1",
66
"keywords": [
77
"laravel",
88
"json",

docs/docs.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114

115115
### Builder ###
116116

117-
To obtain instance of the Builder, two static methods: `asSuccess()` and `asError()` are now exposed. For example, the following
117+
There are two static methods that return instance of the Builder: `asSuccess()` and `asError()`. For example, the following
118118
code would return response indicating a success, with additional data and custom HTTP code:
119119

120120
```php
@@ -124,19 +124,8 @@ return RB::asSuccess()
124124
->build();
125125
```
126126

127-
For simplicity of use, it's recommended to add the following `use` to your code:
128-
129-
```php
130-
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
131-
```
132-
133-
If you dislike typing `ResponseBuilder` you can use [namespace aliasing](https://www.php.net/manual/en/language.namespaces.importing.php):
134-
135-
```php
136-
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
137-
```
138-
139-
Then use short form in your code:
127+
Naturally, if you just need to return success without any payload, just call `success()` as you would have in previous
128+
versions:
140129

141130
```php
142131
return RB::success();
@@ -169,7 +158,7 @@ return RB::success();
169158
response `message` based on localization files (as configured in i.e. `map`) or strings with placeholders.
170159
* `withHttpHeaders($headers)`
171160

172-
Once all is arguments are passed, call `build()` to conclude building and have final `HttpResponse` object returned.
161+
Once all the arguments are passed, call `build()` to have final `HttpResponse` object returned.
173162

174163
**IMPORTANT:** To enforce constant JSON structure of the response, `data` node is always an JSON object, therefore passing
175164
anything but `object` or `array` to `withData()` would trigger internal type casting. There's no smart logic here, just

docs/examples.md

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
## Table of contents ##
99

10+
1011
* [Usage examples](#usage-examples)
1112
* [Use clause](#use-clause)
1213
* [Success](#success)
@@ -17,20 +18,15 @@
1718
## Usage examples ##
1819

1920
The following examples assume `ResponseBuilder` is properly installed and available to your Laravel application.
20-
Installation steps are described in details in further chapters, if help is needed.
21+
See [Installation and Configuration](docs.md#installation-and-configuration) for more details.
2122

2223
### Use clause ###
2324

24-
The library is namespaced, so to simplify the use cases, it's recommended to add propr `use` at the beginning
25-
of your controller to "import" `Builder` class:
26-
27-
```php
28-
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
29-
```
25+
Basic, and mosts common usage examples.
3026

3127
#### Success ####
3228

33-
To report response indicating i.e. operation success, simply your Controller method with:
29+
To create response indicating operation success, simply conclude your Controller method with:
3430

3531
```php
3632
return RB::success();
@@ -48,14 +44,15 @@ return RB::success();
4844
}
4945
```
5046

51-
If you would like to return some data with it (which pretty much always the case :), pass it to `success()` as argument:
47+
If you would like to return some data in your response, which is pretty much always the case for success type of responses:), pass
48+
your payload as `success()`'s argument:
5249

5350
```php
5451
$data = [ 'foo' => 'bar' ];
5552
return RB::success($data);
5653
```
5754

58-
which would return:
55+
This which would produce:
5956

6057
```json
6158
{
@@ -69,17 +66,12 @@ return RB::success($data);
6966
}
7067
```
7168

72-
**NOTE:** As all the data in the response structure must be represented in JSON, `ResponseBuilder` only accepts certain types of
73-
data - you can either pass an `array` or object of any class that can be converted to valid JSON (i.e. Eloquent's Model or
74-
Collection). Data conversion goes on-the-fly, if you need any additional classes supported than said Model or Collection (which
75-
are pre-configured), you need to instruct `ResponseBuilder` how to deal with it. See [Data Conversion](#data-conversion) chapter
76-
for more details. Attempt to pass unsupported data type (i.e. literals) will throw the exception.
69+
**NOTE:** As all the data in the response structure must strictly follow response structure and end up in form os valid JSON data.
70+
`ResponseBuilder` deals with all the primitives and most commonly used classes, using on-the-fly data conversion. You can easily
71+
add own converters if none of built-in handles your data or fits your needs.See [Data Conversion](#data-conversion) chapter details.
7772

78-
**IMPORTANT:** `data` node is **always** an JSON Object. This is **enforced** by the library design, therefore if you need to
79-
return your data array as array and just its elements as shown in above example, it will be wrapped into additional array:
8073

8174
```php
82-
// this is CORRECT
8375
$returned_array = [1,2,3];
8476
return RB::success($data);
8577
```
@@ -93,26 +85,6 @@ return RB::success($data);
9385
"items": [1, 2, 3]
9486
}
9587
}
96-
```
97-
98-
**IMPORTANT:** If provided array has at least one non numeric index, then wrapping will not occur:
99-
100-
```php
101-
// this is WRONG
102-
$returned_array = ['foo' => 1, 'bar' => 2];
103-
return RB::success($returned_array);
104-
```
105-
106-
would give you `data` structure:
107-
108-
```json
109-
{
110-
...
111-
"data": {
112-
"foo": 1,
113-
"bar": 2
114-
}
115-
}
11688
```
11789

11890
#### Errors ####

src/ResponseBuilderServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Disable return type hint inspection as we do not have it specified in that
44
* class for a purpose. The base class is also not having return type hints.
55
*
6-
* @noinspection ReturnTypeCanBeDeclaredInspection
6+
* @noinspection RAeturnTypeCanBeDeclaredInspection
77
*/
88

99
declare(strict_types=1);
@@ -37,7 +37,7 @@ class ResponseBuilderServiceProvider extends ServiceProvider
3737
public function register()
3838
{
3939
foreach ($this->config_files as $file) {
40-
$this->mergeConfigFrom(__DIR__ . "/../config/{$file}", RB::CONF_CONFIG);
40+
$this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG);
4141
}
4242
}
4343

0 commit comments

Comments
 (0)