Skip to content

Commit

Permalink
Merge pull request #278 from netglue/null-ref-labels
Browse files Browse the repository at this point in the history
Refs can possibly have null labels
  • Loading branch information
gsteel committed May 18, 2023
2 parents 350c51c + 70c9708 commit 2b9fabd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ private function previewRef(): Ref|null
}

$cookiePayload = (string) $this->requestCookies[$cookieName];
assert($cookiePayload !== '');
// Fuck this. If you have the toolbar installed on your website. Prismic set the preview cookie for
// *every single request*. This means that if you rely on determining whether a preview is active or not
// by inspecting cookies in order to disable caching for example, this fucks things. It does not matter
Expand Down
31 changes: 24 additions & 7 deletions src/Value/Ref.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@
use function is_bool;
use function is_string;

/** @psalm-immutable */
final class Ref implements Stringable
{
/**
* @param non-empty-string $id
* @param non-empty-string $ref
* @param non-empty-string|null $label
*/
private function __construct(
private string $id,
private string $ref,
private string $label,
private bool $isMasterRef,
public readonly string $id,
public readonly string $ref,
public readonly string|null $label,
public readonly bool $isMasterRef,
) {
}

public static function new(string $id, string $ref, string $label, bool $isMasterRef): self
/**
* @param non-empty-string $id
* @param non-empty-string $ref
* @param non-empty-string|null $label
*/
public static function new(string $id, string $ref, string|null $label, bool $isMasterRef): self
{
return new self($id, $ref, $label, $isMasterRef);
}
Expand All @@ -32,24 +43,30 @@ public static function factory(object $object): self
$label = $object->label ?? null;
$isMaster = $object->isMasterRef ?? false;
assert(is_string($id));
assert($id !== '');
assert(is_string($ref));
assert(is_string($label));
assert($ref !== '');
assert(is_string($label) || $label === null);
$label = $label === '' ? null : $label;
assert(is_bool($isMaster));

return self::new($id, $ref, $label, $isMaster);
}

/** @return non-empty-string */
public function id(): string
{
return $this->id;
}

/** @return non-empty-string */
public function ref(): string
{
return $this->ref;
}

public function label(): string
/** @return non-empty-string|null */
public function label(): string|null
{
return $this->label;
}
Expand Down
52 changes: 52 additions & 0 deletions test/Unit/Value/RefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,56 @@ public function testRefCanBeCastToAString(): void
$ref = Ref::new('foo', 'bar', 'baz', true);
$this->assertSame('bar', (string) $ref);
}

public function testThatTheRefCanBeNull(): void
{
$ref = Ref::new('foo', 'bar', null, false);
$this->assertSame('bar', (string) $ref);
}

public function testFactory(): void
{
$input = (object) [
'id' => 'foo',
'ref' => 'ref',
'label' => 'Master',
'isMasterRef' => false,
];

$ref = Ref::factory($input);

self::assertSame('foo', $ref->id());
self::assertSame('ref', $ref->ref());
self::assertSame('Master', $ref->label());
self::assertFalse($ref->isMaster());
}

public function testFactoryWithNullLabel(): void
{
$input = (object) [
'id' => 'foo',
'ref' => 'ref',
'label' => null,
'isMasterRef' => false,
];

$ref = Ref::factory($input);

self::assertSame('foo', $ref->id());
self::assertSame('ref', $ref->ref());
self::assertNull($ref->label());
self::assertFalse($ref->isMaster());
}

public function testFactoryWithMissingIsMasterRefAndLabel(): void
{
$input = (object) [
'id' => 'foo',
'ref' => 'ref',
];

$ref = Ref::factory($input);

self::assertFalse($ref->isMaster());
}
}

0 comments on commit 2b9fabd

Please sign in to comment.