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
|
13 |
|
public function __construct(HttpFactoryInterface $factory) |
45
|
|
|
{ |
46
|
13 |
|
$this->factory = $factory; |
47
|
13 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
13 |
|
public function parse(ServerRequestInterface $request) |
53
|
|
|
{ |
54
|
13 |
|
$acceptHeader = null; |
55
|
13 |
|
$contentTypeHeader = null; |
56
|
|
|
|
57
|
13 |
|
$method = $request->getMethod(); |
58
|
|
|
|
59
|
|
|
try { |
60
|
13 |
|
$header = $this->getHeader($request, HeaderInterface::HEADER_CONTENT_TYPE); |
61
|
13 |
|
$contentTypeHeader = Header::parse($header, HeaderInterface::HEADER_CONTENT_TYPE); |
62
|
13 |
|
} catch (InvalidArgumentException $exception) { |
63
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
12 |
|
$header = $this->getHeader($request, HeaderInterface::HEADER_ACCEPT); |
68
|
12 |
|
$acceptHeader = AcceptHeader::parse($header); |
69
|
12 |
|
} catch (InvalidArgumentException $exception) { |
70
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
return $this->factory->createHeaderParameters($method, $acceptHeader, $contentTypeHeader); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ServerRequestInterface $request |
78
|
|
|
* @param string $name |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
13 |
|
private function getHeader(ServerRequestInterface $request, $name) |
83
|
|
|
{ |
84
|
13 |
|
$value = $request->getHeader($name); |
85
|
13 |
|
if (empty($value) === false) { |
86
|
13 |
|
$value = $value[0]; |
87
|
13 |
|
if (empty($value) === false) { |
88
|
13 |
|
return $value; |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
2 |
|
return MediaTypeInterface::JSON_API_MEDIA_TYPE; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: