• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DoclerLabs / api-client-generator / 7259428899

19 Dec 2023 08:47AM UTC coverage: 89.733% (-0.3%) from 90.043%
7259428899

push

github

web-flow
Merge pull request #102 from DoclerLabs/generator-php-8

generator php 8

355 of 381 new or added lines in 40 files covered. (93.18%)

6 existing lines in 4 files now uncovered.

2657 of 2961 relevant lines covered (89.73%)

3.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

24.0
/src/Output/Copy/Serializer/ContentType/AbstractJsonContentTypeSerializer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType;
6

7
use DoclerLabs\ApiClientGenerator\Output\Copy\Schema\SerializableInterface;
8
use Psr\Http\Message\StreamInterface;
9

10
abstract class AbstractJsonContentTypeSerializer implements ContentTypeSerializerInterface
11
{
12
    private const JSON_DEPTH = 512;
13

14
    private const JSON_OPTIONS = JSON_BIGINT_AS_STRING | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_UNICODE;
15

16
    /**
17
     * @throws SerializeException
18
     */
19
    public function encode(SerializableInterface $body): string
20
    {
21
        $encodedData = json_encode($body->toArray(), self::JSON_OPTIONS);
×
22

23
        $lastErrorCode = json_last_error();
×
24
        if ($lastErrorCode === JSON_ERROR_NONE && $encodedData !== false) {
×
25
            return $encodedData;
×
26
        }
27

28
        throw new SerializeException('JSON encode error: ' . $this->getErrorMessage($lastErrorCode));
×
29
    }
30

31
    /**
32
     * @throws SerializeException
33
     */
34
    public function decode(StreamInterface $body): array
35
    {
36
        $body->rewind();
4✔
37

38
        // According to RFC7159 a JSON value MUST be an object, array, number, string,
39
        // or one of the following three literal names: false, null, true.
40
        $result = json_decode($body->getContents(), true, self::JSON_DEPTH, self::JSON_OPTIONS);
4✔
41

42
        $lastErrorCode = json_last_error();
4✔
43
        if ($lastErrorCode === JSON_ERROR_NONE) {
4✔
44
            if (!is_array($result)) {
4✔
UNCOV
45
                $result = [
×
46
                    ContentTypeSerializerInterface::LITERAL_VALUE_KEY => $result,
×
47
                ];
48
            }
49

50
            return $result;
4✔
51
        }
52

53
        throw new SerializeException('JSON decode error: ' . $this->getErrorMessage($lastErrorCode));
×
54
    }
55

56
    private function getErrorMessage(int $errorCode): string
57
    {
UNCOV
58
        $errorMessages = [
×
59
            JSON_ERROR_NONE             => 'No errors',
×
60
            JSON_ERROR_DEPTH            => 'Maximum stack depth exceeded',
×
61
            JSON_ERROR_STATE_MISMATCH   => 'Underflow or the modes mismatch',
×
62
            JSON_ERROR_CTRL_CHAR        => 'Unexpected control character found',
×
63
            JSON_ERROR_SYNTAX           => 'Syntax error, malformed JSON',
×
64
            JSON_ERROR_UTF8             => 'Malformed UTF-8 characters, possibly incorrectly encoded',
×
65
            JSON_ERROR_RECURSION        => 'Recursive reference was found',
×
66
            JSON_ERROR_INF_OR_NAN       => 'NaN or Inf was found',
×
67
            JSON_ERROR_UNSUPPORTED_TYPE => 'Unsupported type was found',
×
68
        ];
69

70
        return $errorMessages[$errorCode] ?? 'Unknown error';
×
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc