Skip to content

Commit 9a15037

Browse files
Merge pull request #173 from MarcinOrlowski/dev
Release 9.0.3
2 parents 2fb8544 + 9036d87 commit 9a15037

31 files changed

+336
-116
lines changed

.github/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Number of days of inactivity before an issue becomes stale
2-
daysUntilStale: 21
2+
daysUntilStale: 14
33
# Number of days of inactivity before a stale issue is closed
44
daysUntilClose: 7
55
# Issues with these labels will never be considered stale

CHANGES.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ before doing major upgrade!
88

99
## CHANGE LOG ##
1010

11+
* v9.0.3 (2020-10-27)
12+
* `Validator` type related exceptions must now implement `InvalidTypeExceptionContract`.
13+
* `converter` config `key` element now accepts `null` to indicate you want no key to be used.
14+
1115
* v9.0.2 (2020-10-24)
1216
* Corrected tests to use regular ServiceProvider.
1317
* Corrected primitive converter tests.
14-
* Presence of configuration "converter/classes" array is now mandatory (reported by Raja)
15-
* Extensive documentation overhaul
18+
* Presence of configuration "converter/classes" array is now mandatory (reported by Raja).
19+
* Extensive documentation overhaul.
1620

1721
* v9.0.1 (2020-10-22)
1822
* Fixed auto-discovery failing due to broken `ServiceProvider` (reported by Efriandika Pratama).
1923
* Corrected documentation and usage examples.
2024

2125
* v9.0.0 (2020-10-17)
22-
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
26+
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md)).
2327
* [RB-156] Added logic to deal with directly returned objects or arrays.
2428
* [RB-158] Passing primitives as direct payload (i.e. `success(12.50);` is now supported for `array`, `boolean`,
2529
`double`, `integer` and `string` types, configurable via new `converter/primitives`.
@@ -28,7 +32,7 @@ before doing major upgrade!
2832

2933
* v8.1.1 (2020-10-15)
3034
* [RB-155] Fixed `ResponseBuilder` internals preventing exdending class code from
31-
being invoked, thus making response object structure manipulation ineffective (reported by krek95)
35+
being invoked, thus making response object structure manipulation ineffective (reported by krek95).
3236

3337
* v8.1.0 (2020-09-17)
3438
* Added logging (`.env` controllable) for payload Converter to help debugging inproper data conversion.
@@ -38,7 +42,7 @@ before doing major upgrade!
3842
* Removed `Util::printArray()` helper.
3943

4044
* v8.0.0 (2020-07-14)
41-
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
45+
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md)).
4246
* Improved performance by using calls qualified references.
4347
* [RB-132] Reworked exception handler helper to support delegated handlers for better flexibility.
4448
* Reverted depreciation of `BaseApiCodes` reserved range codes.
@@ -47,30 +51,30 @@ before doing major upgrade!
4751
* Added German localization.
4852

4953
* v7.1.2 (2020-07-12)
50-
* [RB-141] Fixed `JsonSerializableConverter` to deal non-string return data (reported by Jonatan Fekete)
54+
* [RB-141] Fixed `JsonSerializableConverter` to deal non-string return data (reported by Jonatan Fekete).
5155

5256
* v7.1.1 (2020-07-11)
5357
* Added more tests.
5458
* Updated dependencies.
5559
* Switched away from deprecated Codacy coverage package.
5660

5761
* v7.1.0 (2020-03-04)
58-
* Added support for Laravel v7
62+
* Added support for Laravel v7.
5963

6064
* v7.0.3 (2019-12-30)
6165
* Fixed `composer.json` file.
6266

6367
* v7.0.2 (2019-12-29)
64-
* Updated Travis config to cover Laravel 6.5, 6.6, 6.7 and 6.8
65-
* Updated Travis config to cover PHP 7.4
68+
* Updated Travis config to cover Laravel 6.5, 6.6, 6.7 and 6.8.
69+
* Updated Travis config to cover PHP 7.4.
6670

6771
* v7.0.1 (2019-11-25)
6872
* Disabled Scrutinizer's "false positive" in Builder class.
6973
* Added more tests to improve overall coverage.
70-
* Updated PHPDocs
74+
* Updated PHPDocs.
7175

7276
* v7.0.0 (2019-11-22)
73-
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
77+
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md)).
7478
* New, flexible API based on `Builder` pattern (see [docs](docs/compatibility.md) for details).
7579
* Reworked `ExceptionHandlerHelper` configuration. Now, you will be able to easily configure every
7680
HttpException for each HTTP status code you want. Separate `ExceptionHandler::TYPE_HTTP_NOT_FOUND_KEY`

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

docs/config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ php artisan vendor:publish
8383
Meaning of parameters:
8484

8585
* `handler` (mandatory) specifies a full name of the class implementing `ConverterContract`. Object of that class will be
86-
instantiated and conversion method will be invked with object given as argument. The `key` is a string that will be used
86+
instantiated and conversion method will be invoked with object given as argument. The `key` is a string that will be used
8787
as the JSON response as key to array representation when object of that class is passed as direct payload
8888
(i.e. `success($object);`). Note, that `key` is not used otherwise, so if you have i.e. array of objects, they will be
8989
properly converted without `key` used.
90-
* `key` (mandatory) is a string, used by some converters when dealing with object of given class being returned directly
91-
as response payload (i.e. `success($collection)`).
90+
* `key` (mandatory) can be a string or `NULL`. A string is useful for some converters when dealing with an object of a given class being returned directly
91+
as response payload (i.e. `success($collection)`). Otherwise `NULL` can be used to tell `ResponseBuilder` to return object directly, which may be useful.
9292
* `pri` (optional) is an integer being entry's priority (default `0`). Entries with higher values will be matched first. If you got one
9393
class extending another and you want to support both of them with separate configuration, then you **must** ensure child
9494
class has higher priority than it's parent class.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MarcinOrlowski\ResponseBuilder\Contracts;
5+
6+
/**
7+
* Laravel API Response Builder
8+
*
9+
* @package MarcinOrlowski\ResponseBuilder
10+
*
11+
* @author Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12+
* @copyright 2016-2020 Marcin Orlowski
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT
14+
* @link https://github.com/MarcinOrlowski/laravel-api-response-builder
15+
*/
16+
interface InvalidTypeExceptionContract
17+
{
18+
public function __construct(string $var_name, string $type, array $allowed_types);
19+
}

src/Converter.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function getClassMappingConfigOrThrow(object $data): array
9090

9191
// check for exact class name match...
9292
$cls = \get_class($data);
93-
if (\is_string($cls)) {
93+
if ($cls !== false) {
9494
if (\array_key_exists($cls, $this->classes)) {
9595
$result = $this->classes[ $cls ];
9696
$debug_result = 'exact config match';
@@ -121,9 +121,9 @@ protected function getClassMappingConfigOrThrow(object $data): array
121121
/**
122122
* Main entry for data conversion
123123
*
124-
* @param object|array|null $data
124+
* @param mixed|null $data
125125
*
126-
* @return mixed|null
126+
* @return array|null
127127
*/
128128
public function convert($data = null): ?array
129129
{
@@ -145,8 +145,9 @@ public function convert($data = null): ?array
145145
if ($result === null && \is_object($data)) {
146146
$cfg = $this->getClassMappingConfigOrThrow($data);
147147
$worker = new $cfg[ RB::KEY_HANDLER ]();
148-
$result = [$cfg[ RB::KEY_KEY ] => $worker->convert($data, $cfg)];
149-
}
148+
$result = $worker->convert($data, $cfg);
149+
$result = $cfg[ RB::KEY_KEY ] === null ? $result : [$cfg[ RB::KEY_KEY ] => $result];
150+
}
150151

151152
if ($result === null && \is_array($data)) {
152153
$cfg = $this->getPrimitiveMappingConfigOrThrow($data);
@@ -210,19 +211,21 @@ protected static function getClassesMapping(): array
210211

211212
if (!empty($classes)) {
212213
$mandatory_keys = [
213-
RB::KEY_HANDLER,
214-
RB::KEY_KEY,
214+
RB::KEY_HANDLER => [TYPE::STRING],
215+
RB::KEY_KEY => [TYPE::STRING, TYPE::NULL],
215216
];
216217
foreach ($classes as $class_name => $class_config) {
217218
if (!\is_array($class_config)) {
218219
throw new Ex\InvalidConfigurationElementException(
219220
sprintf("Config for '{$class_name}' class must be an array (%s found).", \gettype($class_config)));
220221
}
221-
foreach ($mandatory_keys as $key_name) {
222+
foreach ($mandatory_keys as $key_name => $allowed_types) {
222223
if (!\array_key_exists($key_name, $class_config)) {
223224
throw new Ex\IncompleteConfigurationException(
224225
"Missing '{$key_name}' entry in '{$class_name}' class mapping config.");
225226
}
227+
228+
Validator::assertIsType(RB::KEY_KEY, $class_config[$key_name], $allowed_types);
226229
}
227230
}
228231
}

src/Converters/ArrayableConverter.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace MarcinOrlowski\ResponseBuilder\Converters;
45

@@ -17,21 +18,24 @@
1718
use MarcinOrlowski\ResponseBuilder\Contracts\ConverterContract;
1819
use MarcinOrlowski\ResponseBuilder\Validator;
1920

21+
/**
22+
* Converter for Arrayable class type of objects.
23+
*/
2024
final class ArrayableConverter implements ConverterContract
2125
{
22-
/**
23-
* Returns array representation of the object implementing Arrayable interface
24-
*
25-
* @param Arrayable $obj Object to be converted
26-
* @param array $config Converter config array to be used for this object (based on exact class
27-
* name match or inheritance).
28-
*
29-
* @return array
30-
*/
31-
public function convert(object $obj, /** @scrutinizer ignore-unused */ array $config): array
32-
{
33-
Validator::assertInstanceOf('obj', $obj, Arrayable::class);
26+
/**
27+
* Returns array representation of the object implementing Arrayable interface
28+
*
29+
* @param Arrayable $obj Object to be converted
30+
* @param array $config Converter config array to be used for this object (based on exact class
31+
* name match or inheritance).
32+
*
33+
* @return array
34+
*/
35+
public function convert(object $obj, /** @scrutinizer ignore-unused */ array $config): array
36+
{
37+
Validator::assertInstanceOf('obj', $obj, Arrayable::class);
3438

35-
return $obj->toArray();
36-
}
39+
return $obj->toArray();
40+
}
3741
}

src/Converters/JsonSerializableConverter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
use MarcinOrlowski\ResponseBuilder\Validator;
1919
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
2020

21+
/**
22+
* Converts JsonSerializable to array
23+
*
24+
* @package MarcinOrlowski\ResponseBuilder\Converters
25+
*/
2126
final class JsonSerializableConverter implements ConverterContract
2227
{
2328
/**

src/Converters/ToArrayConverter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use MarcinOrlowski\ResponseBuilder\Contracts\ConverterContract;
1818
use MarcinOrlowski\ResponseBuilder\Validator;
1919

20+
/**
21+
* Generic object-to-array array converter.
22+
*/
2023
final class ToArrayConverter implements ConverterContract
2124
{
2225
/**

src/ExceptionHandlerHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace MarcinOrlowski\ResponseBuilder;
55

66
/**
7-
* Exception handler using ResponseBuilder to return JSON even in such hard tines
7+
* Laravel API Response Builder
88
*
99
* @package MarcinOrlowski\ResponseBuilder
1010
*
@@ -25,7 +25,7 @@
2525
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
2626

2727
/**
28-
* Class ExceptionHandlerHelper
28+
* Exception handler using ResponseBuilder to return JSON even in such hard tines
2929
*/
3030
class ExceptionHandlerHelper
3131
{

0 commit comments

Comments
 (0)