JsonSerializableConverter::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder\Converters;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2021 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
17
use MarcinOrlowski\ResponseBuilder\Contracts\ConverterContract;
18
use MarcinOrlowski\ResponseBuilder\Validator;
19
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
20
21
/**
22
 * Converts JsonSerializable to array
23
 *
24
 * @package MarcinOrlowski\ResponseBuilder\Converters
25
 */
26
final class JsonSerializableConverter implements ConverterContract
27
{
28
	/**
29
	 * Returns array representation of the object implementing \JsonSerializable interface.
30
	 *
31 1
	 * @param \JsonSerializable $obj    Object to be converted
32
	 * @param array             $config Converter config array to be used for this object (based on exact class
33 1
	 *                                  name match or inheritance).
34
	 *
35 1
	 * @return array
36
	 */
37
	public function convert(object $obj, /** @scrutinizer ignore-unused */ array $config): array
38
	{
39
		Validator::assertInstanceOf('obj', $obj, \JsonSerializable::class);
40
41
		return [$config[RB::KEY_KEY] => \json_decode(\json_encode($obj->jsonSerialize()), true)];
42
	}
43
}
44