Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.0.x' into 1.11.x-merge-up-into…
Browse files Browse the repository at this point in the history
…-2.0.x_lo5L9oGr

# Conflicts:
#	test/Unit/Document/Fragment/FactoryTest.php
  • Loading branch information
gsteel committed Oct 31, 2024
2 parents bc52738 + 1adb8b5 commit a14bc36
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/DefaultLinkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function resolve(Link $link): string|null
}

if ($link instanceof DocumentLink) {
return $this->resolveDocumentLink($link);
return $link->url() ?? $this->resolveDocumentLink($link);
}

return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function asLink(): DocumentLink;
* Return the value object containing all of the document content fragments
*/
public function data(): DocumentData;

/**
* If the api has been configured with a route for this type of document, the url might be a string
*/
public function url(): string|null;
}
5 changes: 5 additions & 0 deletions src/Document/DocumentDataConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public function data(): DocumentData
{
return $this->data;
}

public function url(): string|null
{
return $this->data->url();
}
}
10 changes: 9 additions & 1 deletion src/Document/Fragment/DocumentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private function __construct(
private string $lang,
private bool $isBroken,
iterable $tags,
private string|null $url,
) {
$this->tags = [];
foreach ($tags as $tag) {
Expand All @@ -41,8 +42,9 @@ public static function new(
string $lang,
bool $isBroken = false,
iterable $tags = [],
string|null $url = null,
): self {
return new self($id, $uid, $type, $lang, $isBroken, $tags);
return new self($id, $uid, $type, $lang, $isBroken, $tags, $url);
}

public static function withDocument(Document $document): self
Expand All @@ -54,6 +56,7 @@ public static function withDocument(Document $document): self
$document->lang(),
false,
$document->tags(),
$document->url(),
);
}

Expand Down Expand Up @@ -97,4 +100,9 @@ public function isEmpty(): bool
{
return false;
}

public function url(): string|null
{
return $this->url;
}
}
1 change: 1 addition & 0 deletions src/Document/Fragment/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private static function linkFactory(object $data): Link
$lang,
$isBroken,
self::assertObjectPropertyAllString($data, 'tags'),
self::optionalStringProperty($data, 'url'),
);
}

Expand Down
25 changes: 19 additions & 6 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Prismic\Value\FormSpec;
use Prismic\Value\Ref;
use Prismic\Value\RouteResolverSpec;

use function array_filter;
use function array_map;
Expand Down Expand Up @@ -45,21 +46,21 @@ public function toUrl(): string
/** @psalm-return QueryParams */
private function mergeWithDefaults(): array
{
$parameters = $this->defaultParameters();
$defaults = $this->defaultParameters();
foreach ($this->parameters as $name => $value) {
if (is_scalar($value)) {
$parameters[$name] = $value;
$defaults[$name] = $value;
continue;
}

$merged = isset($parameters[$name]) && is_array($parameters[$name])
? array_merge($parameters[$name], (array) $value)
$merged = isset($defaults[$name]) && is_array($defaults[$name])
? array_merge($defaults[$name], (array) $value)
: $value;

$parameters[$name] = $merged;
$defaults[$name] = $merged;
}

return $parameters;
return $defaults;
}

private function buildQuery(): string
Expand Down Expand Up @@ -236,6 +237,18 @@ public function ref(Ref $ref): self
return $this->set('ref', (string) $ref);
}

/**
* Override or set Route Resolvers used to return document and link urls in received payloads
*
* If the api was initialised with a `routes` parameter, the query will be pre-populated with that value.
*
* @param list<RouteResolverSpec> $routes
*/
public function routes(array $routes): self
{
return $this->set('routes', Json::encode($routes));
}

/**
* Order results
*
Expand Down
7 changes: 7 additions & 0 deletions src/Value/DocumentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private function __construct(
private string $lang,
private DateTimeImmutable $firstPublished,
private DateTimeImmutable $lastPublished,
private string|null $url,
iterable $tags,
iterable $translations,
private FragmentCollection $body,
Expand Down Expand Up @@ -75,6 +76,7 @@ public static function factory(object $data): self
self::assertObjectPropertyIsString($data, 'lang'),
self::assertObjectPropertyIsUtcDateTime($data, 'first_publication_date'),
self::assertObjectPropertyIsUtcDateTime($data, 'last_publication_date'),
self::optionalStringProperty($data, 'url'),
self::assertObjectPropertyAllString($data, 'tags'),
$translations,
$body,
Expand Down Expand Up @@ -147,4 +149,9 @@ public function data(): DocumentData
{
return $this;
}

public function url(): string|null
{
return $this->url;
}
}
8 changes: 7 additions & 1 deletion src/Value/FormSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ private function __construct(
FormField ...$fields,
) {
$this->fields = $fields;

if ($this->hasField('routes')) {
return;
}

$this->fields[] = FormField::new('routes', FormField::TYPE_STRING, false, null);
}

public static function factory(string $id, object $object): self
Expand Down Expand Up @@ -107,7 +113,7 @@ public function field(string $name): FormField
}

/**
* @return FormField[]
* @return Traversable<array-key, FormField>
* @psalm-return ArrayIterator<array-key, FormField>
*/
public function getIterator(): Traversable
Expand Down
47 changes: 47 additions & 0 deletions src/Value/RouteResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Prismic\Value;

use JsonSerializable;
use Prismic\Json;
use Stringable;

use const JSON_FORCE_OBJECT;

final class RouteResolverSpec implements JsonSerializable, Stringable
{
/** @param array<string, string> $resolvers */
public function __construct(
private string $type,
private string $path,
private array $resolvers,
) {
}

public function __toString(): string
{
return Json::encode($this, JSON_FORCE_OBJECT);
}

/** @return array{type: string, path: string, resolvers: array<string, string>} */
public function jsonSerialize(): array
{
return [
'type' => $this->type,
'path' => $this->path,
'resolvers' => $this->resolvers,
];
}

/** @param array{type: string, path: string, resolvers: array<string, string>} $data */
public static function __set_state(array $data): self
{
return new self(
$data['type'],
$data['path'],
$data['resolvers'],
);
}
}
56 changes: 56 additions & 0 deletions test/Unit/DefaultLinkResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace PrismicTest;

use PHPUnit\Framework\TestCase;
use Prismic\DefaultLinkResolver;
use Prismic\Document\Fragment\DocumentLink;
use Prismic\Document\Fragment\MediaLink;
use Prismic\Link;

class DefaultLinkResolverTest extends TestCase
{
private DefaultLinkResolver $linkResolver;

protected function setUp(): void
{
parent::setUp();

$this->linkResolver = new class extends DefaultLinkResolver {
protected function resolveDocumentLink(DocumentLink $link): string|null
{
return '/some/url';
}
};
}

public function testMediaUrlsReturnTheExpectedUrl(): void
{
$media = MediaLink::new('/foo', 'whatever.jpg', 1234);

self::assertSame('/foo', $this->linkResolver->resolve($media));
}

public function testDocLinksWillYieldStoredUrlWhenPresent(): void
{
$docLink = DocumentLink::new('id', 'uid', 'foo', 'en-gb', false, [], '/special');

self::assertSame('/special', $this->linkResolver->resolve($docLink));
}

public function testDocLinksWillUseCustomResolveWhenNull(): void
{
$docLink = DocumentLink::new('id', 'uid', 'foo', 'en-gb', false, [], null);

self::assertSame('/some/url', $this->linkResolver->resolve($docLink));
}

public function testUnknownLinkTypesWillYieldNull(): void
{
$link = $this->createMock(Link::class);

self::assertNull($this->linkResolver->resolve($link));
}
}
1 change: 1 addition & 0 deletions test/Unit/Document/DocumentDataConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testProxyMethods(): void
$this->assertSame($this->document->type(), $this->subject->type());
$this->assertSame($this->document->tags(), $this->subject->tags());
$this->assertSame($this->document->lang(), $this->subject->lang());
$this->assertSame($this->document->url(), $this->subject->url());
$this->assertSame($this->document->firstPublished(), $this->subject->firstPublished());
$this->assertSame($this->document->lastPublished(), $this->subject->lastPublished());
$this->assertSame($this->document->translations(), $this->subject->translations());
Expand Down
36 changes: 35 additions & 1 deletion test/Unit/Document/Fragment/DocumentLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,29 @@ public function testConstructor(): DocumentLink
false,
['a', 'b'],
);
$this->expectNotToPerformAssertions();
self::assertTrue(true);

return $link;
}

/** @depends testConstructor */
public function testThatCastingALinkToAStringWillYieldItsId(DocumentLink $link): void
{
self::assertSame('id', (string) $link);
}

public function testConstructorWithUrl(): DocumentLink
{
$link = DocumentLink::new(
'id',
'uid',
'type',
'en-gb',
false,
['a', 'b'],
'/some/url',
);
self::assertTrue(true);

return $link;
}
Expand Down Expand Up @@ -81,4 +103,16 @@ public function testThatTagsHaveExpectedMembers(DocumentLink $link): void
$this->assertContainsEquals('a', $link->tags());
$this->assertContainsEquals('b', $link->tags());
}

/** @depends testConstructor */
public function testTheLinkMayHaveAUrlAndItIsNullByDefault(DocumentLink $link): void
{
self::assertNull($link->url());
}

/** @depends testConstructorWithUrl */
public function testThatUrlReturnsTheExpectedValue(DocumentLink $link): void
{
self::assertSame('/some/url', $link->url());
}
}
Loading

0 comments on commit a14bc36

Please sign in to comment.