-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathError.php
More file actions
109 lines (97 loc) · 3.41 KB
/
Copy pathError.php
File metadata and controls
109 lines (97 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt
//
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Art4\JsonApiClient\V1;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\Helper\AbstractElement;
use Art4\JsonApiClient\Helper\AccessKey;
/**
* Error Object
*
* @see http://jsonapi.org/format/#error-objects
*/
final class Error extends AbstractElement
{
/**
* Parses the data for this element
*
* @throws ValidationException
*/
protected function parse(mixed $object): void
{
if (!is_object($object)) {
throw new ValidationException(
'Error has to be an object, "' . gettype($object) . '" given.'
);
}
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'id') {
if (!is_string($value)) {
throw new ValidationException(
'property "id" has to be a string, "' .
gettype($value) . '" given.'
);
}
$this->set('id', strval($value));
} elseif ($key === 'links') {
$this->set('links', $this->create('ErrorLink', $value));
} elseif ($key === 'status') {
if (!is_string($value)) {
throw new ValidationException(
'property "status" has to be a string, "' .
gettype($value) . '" given.'
);
}
$this->set('status', strval($value));
} elseif ($key === 'code') {
if (!is_string($value)) {
throw new ValidationException(
'property "code" has to be a string, "' .
gettype($value) . '" given.'
);
}
$this->set('code', strval($value));
} elseif ($key === 'title') {
if (!is_string($value)) {
throw new ValidationException(
'property "title" has to be a string, "' .
gettype($value) . '" given.'
);
}
$this->set('title', strval($value));
} elseif ($key === 'detail') {
if (!is_string($value)) {
throw new ValidationException(
'property "detail" has to be a string, "' .
gettype($value) . '" given.'
);
}
$this->set('detail', strval($value));
} elseif ($key === 'source') {
$this->set('source', $this->create('ErrorSource', $value));
} elseif ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} else {
$this->set($key, $value);
}
}
}
/**
* Get a value by the key of this object
*
* @param int|string|AccessKey<string> $key The key of the value
*/
public function get($key): mixed
{
try {
return parent::get($key);
} catch (AccessException $e) {
throw new AccessException(
'"' . $key . '" doesn\'t exist in this error object.'
);
}
}
}