InputDataCannotBeEncoded::encountered()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Json;
5
6
use InvalidArgumentException as InvalidArgument;
7
use function sprintf;
8
use Throwable;
9
10
/**
11
 * Exception to notify that the input data cannot be encoded in json format.
12
 *
13
 * @author Stratadox
14
 */
15
final class InputDataCannotBeEncoded extends InvalidArgument implements InvalidJson
16
{
17
    /**
18
     * Produces an `Invalid Json` type exception, to throw when encoding the
19
     * input caused an exception.
20
     *
21
     * @param Throwable $exception The exception that was thrown while encoding.
22
     * @return InvalidJson         The Json context-specific exception to throw.
23
     */
24
    public static function encountered(Throwable $exception): InvalidJson
25
    {
26
        return new self(sprintf(
27
            'Encoding the input caused an exception: %s',
28
            $exception->getMessage()
29
        ));
30
    }
31
32
    /**
33
     * Produces an `Invalid Json` type exception, to throw when encoding the
34
     * input caused an error.
35
     *
36
     * @param string $error The Json error that was found.
37
     * @return InvalidJson  The Json context-specific exception to throw.
38
     */
39
    public static function found(string $error): InvalidJson
40
    {
41
        return new self(sprintf(
42
            'Encoding the input caused an error: %s.',
43
            $error
44
        ));
45
    }
46
}
47