Passed
Push — main ( 72c6fa...9344f0 )
by Sílvio
02:59
created

BaseException::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silviooosilva\CacheerPhp\Exceptions;
6
7
use Exception;
8
use JsonSerializable;
9
10
class BaseException extends Exception implements JsonSerializable
11
{
12
    /** @param array $details */
13
    protected array $details;
14
15
    public function __construct(string $message = "", int $code = 0, ?Exception $previous = null, array $details = [])
16
    {
17
        parent::__construct($message, $code, $previous);
18
        $this->details = $details;
19
    }
20
21
    /** @return array */
22
    public function getDetails()
23
    {
24
        return $this->details;
25
    }
26
27
    /** @return void */
28
    public function setDetails(array $details)
29
    {
30
        $this->details = $details;
31
    }
32
33
    /** @return array */
34
    public function jsonSerialize(): array
35
    {
36
        return [
37
            'message' => $this->getMessage(),
38
            'code' => $this->getCode(),
39
            'details' => $this->getDetails(),
40
            'file' => $this->getFile(),
41
            'line' => $this->getLine(),
42
            'trace' => $this->getTrace()
43
        ];
44
    }
45
46
    public function __toString(): string
47
    {
48
        return sprintf(
49
            "[%s] %s in %s on line %d\nDetails: %s",
50
            $this->getCode(),
51
            $this->getMessage(),
52
            $this->getFile(),
53
            $this->getLine(),
54
            json_encode($this->getDetails(), JSON_PRETTY_PRINT)
55
        );
56
    }
57
}
58