• 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/Decoder/JsonDecoder.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\Decoder;
11

12
use Jojo1981\DecoderAggregate\DecoderInterface;
13
use Jojo1981\DecoderAggregate\Exception\JsonDecodeException;
14
use Throwable;
15
use function json_decode;
16

17
/**
18
 * @package Jojo1981\DecoderAggregate\Decoder
19
 */
20
final class JsonDecoder implements DecoderInterface
21
{
22
    /** @var bool */
23
    private bool $associative;
24

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

28
    /** @var int */
29
    private int $flags;
30

31
    /**
32
     * @param bool $associative
33
     * @param int $depth
34
     * @param int $flags
35
     */
36
    public function __construct(bool $associative = false, int $depth = 512, int $flags = 0)
37
    {
38
        $this->associative = $associative;
7✔
39
        $this->depth = $depth;
7✔
40
        $this->flags = $flags;
7✔
41
    }
42

43
    /**
44
     * @param string $encodedString
45
     * @param array $options
46
     * @return mixed
47
     * @throws JsonDecodeException
48
     */
49
    public function decode(string $encodedString, array $options = [])
50
    {
51
        $options['associative'] = $options['associative'] ?? $this->associative;
6✔
52
        $options['depth'] = $options['depth'] ?? $this->depth;
6✔
53
        $options['flags'] = $options['flags'] ?? $this->flags;
6✔
54

55
        if ($options['flags'] & JSON_THROW_ON_ERROR) {
6✔
56
            $options['flags'] -= JSON_THROW_ON_ERROR;
1✔
57
        }
58

59
        try {
60
            $result = json_decode($encodedString, $options['associative'], $options['depth'], $options['flags'] + JSON_THROW_ON_ERROR);
6✔
61
        } catch (Throwable $exception) {
1✔
62
            throw new JsonDecodeException('Could not decode json string', 0, $exception);
1✔
63
        }
64

65
        return $result;
5✔
66
    }
67
}
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