|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Representation; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Representation\ErrorWriterInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @package Neomerx\JsonApi |
|
27
|
|
|
*/ |
|
28
|
|
|
class ErrorWriter extends BaseWriter implements ErrorWriterInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
*/ |
|
33
|
7 |
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
7 |
|
parent::__construct(); |
|
36
|
|
|
|
|
37
|
7 |
|
$this->data[DocumentInterface::KEYWORD_ERRORS] = []; |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function addError(ErrorInterface $error): ErrorWriterInterface |
|
44
|
|
|
{ |
|
45
|
6 |
|
$representation = \array_filter([ |
|
46
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_ID => $error->getId(), |
|
47
|
6 |
|
DocumentInterface::KEYWORD_LINKS => $this->getErrorLinksRepresentation($error), |
|
48
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_STATUS => $error->getStatus(), |
|
49
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_CODE => $error->getCode(), |
|
50
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_TITLE => $error->getTitle(), |
|
51
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_DETAIL => $error->getDetail(), |
|
52
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_SOURCE => $error->getSource(), |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
6 |
|
if ($error->hasMeta() === true) { |
|
56
|
4 |
|
$representation[DocumentInterface::KEYWORD_ERRORS_META] = $error->getMeta(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// There is a special case when error representation is an empty array |
|
60
|
|
|
// Due to further json transform it must be an object otherwise it will be an empty array in json |
|
61
|
6 |
|
$representation = empty($representation) === false ? $representation : (object)$representation; |
|
62
|
|
|
|
|
63
|
6 |
|
$this->data[DocumentInterface::KEYWORD_ERRORS][] = $representation; |
|
64
|
|
|
|
|
65
|
6 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param ErrorInterface $error |
|
70
|
|
|
* |
|
71
|
|
|
* @return array|null |
|
72
|
|
|
* |
|
73
|
|
|
* @SuppressWarnings(PHPMD.IfStatementAssignment) |
|
74
|
|
|
*/ |
|
75
|
6 |
|
private function getErrorLinksRepresentation(ErrorInterface $error): ?array |
|
76
|
|
|
{ |
|
77
|
6 |
|
$linksRepresentation = null; |
|
78
|
|
|
|
|
79
|
6 |
|
if (($value = $error->getLinks()) !== null) { |
|
80
|
4 |
|
$linksRepresentation = $this->getLinksRepresentation($this->getUrlPrefix(), $value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
if (($value = $error->getTypeLinks()) !== null) { |
|
84
|
4 |
|
$linksRepresentation[DocumentInterface::KEYWORD_ERRORS_TYPE] = |
|
85
|
4 |
|
$this->getLinksListRepresentation($this->getUrlPrefix(), $value); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
6 |
|
return $linksRepresentation; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|