Skip to content

Commit 5f5784c

Browse files
committed
[Refactor] Update responses and header parsing
1 parent 4f710e6 commit 5f5784c

File tree

11 files changed

+545
-117
lines changed

11 files changed

+545
-117
lines changed

src/Codec/Codec.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,17 @@ public function getEncoder(): EncoderInterface
100100
throw new \RuntimeException('Codec does not support encoding JSON API content.');
101101
}
102102

103-
return $this->factory->createEncoder(
104-
$this->container,
105-
$this->encoding->getOptions()
106-
);
103+
$encoder = $this->factory->createEncoder($this->container);
104+
$options = $this->encoding->getOptions();
105+
106+
if ($options) {
107+
$encoder
108+
->withEncodeOptions($options->getOptions())
109+
->withEncodeDepth($options->getDepth())
110+
->withUrlPrefix($options->getUrlPrefix() ?? '');
111+
}
112+
113+
return $encoder;
107114
}
108115

109116
/**

src/Codec/EncodingList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace CloudCreativity\LaravelJsonApi\Codec;
1919

20-
use Neomerx\JsonApi\Contracts\Http\Headers\AcceptHeaderInterface;
20+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\AcceptHeaderInterface;
2121
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
2222
use Neomerx\JsonApi\Http\Headers\MediaType;
2323

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace CloudCreativity\LaravelJsonApi\Contracts\Http\Headers;
21+
22+
interface HeaderParametersInterface
23+
{
24+
/**
25+
* Get get 'Content-Type' header if request has body and `null` otherwise.
26+
*
27+
* @return HeaderInterface|null
28+
*/
29+
public function getContentTypeHeader(): ?HeaderInterface;
30+
31+
/**
32+
* Get 'Accept' header.
33+
*
34+
* @return AcceptHeaderInterface
35+
*/
36+
public function getAcceptHeader(): ?AcceptHeaderInterface;
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace CloudCreativity\LaravelJsonApi\Contracts\Http\Headers;
21+
22+
use Psr\Http\Message\ServerRequestInterface;
23+
24+
interface HeaderParametersParserInterface
25+
{
26+
/**
27+
* Parse input parameters from request.
28+
*
29+
* @param ServerRequestInterface $request
30+
* @param bool $checkContentType
31+
* @return HeaderParametersInterface
32+
*/
33+
public function parse(ServerRequestInterface $request, bool $checkContentType = true): HeaderParametersInterface;
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CloudCreativity\LaravelJsonApi\Http\Headers;
6+
7+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\AcceptHeaderInterface;
8+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderInterface;
9+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderParametersInterface;
10+
11+
class HeaderParameters implements HeaderParametersInterface
12+
{
13+
/**
14+
* @var AcceptHeaderInterface
15+
*/
16+
private AcceptHeaderInterface $accept;
17+
18+
/**
19+
* @var HeaderInterface|null
20+
*/
21+
private ?HeaderInterface $contentType;
22+
23+
/**
24+
* HeaderParameters constructor.
25+
*
26+
* @param AcceptHeaderInterface $accept
27+
* @param HeaderInterface|null $contentType
28+
*/
29+
public function __construct(AcceptHeaderInterface $accept, HeaderInterface $contentType = null)
30+
{
31+
$this->accept = $accept;
32+
$this->contentType = $contentType;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function getAcceptHeader(): AcceptHeaderInterface
39+
{
40+
return $this->accept;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function getContentTypeHeader(): ?HeaderInterface
47+
{
48+
return $this->contentType;
49+
}
50+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace CloudCreativity\LaravelJsonApi\Http\Headers;
21+
22+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderInterface;
23+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderParametersInterface;
24+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderParametersParserInterface;
25+
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface as NeomerxHeaderParametersParser;
26+
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
27+
use Psr\Http\Message\ServerRequestInterface;
28+
use Traversable;
29+
30+
class HeaderParametersParser implements HeaderParametersParserInterface
31+
{
32+
/**
33+
* @var NeomerxHeaderParametersParser
34+
*/
35+
private NeomerxHeaderParametersParser $parser;
36+
37+
/**
38+
* HeaderParametersParser constructor.
39+
*
40+
* @param NeomerxHeaderParametersParser $parser
41+
*/
42+
public function __construct(NeomerxHeaderParametersParser $parser)
43+
{
44+
$this->parser = $parser;
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function parse(ServerRequestInterface $request, bool $checkContentType = true): HeaderParametersInterface
51+
{
52+
$contentType = null;
53+
54+
if ($checkContentType === true) {
55+
$contentMediaType = $this->parser->parseContentTypeHeader(
56+
$this->getHeader($request, HeaderInterface::HEADER_CONTENT_TYPE)
57+
);
58+
$contentType = new Header(
59+
HeaderInterface::HEADER_CONTENT_TYPE,
60+
[$contentMediaType]
61+
);
62+
}
63+
64+
$acceptMediaTypes = $this->parser->parseAcceptHeader(
65+
$this->getHeader($request, HeaderInterface::HEADER_ACCEPT)
66+
);
67+
68+
if ($acceptMediaTypes instanceof Traversable) {
69+
$acceptMediaTypes = iterator_to_array($acceptMediaTypes);
70+
}
71+
72+
return new HeaderParameters(
73+
new AcceptHeader($acceptMediaTypes),
74+
$contentType,
75+
);
76+
}
77+
78+
/**
79+
* @param ServerRequestInterface $request
80+
* @param string $name
81+
* @return string
82+
*/
83+
private function getHeader(ServerRequestInterface $request, string $name): string
84+
{
85+
$value = $request->getHeader($name);
86+
if (empty($value) === false) {
87+
$value = $value[0];
88+
if (empty($value) === false) {
89+
return $value;
90+
}
91+
}
92+
93+
return MediaTypeInterface::JSON_API_MEDIA_TYPE;
94+
}
95+
}

src/Http/Middleware/BootJsonApi.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Http\Middleware;
1921

2022
use Closure;
2123
use CloudCreativity\LaravelJsonApi\Api\Api;
2224
use CloudCreativity\LaravelJsonApi\Api\Repository;
25+
use CloudCreativity\LaravelJsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
2326
use CloudCreativity\LaravelJsonApi\Exceptions\ResourceNotFoundException;
2427
use CloudCreativity\LaravelJsonApi\Routing\Route;
2528
use Illuminate\Contracts\Container\Container;
2629
use Illuminate\Http\Request;
2730
use Illuminate\Pagination\AbstractPaginator;
28-
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
2931

3032
/**
3133
* Class BootJsonApi
@@ -38,7 +40,7 @@ class BootJsonApi
3840
/**
3941
* @var Container
4042
*/
41-
private $container;
43+
private Container $container;
4244

4345
/**
4446
* @param Container $container

0 commit comments

Comments
 (0)