1
|
|
|
<?php namespace Neomerx\JsonApi\Http\Headers; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
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
|
15 |
|
public function __construct(HttpFactoryInterface $factory) |
45
|
|
|
{ |
46
|
15 |
|
$this->factory = $factory; |
47
|
15 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
* |
52
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
53
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
54
|
|
|
*/ |
55
|
15 |
|
public function parse(ServerRequestInterface $request, $checkContentType = true) |
56
|
|
|
{ |
57
|
15 |
|
$acceptHeader = null; |
58
|
15 |
|
$contentTypeHeader = null; |
59
|
|
|
|
60
|
15 |
|
if ($checkContentType === true) { |
61
|
|
|
try { |
62
|
14 |
|
$header = $this->getHeader($request, HeaderInterface::HEADER_CONTENT_TYPE); |
63
|
14 |
|
$contentTypeHeader = Header::parse($header, HeaderInterface::HEADER_CONTENT_TYPE); |
64
|
14 |
|
} catch (InvalidArgumentException $exception) { |
65
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
66
|
|
|
} |
67
|
13 |
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
14 |
|
$header = $this->getHeader($request, HeaderInterface::HEADER_ACCEPT); |
71
|
14 |
|
$acceptHeader = AcceptHeader::parse($header); |
72
|
14 |
|
} catch (InvalidArgumentException $exception) { |
73
|
1 |
|
E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception)); |
74
|
|
|
} |
75
|
|
|
|
76
|
13 |
|
$method = $request->getMethod(); |
77
|
|
|
|
78
|
13 |
|
return $checkContentType === true ? |
79
|
13 |
|
$this->factory->createHeaderParameters($method, $acceptHeader, $contentTypeHeader): |
80
|
13 |
|
$this->factory->createNoContentHeaderParameters($method, $acceptHeader); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ServerRequestInterface $request |
85
|
|
|
* @param string $name |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
15 |
|
private function getHeader(ServerRequestInterface $request, $name) |
90
|
|
|
{ |
91
|
15 |
|
$value = $request->getHeader($name); |
92
|
15 |
|
if (empty($value) === false) { |
93
|
15 |
|
$value = $value[0]; |
94
|
15 |
|
if (empty($value) === false) { |
95
|
15 |
|
return $value; |
96
|
|
|
} |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
return MediaTypeInterface::JSON_API_MEDIA_TYPE; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|