Skip to content

Commit 4e33073

Browse files
committed
More spec validator updates
1 parent 6555627 commit 4e33073

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

src/Spec/Document.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
namespace LaravelJsonApi\Spec;
2121

2222
use LaravelJsonApi\Core\Document\ErrorList;
23-
use stdClass;
2423
use function json_decode;
2524

2625
class Document
2726
{
2827

2928
/**
30-
* @var stdClass
29+
* @var object
3130
*/
32-
private stdClass $document;
31+
private object $document;
3332

3433
/**
3534
* @var ErrorList
@@ -46,7 +45,7 @@ public static function cast($json): self
4645
return self::fromString($json);
4746
}
4847

49-
if ($json instanceof stdClass) {
48+
if (is_object($json)) {
5049
return new self($json);
5150
}
5251

@@ -67,9 +66,9 @@ public static function fromString(string $json): self
6766
/**
6867
* Document constructor.
6968
*
70-
* @param stdClass $document
69+
* @param object $document
7170
*/
72-
public function __construct(stdClass $document)
71+
public function __construct(object $document)
7372
{
7473
$this->document = $document;
7574
$this->errors = new ErrorList();
@@ -129,4 +128,12 @@ public function invalid(): bool
129128
return !$this->valid();
130129
}
131130

131+
/**
132+
* @return object
133+
*/
134+
public function toBase(): object
135+
{
136+
return $this->document;
137+
}
138+
132139
}

src/Spec/Validators/RelationshipValidator.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,53 @@ private function acceptIdentifier($path, $value, int $idx = null): array
146146
$errors = [];
147147
$dataPath = sprintf('%s/%s', rtrim($path, '/'), $member);
148148

149-
if (!property_exists($value, 'type')) {
150-
$errors[] = $this->translator->memberRequired($dataPath, 'type');
151-
} else if ($error = $this->acceptType($dataPath, $value->type)) {
152-
$errors[] = $error;
153-
}
149+
$errors[] = $this->acceptIdentifierType($dataPath, $value);
150+
$errors[] = $this->acceptIdentifierId($dataPath, $value);
154151

155-
return $errors;
152+
return array_filter($errors);
156153
}
157154

158155
/**
159-
* @param $path
160-
* @param $value
156+
* @param string $path
157+
* @param object $identifier
161158
* @return Error|null
162159
*/
163-
private function acceptType($path, $value): ?Error
160+
private function acceptIdentifierType(string $path, object $identifier): ?Error
164161
{
165-
if (!is_string($value)) {
162+
if (!property_exists($identifier, 'type')) {
163+
return $this->translator->memberRequired($path, 'type');
164+
}
165+
166+
if (!is_string($identifier->type)) {
166167
return $this->translator->memberNotString($path, 'type');
167168
}
168169

169-
if (empty($value)) {
170+
if (empty($identifier->type)) {
170171
return $this->translator->memberEmpty($path, 'type');
171172
}
172173

173174
return null;
174175
}
176+
177+
/**
178+
* @param string $path
179+
* @param object $identifier
180+
* @return Error|null
181+
*/
182+
private function acceptIdentifierId(string $path, object $identifier): ?Error
183+
{
184+
if (!property_exists($identifier, 'id')) {
185+
return $this->translator->memberRequired($path, 'id');
186+
}
187+
188+
if (!is_string($identifier->id)) {
189+
return $this->translator->memberNotString($path, 'id');
190+
}
191+
192+
if (empty($identifier->id) && '0' !== $identifier->id) {
193+
return $this->translator->memberEmpty($path, 'id');
194+
}
195+
196+
return null;
197+
}
175198
}

tests/lib/Integration/Spec/ResourceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public function createProvider(): array
321321
],
322322
[
323323
'title' => 'Non-Compliant JSON API Document',
324-
'detail' => "The member data must be an object.",
324+
'detail' => "The member type is required.",
325325
'status' => '400',
326326
'source' => ['pointer' => '/data/relationships/author/data'],
327327
],
@@ -346,7 +346,7 @@ public function createProvider(): array
346346
],
347347
[
348348
'title' => 'Non-Compliant JSON API Document',
349-
'detail' => "The member data must be an object.",
349+
'detail' => "The member id is required.",
350350
'status' => '400',
351351
'source' => ['pointer' => '/data/relationships/author/data'],
352352
],
@@ -457,7 +457,7 @@ public function createProvider(): array
457457
],
458458
[
459459
'title' => 'Non-Compliant JSON API Document',
460-
'detail' => "The member type is required.",
460+
'detail' => "The member id is required.",
461461
'status' => '400',
462462
'source' => ['pointer' => '/data/relationships/tags/data/0'],
463463
],

0 commit comments

Comments
 (0)