Passed
Push — main ( cd5745...cfa081 )
by Sílvio
10:20 queued 07:29
created

BaseException::__toString()   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
    /** 
13
     * @var array 
14
     * */
15
    protected array $details;
16
17
    public function __construct(string $message = "", int $code = 0, ?Exception $previous = null, array $details = [])
18
    {
19
        parent::__construct($message, $code, $previous);
20
        $this->details = $details;
21
    }
22
23
    /** 
24
     * @return array 
25
     * */
26
    public function getDetails()
27
    {
28
        return $this->details;
29
    }
30
31
    /** 
32
     * @param array $details 
33
     * */
34
    public function setDetails(array $details)
35
    {
36
        $this->details = $details;
37
    }
38
39
    /** 
40
     * @return array 
41
     * */
42
    public function toArray()
43
    {
44
        return [
45
            'type' => static::class,
46
            'message' => $this->getMessage(),
47
            'code' => $this->getCode(),
48
            'details' => $this->getDetails(),
49
            'file' => $this->getFile(),
50
            'line' => $this->getLine(),
51
            'trace' => $this->getTrace()
52
        ];
53
    }
54
55
    /** 
56
     * @return array 
57
     * */
58
    public function jsonSerialize(): array
59
    {
60
        return $this->toArray();
61
    }
62
63
    /** 
64
     * @return string 
65
     * */
66
    public function toJson(int $options = 0)
67
    {
68
        return json_encode($this->toArray(), $options | JSON_THROW_ON_ERROR);
69
    }
70
71
    /** 
72
     * @return string 
73
     * */
74
    public function __toString()
75
    {
76
        return sprintf(
77
            "[%s] %s in %s on line %d\nDetails: %s",
78
            $this->getCode(),
79
            $this->getMessage(),
80
            $this->getFile(),
81
            $this->getLine(),
82
            json_encode($this->getDetails(), JSON_PRETTY_PRINT)
83
        );
84
    }
85
}
86