Completed
Branch release/1.0.0 (faa89b)
by Yo
01:45
created

ErrorDoc::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServerDoc\Domain\Model;
3
4
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
5
6
/**
7
 * Class ErrorDoc
8
 */
9
class ErrorDoc
10
{
11
    /** @var string */
12
    private $title;
13
    /** @var string */
14
    private $identifier;
15
    /** @var int */
16
    private $code;
17
    /** @var string|null */
18
    private $message = null;
19
    /** @var TypeDoc|null */
20
    private $dataDoc = null;
21
22
    /**
23
     * @param string       $title
24
     * @param int          $code
25
     * @param string|null  $message
26
     * @param TypeDoc|null $dataDoc
27
     * @param string|null  $identifier
28
     */
29 12
    public function __construct(
30
        string $title,
31
        int $code,
32
        string $message = null,
33
        TypeDoc $dataDoc = null,
34
        string $identifier = null
35
    ) {
36 12
        $this->title = $title;
37 12
        $this->code = $code;
38 12
        $this->message = $message;
39 12
        $this->dataDoc = $dataDoc;
40
        // Use title and code as id if not provided
41 12
        $this->setIdentifier($identifier ?? $title.((string)$code));
42 12
    }
43
44
    /**
45
     * @param string $identifier
46
     *
47
     * @return ErrorDoc
48
     */
49 12
    public function setIdentifier(string $identifier) : ErrorDoc
50
    {
51
        // Sanitize Identifier => remove space and slashes
52 12
        $this->identifier = strtr(
53 12
            ucwords(strtr(
54 12
                $identifier,
55 12
                ['_' => ' ', '/' => '-', '.' => '_ ', '\\' => '_ ']
56
            )),
57 12
            [' ' => '']
58
        );
59
60 12
        return $this;
61
    }
62
63
    /**
64
     * @param TypeDoc $dataDoc
65
     *
66
     * @return ErrorDoc
67
     */
68 1
    public function setDataDoc(TypeDoc $dataDoc) : ErrorDoc
69
    {
70 1
        $this->dataDoc = $dataDoc;
71
72 1
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function getTitle() : string
79
    {
80 1
        return $this->title;
81
    }
82
83
    /**
84
     * @return int
85
     */
86 1
    public function getCode() : int
87
    {
88 1
        return $this->code;
89
    }
90
91
    /**
92
     * @return string|null
93
     */
94 2
    public function getMessage()
95
    {
96 2
        return $this->message;
97
    }
98
99
    /**
100
     * @return TypeDoc|null
101
     */
102 3
    public function getDataDoc()
103
    {
104 3
        return $this->dataDoc;
105
    }
106
107
    /**
108
     * @return string
109
     */
110 7
    public function getIdentifier() : string
111
    {
112 7
        return $this->identifier;
113
    }
114
}
115