Skip to content

Commit 65b5b35

Browse files
Merge pull request #104 from MarcinOrlowski/dev
Release v6.3.0
2 parents 8ee32cb + 1930a62 commit 65b5b35

35 files changed

+3924
-3644
lines changed

CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
66

77
## CHANGE LOG ##
88

9+
* v6.3.0 (2019-11-02)
10+
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
11+
* Signature of `ResponseBuilder::buildResponse()` changed to allow customization of final `message` entry (@hawezo).
12+
* Moved all code that produces messages for API codes to `ResponseBuilder::getMessageForApiCode()`.
13+
* Added `Validator::assertType()` helper method that validates var against set of allowed types.
14+
* Added `Validator::assertString()` helper.
15+
916
* v6.2.3 (2019-10-31)
1017
* Added Laravel 6.4 to Travis-CI unit tests.
1118
* Corrected example in "Manipulating Response Object" docs.

CODE_OF_CONDUCT.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
If you need to be told how to behave, then something went wrong during your childhood.
2-
Other than that you are responsible for your actions and the possible consequences
3-
(which actually should be nothing new to any grown up human being). Amen.
1+
If you need to be told how to behave, then something went wrong during your
2+
childhood, otherwise you already know that you are solely responsible for
3+
your actions and the possible consequences (which actually should be nothing
4+
new to any grown up human being). Amen.

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
[![Latest Stable Version](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/v/stable)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
66
[![Build Status](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder.svg?branch=master)](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder)
7-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=master)
8-
[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=dev)
9-
[![Codacy Grade Badge](https://api.codacy.com/project/badge/Grade/44f427e872e2480597bde0242417a2a7)](https://www.codacy.com/app/MarcinOrlowski/laravel-api-response-builder?utm_source=github.com&utm_medium=referral&utm_content=MarcinOrlowski/laravel-api-response-builder&utm_campaign=Badge_Grade)
7+
[![Code Quality](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=master)
8+
[![Code Coverage](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=master)
9+
[![Codacy Grade Badge](https://api.codacy.com/project/badge/Grade/44f427e872e2480597bde0242417a2a7)](https://www.codacy.com/app/MarcinOrlowski/laravel-api-response-builder)
1010
[![Monthly Downloads](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/d/monthly)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
1111
[![License](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/license)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
1212

1313
[![SensioLabs Insight](https://insight.sensiolabs.com/projects/5c5f4dc1-41d5-49f9-b4ba-6268aa3fea00/big.png)](https://insight.sensiolabs.com/projects/5c5f4dc1-41d5-49f9-b4ba-6268aa3fea00)
1414

15-
[![Latest Unstable Version](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/v/unstable)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
16-
[![Build Status](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder.svg?branch=dev)](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder)
17-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/quality-score.png?b=dev)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=dev)
18-
[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/coverage.png?b=dev)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=dev)
15+
[![Unstable Version](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/v/unstable)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
16+
[![Unstable Build Status](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder.svg?branch=dev)](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder)
17+
[![Ustable Code Quality](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/quality-score.png?b=dev)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=dev)
18+
[![Unstble Code Coverage](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/badges/coverage.png?b=dev)](https://scrutinizer-ci.com/g/MarcinOrlowski/laravel-api-response-builder/?branch=dev)
1919

2020
## Table of contents ##
2121

@@ -30,7 +30,7 @@
3030
* [License](#license)
3131
* [Changelog](CHANGES.md)
3232

33-
**Upgrading from previous version? Ensure you read [compatibility docs](docs/compatibility.md) prior altering your `composer.json`!**
33+
**Upgrading from previous version? Check [compatibility docs](docs/compatibility.md) prior altering your `composer.json`!**
3434

3535
----
3636

@@ -97,15 +97,12 @@
9797

9898
* Easy to use,
9999
* [Stable and production ready](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder),
100-
* Laravel compatibility: v6.x (see [legacy support](docs/legacy.md) if you use older Laravel version),
101-
* Supports Laravel [auto-discovery](https://medium.com/@taylorotwell/package-auto-discovery-in-laravel-5-5-ea9e3ab20518),
102-
* Configurable (with ready-to-use defaults),
100+
* On-the-fly data object conversion,
101+
* API chaining support,
103102
* Localization support,
104-
* Automatic object conversion with custom mapping,
105-
* API chaining/cascading support,
106-
* Includes traits to help [unit test your API code](docs/testing.md),
107-
* Provides own [exception handler helper](docs/exceptions.md) to ensure your API stays consumable even in case of unexpected,
108-
* No extra dependencies.
103+
* Provides traits to help [unit test your API code](docs/testing.md),
104+
* Comes with [exception handler helper](docs/exceptions.md) to ensure your API stays consumable even in case of unexpected,
105+
* No additional dependencies.
109106

110107
----
111108

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": "6.2.3",
5+
"version": "6.3.0",
66
"keywords": [
77
"laravel",
88
"json",

docs/compatibility.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
# REST API Response Builder for Laravel #
44

5-
`ResponseBuilder` follows [Semantic Versioning](http://semver.org/).
6-
7-
85
### v6 ###
96

7+
#### v6.3 ####
8+
* `[BREAKING]` This is backward incompatible change in signature of `ResponseBuilder::buildResponse()`, but it only affects
9+
you if you extend `ResponseBuilder` and provide own implementation to manipulate response object
10+
(see [Manipulating Response Object](docs.md#manipulating-response-object)). If you do not, then you are not affected.
11+
1012
#### v6.2 ####
1113
* `[Very Low]` Data conversion logic changed slightly. Now it checks if we have configuration entry matching **exactly** the
1214
object's class name. If not, then we'd try to find if we have any configuration for its parent class.

docs/docs.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
## Response structure ##
2727

28-
Predictability, simplicity and no special-case is the key of the `ResponseBuilder` design. I wanted to make my life easier not
29-
only when I develop the API itself, but also when I'd try to use it i.e. in mobile applications, therefore all responses created
30-
by this package **guarantee** consistent JSON structure by design.
28+
Predictability, simplicity and no special-case is the key of the `ResponseBuilder` design. I wanted to make my life
29+
easier not only when I develop the API itself, but also when I'd try to use it i.e. in mobile applications,
30+
therefore all responses created by this package **guarantee** consistent JSON structure by design.
3131

3232
By default response always contain at least the following elements:
3333

@@ -45,18 +45,23 @@
4545

4646
* `success` (**boolean**) indicates API method failure or success,
4747
* `code` (**int**) is your own return code (usually used when returning error message or other failure),
48-
* `locale` (**string**) represents locale used for returned error message (obtained automatically via `\App::getLocale()`). This helps processing the response if you support multiple languages,
49-
* `message` (**string**) human readable message that is ready to display and explains human readable explanation of the `code` value,
50-
* `data` (**object**|**array**|**null**) if you return any additional data with your reply, it would end here. If no extra data is needed, that key still be present in the response with `null` value.
48+
* `locale` (**string**) represents locale used for returned error message (obtained automatically via
49+
`\App::getLocale()`). This helps processing the response if you support multiple languages,
50+
* `message` (**string**) human readable message that is ready to display and explains human readable explanation
51+
of the `code` value,
52+
* `data` (**object**|**array**|**null**) if you return any additional data with your reply, it would end here.
53+
If no extra data is needed, that key still be present in the response with `null` value.
5154

52-
**NOTE:** If you need to return other/different elements in the above structure (not in your `data`), see [Manipulating Response Object](#manipulating-response-object) chapter for detailed information about how to achieve this.
55+
**NOTE:** If you need to return other/different elements in the above structure (not in your `data`),
56+
see [Manipulating Response Object](#manipulating-response-object) chapter for detailed information about how
57+
to achieve this.
5358

5459
----
5560

5661
## Usage examples ##
5762

58-
The following examples assume `ResponseBuilder` is properly installed and available to your Laravel application. Installation
59-
steps are described in details in further chapters, if help is needed.
63+
The following examples assume `ResponseBuilder` is properly installed and available to your Laravel application.
64+
Installation steps are described in details in further chapters, if help is needed.
6065

6166
#### Success ####
6267

@@ -438,30 +443,34 @@ $data = [
438443

439444
Minimum requirements:
440445

441-
* PHP 7.2+
442-
* Laravel 6.*
443-
444-
The following PHP extensions are optional but strongly recommended:
445-
446-
* iconv
447-
* mb_string
446+
* PHP 7.2+ with [json extension](https://www.php.net/manual/en/book.json.php),
447+
* Laravel v6.x (see [legacy](docs/legacy.md) for Laravel 5.x support).
448448

449449
----
450450

451451
## Installation and Configuration ##
452452

453453
To install `ResponseBuilder` all you need to do is to open your shell/cmd and do:
454454

455-
composer require marcin-orlowski/laravel-api-response-builder
455+
composer require marcin-orlowski/laravel-api-response-builder:<VERSION>
456+
457+
Where `<VERSION>` string consists of `MAJOR` and `MINOR` release numbers. For
458+
example if current relase is 6.4.13, you need to invoke:
459+
460+
composer require marcin-orlowski/laravel-api-response-builder:6.3
461+
462+
which will add the dependency at the release 6.3 + all the bugfixing releses
463+
(`6.3.*`) but won't automatically pull 6.4 even if available, unless
464+
`composer.json` is updated manually.
456465

457466
If you want to use different configuration than `ResponseBuilder` defaults,
458467
publish and edit configuration file as described in [Configuration file](config.md)
459468
documentation.
460469

461470
#### Setup ####
462471

463-
`ResponseBuilder` supports Laravel's auto-discovery feature and it's ready to use once
464-
installed with default configuration.
472+
`ResponseBuilder` supports Laravel's auto-discovery feature and it's ready to use once
473+
installed.
465474

466475
#### ApiCodes class ####
467476

@@ -546,6 +555,8 @@ class ApiCode {
546555
If you need to return more fields in response object you can simply extend `ResponseBuilder` class
547556
and override `buildResponse()` method.
548557

558+
### Custom response structure ###
559+
549560
For example, you want to get rid of `locale` field and add server time and timezone to returned
550561
responses. First, create `MyResponseBuilder.php` file in `app/` folder (both location and class
551562
name can be anything you wish, just remember to adjust the namespace too) and override
@@ -559,11 +570,12 @@ namespace App;
559570

560571
class MyResponseBuilder extends MarcinOrlowski\ResponseBuilder\ResponseBuilder
561572
{
562-
protected static function buildResponse(bool $success, int $api_code, string $message,
573+
protected static function buildResponse(bool $success, int $api_code,
574+
$message_or_api_code, array $lang_args = null,
563575
$data = null, array $debug_data = null): array
564576
{
565577
// tell ResponseBuilder to do all the heavy lifting first
566-
$response = parent::buildResponse($code, $message, $data);
578+
$response = parent::buildResponse($success, $api_code, $message_or_api_code, $lang_args, $data, $debug_data);
567579

568580
// then do all the tweaks you need
569581
$date = new DateTime();
@@ -575,13 +587,14 @@ class MyResponseBuilder extends MarcinOrlowski\ResponseBuilder\ResponseBuilder
575587
// finally, return what $response holds
576588
return $response;
577589
}
590+
578591
}
579592
```
580593

581594
and from now on use `MyResponseBuilder` class instead of `ResponseBuilder`. As all responses are
582595
always produced with use of `buildResponse()` internally, your **all** responses will be affected
583596
the same way. For example:
584-
597+
585598
```php
586599
MyResponseBuilder::success();
587600
```
@@ -621,6 +634,28 @@ return MyResponseBuilder::errorWithData(ApiCode::SOMETHING_WENT_WRONG, $data);
621634
}
622635
```
623636

637+
### Overriding code to message conversion ###
638+
639+
`ResponseBuilder` automatically provides human readable error messages for each API code used but if for any
640+
reason you want to take control on this, you can now provide own implementation of `ResponseBuilder::getMessageForApiCode()`.
641+
642+
```php
643+
<?php
644+
645+
namespace App;
646+
647+
class MyResponseBuilder extends MarcinOrlowski\ResponseBuilder\ResponseBuilder
648+
{
649+
protected static function getMessageForApiCode(bool $success, int $api_code, array $lang_args = null): string
650+
{
651+
return "My cool message for code {$api_code}";
652+
}
653+
}
654+
```
655+
656+
Please see current implementation for `getMessageForApiCode()` for details how to correctly obtain localization string key etc.
657+
658+
624659
----
625660

626661
## Overriding built-in messages ##

docs/exceptions.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ public const VALIDATION_EXCEPTION = ...;
142142
`ResponseBuilder`'s handler which usually lead to one (or another) handler not being executed
143143
at all.
144144

145-
For example if your API delegates OAuth2 related tasks to popular [lucadegasperi/oauth2-server-laravel](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel)
146-
package, then you must **NOT** use its `OAuthExceptionHandlerMiddleware` class and ensure it is not set,
147-
by inspecting `app/Kernel.php` file and ensuring the following line (if present) is removed or commented out:
145+
For example if your API delegates OAuth2 related tasks to popular
146+
[lucadegasperi/oauth2-server-laravel](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel) package, then you
147+
must **NOT** use its `OAuthExceptionHandlerMiddleware` class and ensure it is not set, by inspecting `app/Kernel.php` file
148+
and ensuring the following line (if present) is removed or commented out:
148149

149150
```php
150151
// remove or comment out

0 commit comments

Comments
 (0)