1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Resource\Error; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\AbstractResourceSupport; |
17
|
|
|
use FiveLab\Component\Resource\Resource\Href\HrefInterface; |
18
|
|
|
use FiveLab\Component\Resource\Resource\Relation\Relation; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Default error resource. |
22
|
|
|
* |
23
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ErrorResource extends AbstractResourceSupport implements ErrorResourceInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The identifier of error. |
29
|
|
|
* Can use for multiple errors for split errors. |
30
|
|
|
* |
31
|
|
|
* @var string|int |
32
|
|
|
*/ |
33
|
|
|
private $identifier; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The reason of error. |
37
|
|
|
* |
38
|
|
|
* @var string|int |
39
|
|
|
*/ |
40
|
|
|
private $reason; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The custom message of error. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $message; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The custom attributes for this error. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $attributes; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The path of this error. |
58
|
|
|
* As an example: the name of invalid form field. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $path; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor. |
66
|
|
|
* |
67
|
|
|
* @param string $message |
68
|
|
|
* @param string|int $reason |
69
|
|
|
* @param string $path |
70
|
|
|
* @param array $attributes |
71
|
|
|
* @param string|int $identifier |
72
|
|
|
*/ |
73
|
16 |
|
public function __construct(string $message, $reason = null, string $path = null, array $attributes = [], $identifier = null) |
74
|
|
|
{ |
75
|
16 |
|
parent::__construct(); |
76
|
|
|
|
77
|
16 |
|
$this->message = $message; |
78
|
16 |
|
$this->reason = $reason; |
79
|
16 |
|
$this->path = $path; |
80
|
16 |
|
$this->attributes = $attributes; |
81
|
16 |
|
$this->identifier = $identifier; |
82
|
16 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
6 |
|
public function getMessage(): string |
88
|
|
|
{ |
89
|
6 |
|
return $this->message; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
5 |
|
public function getReason() |
96
|
|
|
{ |
97
|
5 |
|
return $this->reason; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
5 |
|
public function getIdentifier() |
104
|
|
|
{ |
105
|
5 |
|
return $this->identifier; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
5 |
|
public function getPath(): ?string |
112
|
|
|
{ |
113
|
5 |
|
return $this->path; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
4 |
|
public function getAttributes(): array |
120
|
|
|
{ |
121
|
4 |
|
return $this->attributes; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1 |
|
public function addHelp(HrefInterface $href): void |
128
|
|
|
{ |
129
|
1 |
|
$relation = new Relation('help', $href); |
130
|
|
|
|
131
|
1 |
|
$this->addRelation($relation); |
132
|
1 |
|
} |
133
|
|
|
} |
134
|
|
|
|