1
|
|
|
<?php namespace Neomerx\Limoncello\Errors; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use \Closure; |
20
|
|
|
use \Exception; |
21
|
|
|
use \Neomerx\JsonApi\Exceptions\BaseRenderer; |
22
|
|
|
use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface; |
23
|
|
|
use \Neomerx\JsonApi\Contracts\Responses\ResponsesInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Neomerx\Limoncello |
27
|
|
|
*/ |
28
|
|
|
class ConvertContentRenderer extends BaseRenderer |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var CodecMatcherInterface |
32
|
|
|
*/ |
33
|
|
|
private $codecMatcher; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Closure |
37
|
|
|
*/ |
38
|
|
|
private $converter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param CodecMatcherInterface $codecMatcher |
42
|
|
|
* @param ResponsesInterface $responses |
43
|
|
|
* @param int $statusCode |
44
|
|
|
* @param Closure $converter Exception to JSON API Error converter |
45
|
|
|
*/ |
46
|
1 |
|
public function __construct( |
47
|
|
|
CodecMatcherInterface $codecMatcher, |
48
|
|
|
ResponsesInterface $responses, |
49
|
|
|
$statusCode, |
50
|
|
|
Closure $converter |
51
|
|
|
) { |
52
|
1 |
|
parent::__construct($responses); |
53
|
|
|
|
54
|
1 |
|
$this->converter = $converter; |
55
|
1 |
|
$this->codecMatcher = $codecMatcher; |
56
|
|
|
|
57
|
1 |
|
$this->withStatusCode($statusCode); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
1 |
|
public function getContent(Exception $exception) |
64
|
|
|
{ |
65
|
1 |
|
$converter = $this->converter; |
66
|
1 |
|
$errors = $converter($exception); |
67
|
1 |
|
$encoder = $this->codecMatcher->getEncoder(); |
68
|
1 |
|
$content = is_array($errors) === true ? $encoder->encodeErrors($errors) : $encoder->encodeError($errors); |
69
|
|
|
|
70
|
1 |
|
return $content; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|