1
|
|
|
<?php namespace Neomerx\JsonApi\Http\Headers; |
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 \InvalidArgumentException; |
20
|
|
|
use \Psr\Log\LoggerAwareTrait; |
21
|
|
|
use \Psr\Log\LoggerAwareInterface; |
22
|
|
|
use \Psr\Http\Message\ServerRequestInterface; |
23
|
|
|
use \Neomerx\JsonApi\Exceptions\JsonApiException as E; |
24
|
|
|
use \Neomerx\JsonApi\Contracts\Http\HttpFactoryInterface; |
25
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface; |
26
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
27
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Neomerx\JsonApi |
31
|
|
|
*/ |
32
|
|
|
class HeaderParametersParser implements HeaderParametersParserInterface, LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use LoggerAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var HttpFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param HttpFactoryInterface $factory |
43
|
|
|
*/ |
44
|
12 |
|
public function __construct(HttpFactoryInterface $factory) |
45
|
|
|
{ |
46
|
12 |
|
$this->factory = $factory; |
47
|
12 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
12 |
|
public function parse(ServerRequestInterface $request) |
53
|
|
|
{ |
54
|
12 |
|
$acceptHeader = null; |
55
|
12 |
|
$contentTypeHeader = null; |
56
|
|
|
|
57
|
12 |
|
$method = $request->getMethod(); |
58
|
|
|
|
59
|
|
|
try { |
60
|
12 |
|
$contentType = $request->getHeader(HeaderInterface::HEADER_CONTENT_TYPE); |
61
|
12 |
|
$contentTypeHeader = Header::parse( |
62
|
12 |
|
empty($contentType) === true ? MediaTypeInterface::JSON_API_MEDIA_TYPE : $contentType[0], |
63
|
|
|
HeaderInterface::HEADER_CONTENT_TYPE |
64
|
12 |
|
); |
65
|
12 |
|
} catch (InvalidArgumentException $exception) { |
66
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
11 |
|
$headerString = $request->getHeader(HeaderInterface::HEADER_ACCEPT); |
71
|
11 |
|
if (empty($headerString) === false) { |
72
|
10 |
|
$acceptHeader = AcceptHeader::parse($headerString[0]); |
73
|
9 |
|
} else { |
74
|
1 |
|
$jsonMediaType = $this->factory->createAcceptMediaType( |
75
|
1 |
|
0, |
76
|
1 |
|
MediaTypeInterface::JSON_API_TYPE, |
77
|
|
|
MediaTypeInterface::JSON_API_SUB_TYPE |
78
|
1 |
|
); |
79
|
1 |
|
$acceptHeader = $this->factory->createAcceptHeader([$jsonMediaType]); |
80
|
|
|
} |
81
|
11 |
|
} catch (InvalidArgumentException $exception) { |
82
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
83
|
|
|
} |
84
|
|
|
|
85
|
10 |
|
return $this->factory->createHeaderParameters($method, $acceptHeader, $contentTypeHeader); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.