-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathErrorSource.php
66 lines (57 loc) · 1.9 KB
/
ErrorSource.php
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
<?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 Source Object
*
* @see http://jsonapi.org/format/#error-objects
*/
final class ErrorSource extends AbstractElement
{
/**
* Parses the data for this element
*
* @throws ValidationException
*/
protected function parse(mixed $object): void
{
if (!is_object($object)) {
throw new ValidationException('ErrorSource has to be an object, "' . gettype($object) . '" given.');
}
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'pointer') {
if (!is_string($value)) {
throw new ValidationException('property "pointer" has to be a string, "' . gettype($value) . '" given.');
}
$this->set('pointer', strval($value));
} elseif ($key === 'parameter') {
if (!is_string($value)) {
throw new ValidationException('property "parameter" has to be a string, "' . gettype($value) . '" given.');
}
$this->set('parameter', strval($value));
} else {
$this->set($key, $value);
}
}
}
/**
* Get a value by the key of this document
*
* @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 source.');
}
}
}