Skip to content

Commit bb415b0

Browse files
committed
wip
1 parent 9ee46b3 commit bb415b0

13 files changed

+69
-21
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"json-api-php/json-api": "^2.0",
77
"psr/http-message": "^1.0",
88
"psr/http-server-handler": "^1.0",
9-
"spatie/macroable": "^1.0",
10-
"zendframework/zend-diactoros": "^2.0"
9+
"zendframework/zend-diactoros": "^2.1"
1110
},
1211
"license": "MIT",
1312
"authors": [

src/Adapter/EloquentAdapter.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,28 @@ public function delete($model)
119119

120120
public function filterByAttribute($query, Attribute $field, $value)
121121
{
122-
$query->where($this->getAttributeProperty($field), $value);
122+
$property = $this->getAttributeProperty($field);
123+
124+
if (preg_match('/(.+)\.\.(.+)/', $value, $matches)) {
125+
if ($matches[1] !== '*') {
126+
$query->where($property, '>=', $matches[1]);
127+
}
128+
if ($matches[2] !== '*') {
129+
$query->where($property, '<=', $matches[2]);
130+
}
131+
132+
return;
133+
}
134+
135+
foreach (['>=', '>', '<=', '<'] as $operator) {
136+
if (strpos($value, $operator) === 0) {
137+
$query->where($property, $operator, substr($value, strlen($operator)));
138+
139+
return;
140+
}
141+
}
142+
143+
$query->where($property, $value);
123144
}
124145

125146
public function filterByHasOne($query, HasOne $field, array $ids)

src/Api.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ public function handleError($e)
123123
}
124124

125125
$errors = $e->getJsonApiErrors();
126+
$status = $e->getJsonApiStatus();
126127

127128
$data = new JsonApi\ErrorDocument(
128129
...$errors
129130
);
130131

131-
return new JsonApiResponse($data);
132+
return new JsonApiResponse($data, $status);
132133
}
133134

134135
public function getBaseUrl(): string

src/ErrorProviderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
interface ErrorProviderInterface
66
{
77
public function getJsonApiErrors(): array;
8+
9+
public function getJsonApiStatus(): string;
810
}

src/Exception/BadRequestException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ public function getJsonApiErrors(): array
3434
return [
3535
new Error(
3636
new Error\Title('Bad Request'),
37-
new Error\Status('400'),
37+
new Error\Status($this->getJsonApiStatus()),
3838
...$members
3939
)
4040
];
4141
}
42+
43+
public function getJsonApiStatus(): string
44+
{
45+
return '400';
46+
}
4247
}

src/Exception/ForbiddenException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
use JsonApiPhp\JsonApi\Error;
66
use Tobscure\JsonApiServer\ErrorProviderInterface;
77

8-
class BadRequestException extends \DomainException implements ErrorProviderInterface
8+
class ForbiddenException extends \DomainException implements ErrorProviderInterface
99
{
1010
public function getJsonApiErrors(): array
1111
{
1212
return [
1313
new Error(
1414
new Error\Title('Forbidden'),
15-
new Error\Status('403')
15+
new Error\Status($this->getJsonApiStatus())
1616
)
1717
];
1818
}
19+
20+
public function getJsonApiStatus(): string
21+
{
22+
return '403';
23+
}
1924
}

src/Exception/InternalServerErrorException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ public function getJsonApiErrors(): array
1616
)
1717
];
1818
}
19+
20+
public function getJsonApiStatus(): string
21+
{
22+
return '500';
23+
}
1924
}

src/Exception/MethodNotAllowedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use JsonApiPhp\JsonApi\Error;
66
use Tobscure\JsonApiServer\ErrorProviderInterface;
77

8-
class BadRequestException extends \DomainException implements ErrorProviderInterface
8+
class MethodNotAllowedException extends \DomainException implements ErrorProviderInterface
99
{
1010
public function getJsonApiErrors(): array
1111
{

src/Handler/Concerns/SavesData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ private function parseData($body): array
4848
$body = (array) $body;
4949

5050
if (! isset($body['data'])) {
51-
throw new BadRequestException;
51+
throw new BadRequestException('Root data attribute missing');
5252
}
5353

5454
if (isset($body['data']['attributes']) && ! is_array($body['data']['attributes'])) {
55-
throw new BadRequestException;
55+
throw new BadRequestException('data.attributes must be an object');
5656
}
5757

5858
if (isset($body['data']['relationships']) && ! is_array($body['data']['relationships'])) {
59-
throw new BadRequestException;
59+
throw new BadRequestException('data.relationships must be an object');
6060
}
6161

6262
return array_merge(

src/Handler/Index.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ private function buildUrl(Request $request, array $overrideParams = []): string
127127
unset($queryParams['page']['offset']);
128128
}
129129

130+
if (isset($queryParams['filter'])) {
131+
foreach ($queryParams['filter'] as $k => &$v) {
132+
if ($v === null) {
133+
$v = '';
134+
}
135+
}
136+
}
137+
130138
$queryString = http_build_query($queryParams);
131139

132140
return $selfUrl.($queryString ? '?'.$queryString : '');

0 commit comments

Comments
 (0)