Completed
Push — master ( 22a756...a8d83f )
by John
02:03
created

src/Body/JsonBodySerializer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\PhpApi\Middleware\Body;
9
10 View Code Duplication
class JsonBodySerializer implements BodySerializer
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @param mixed $body
14
     * @return mixed
15
     * @throws JsonException
16
     */
17
    public function serialize($body): string
18
    {
19
        // Clear json_last_error()
20
        json_encode(null);
21
22
        $string = json_encode($body);
23
24
        if (JSON_ERROR_NONE !== json_last_error()) {
25
            throw new JsonException(json_last_error_msg());
26
        }
27
28
        return $string;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getContentType(): string
35
    {
36
        return 'application/json';
37
    }
38
}