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

jojo1981 / decoder-aggregate / 12712243989

10 Jan 2025 03:19PM UTC coverage: 100.0%. Remained the same
12712243989

push

github

jojo1981
Use GitHub actions workflow for CI instead of Travis CI.

112 of 112 relevant lines covered (100.0%)

4.81 hits per line

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

100.0
/src/Encoder/JsonEncoder.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the jojo1981/decoder-aggregate package
4
 *
5
 * Copyright (c) 2021 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
namespace Jojo1981\DecoderAggregate\Encoder;
11

12
use Jojo1981\DecoderAggregate\EncoderInterface;
13
use Jojo1981\DecoderAggregate\Exception\JsonEncoderException;
14
use Throwable;
15
use function json_encode;
16

17
/**
18
 * @package Jojo1981\DecoderAggregate\Encoder
19
 */
20
final class JsonEncoder implements EncoderInterface
21
{
22
    /** @var int */
23
    private int $flags;
24

25
    /** @var int */
26
    private int $depth;
27

28
    /**
29
     * @param int $flags
30
     * @param int $depth
31
     */
32
    public function __construct(int $flags = 0, int $depth = 512)
33
    {
34
        $this->flags = $flags;
7✔
35
        $this->depth = $depth;
7✔
36
    }
37

38
    /**
39
     * @param mixed $data
40
     * @param array $options
41
     * @return string
42
     * @throws JsonEncoderException
43
     */
44
    public function encode($data, array $options = []): string
45
    {
46
        $options['flags'] = $options['flags'] ?? $this->flags;
5✔
47
        $options['depth'] = $options['depth'] ?? $this->depth;
5✔
48
        if ($options['flags'] & JSON_THROW_ON_ERROR) {
5✔
49
            $options['flags'] -= JSON_THROW_ON_ERROR;
1✔
50
        }
51

52
        try {
53
            $result = json_encode($data, $options['flags'] + JSON_THROW_ON_ERROR, $options['depth']);
5✔
54
        } catch (Throwable $exception) {
1✔
55
            throw new JsonEncoderException('Could not encode data into a json string', 0, $exception);
1✔
56
        }
57

58
        return $result;
4✔
59
    }
60
}
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

© 2026 Coveralls, Inc