1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Representation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [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\Factories\FactoryInterface; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Representation\ErrorWriterInterface; |
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface; |
24
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Neomerx\JsonApi |
28
|
|
|
*/ |
29
|
|
|
class ErrorWriter extends BaseWriter implements ErrorWriterInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
7 |
|
public function __construct(FactoryInterface $factory) |
35
|
|
|
{ |
36
|
7 |
|
parent::__construct($factory); |
37
|
|
|
|
38
|
7 |
|
$this->data[DocumentInterface::KEYWORD_ERRORS] = []; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
6 |
|
public function addError(ErrorInterface $error): ErrorWriterInterface |
45
|
|
|
{ |
46
|
6 |
|
$representation = array_filter([ |
47
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_ID => $error->getId(), |
48
|
6 |
|
DocumentInterface::KEYWORD_LINKS => $this->getErrorLinksRepresentation($error), |
49
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_STATUS => $error->getStatus(), |
50
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_CODE => $error->getCode(), |
51
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_TITLE => $error->getTitle(), |
52
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_DETAIL => $error->getDetail(), |
53
|
6 |
|
DocumentInterface::KEYWORD_ERRORS_SOURCE => $error->getSource(), |
54
|
|
|
]); |
55
|
|
|
|
56
|
6 |
|
if ($error->hasMeta() === true) { |
57
|
4 |
|
$representation[DocumentInterface::KEYWORD_ERRORS_META] = $error->getMeta(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// There is a special case when error representation is an empty array |
61
|
|
|
// Due to further json transform it must be an object otherwise it will be an empty array in json |
62
|
6 |
|
$representation = empty($representation) === false ? $representation : (object)$representation; |
63
|
|
|
|
64
|
6 |
|
$this->data[DocumentInterface::KEYWORD_ERRORS][] = $representation; |
65
|
|
|
|
66
|
6 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ErrorInterface $error |
71
|
|
|
* |
72
|
|
|
* @return array|null |
73
|
|
|
*/ |
74
|
6 |
|
private function getErrorLinksRepresentation(ErrorInterface $error): ?array |
75
|
|
|
{ |
76
|
6 |
|
$linksRepresentation = null; |
77
|
|
|
|
78
|
6 |
|
if (($value = $error->getLinks()) !== null) { |
79
|
4 |
|
$linksRepresentation = $this->getLinksRepresentation($this->getUrlPrefix(), $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
if (($value = $error->getTypeLinks()) !== null) { |
83
|
4 |
|
$linksRepresentation[DocumentInterface::KEYWORD_ERRORS_TYPE] = |
84
|
4 |
|
$this->getLinksListRepresentation($this->getUrlPrefix(), $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
return $linksRepresentation; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|