Skip to content

Commit

Permalink
Merge pull request #293 from koriym/link-self
Browse files Browse the repository at this point in the history
Self link
  • Loading branch information
koriym authored Apr 22, 2024
2 parents b333494 + dd77b62 commit 0a3cd1d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<exclude name="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable"/>
<exclude name="SlevomatCodingStandard.Commenting.UselessInheritDocComment.UselessInheritDocComment"/>
<exclude name="Squiz.Commenting.FunctionComment.InvalidNoReturn"/>
<exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
<!-- /Base -->
<!-- Option -->
<!-- <exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed"/>-->
Expand Down
23 changes: 23 additions & 0 deletions src/EmbedInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
use function array_shift;
use function assert;
use function is_array;
use function is_string;
use function uri_template;

final class EmbedInterceptor implements MethodInterceptor
{
private const SELF_LINK = '_self';

private readonly ResourceInterface $resource;

public function __construct(
Expand Down Expand Up @@ -71,6 +74,12 @@ private function embedResource(array $embeds, ResourceObject $ro, array $query):
throw new LinkException($embed->rel); // @codeCoverageIgnore
}

if ($embed->rel === self::SELF_LINK) {
$this->linkSelf($request, $ro);

continue;
}

$ro->body[$embed->rel] = clone $request;
} catch (BadRequestException $e) {
// wrap ResourceNotFound or Uri exception
Expand Down Expand Up @@ -101,4 +110,18 @@ private function getArgsByInvocation(MethodInvocation $invocation): array

return $namedParameters;
}

public function linkSelf(Request $request, ResourceObject $ro): void
{
$result = $request();
assert(is_array($result->body));
/** @var mixed $value */
foreach ($result->body as $key => $value) {
assert(is_string($key));
/** @psalm-suppress MixedArrayAssignment */
$ro->body[$key] = $value; // @phpstan-ignore-line
}

$ro->code = $result->code;
}
}
8 changes: 8 additions & 0 deletions tests/EmbedInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public function testInvoke(): Birds
return $result;
}

public function testSelfLink(): void
{
$embeded = $this->resource->uri('app://self/bird/child')(['id' => 1]);
$result = $this->resource->uri('app://self/bird/self-link')(['id' => 1]);
$this->assertSame($result->body, $embeded->body);
$this->assertSame($result->code, $embeded->code);
}

public function testInvokeRelativePath(): BirdsRel
{
$result = $this->resource->uri('app://self/bird/birds-rel')(['id' => 1]);
Expand Down
21 changes: 21 additions & 0 deletions tests/Fake/FakeVendor/Sandbox/Resource/App/Bird/Child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace FakeVendor\Sandbox\Resource\App\Bird;

use BEAR\Resource\ResourceObject;

class Child extends ResourceObject
{
public $code = 100;

public function onGet(string $id)
{
$this->body = [
'id' => $id
];

return $this;
}
}
18 changes: 18 additions & 0 deletions tests/Fake/FakeVendor/Sandbox/Resource/App/Bird/SelfLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace FakeVendor\Sandbox\Resource\App\Bird;

use BEAR\Resource\Annotation\Embed;
use BEAR\Resource\Annotation\Link;
use BEAR\Resource\ResourceObject;

class SelfLink extends ResourceObject
{
#[Embed(rel: "_self", src: "app://self/bird/child{?id}")]
public function onGet(string $id)
{
return $this;
}
}

0 comments on commit 0a3cd1d

Please # to comment.