Skip to content

Commit 21972d0

Browse files
authored
feat(type): Introduce MultiLink value object (#45)
1 parent fea1cdb commit 21972d0

File tree

7 files changed

+418
-0
lines changed

7 files changed

+418
-0
lines changed

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ parameters:
5454
count: 1
5555
path: src/Bridge/Faker/Provider/StoryblokProvider.php
5656

57+
-
58+
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:multiLinkResponse\(\) should return array\{id\: string, url\: string, email\: string, linktype\: string, fieldtype\: string\} but returns array\.$#'
59+
identifier: return.type
60+
count: 1
61+
path: src/Bridge/Faker/Provider/StoryblokProvider.php
62+
5763
-
5864
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:richTextResponse\(\) should return array\{type\: string, content\: list\<array\<mixed\>\>\} but returns array\.$#'
5965
identifier: return.type

src/Bridge/Faker/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @method array linkAlternateResponse(array $overrides = [])
3232
* @method array linkResponse(array $overrides = [])
3333
* @method array linksResponse(array $overrides = [])
34+
* @method array multiLinkResponse(array $overrides = [])
3435
* @method string relation()
3536
* @method array richTextEmptyResponse()
3637
* @method array richTextParagraphResponse(?string $text = null)

src/Bridge/Faker/Provider/StoryblokProvider.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Storyblok\Api\Bridge\Faker\Provider;
1616

1717
use Faker\Provider\Base as BaseProvider;
18+
use Storyblok\Api\Domain\Type\MultiLinkType;
1819
use function Safe\array_replace_recursive;
1920
use function Safe\json_encode;
2021

@@ -537,6 +538,39 @@ public function richTextResponse(array $overrides = []): array
537538
);
538539
}
539540

541+
/**
542+
* @param array{
543+
* id?: string,
544+
* url?: string,
545+
* email?: string,
546+
* linktype?: string,
547+
* fieldtype?: string
548+
* } $overrides
549+
*
550+
* @return array{
551+
* id: string,
552+
* url: string,
553+
* email: string,
554+
* linktype: string,
555+
* fieldtype: string
556+
* }
557+
*/
558+
public function multiLinkResponse(array $overrides = []): array
559+
{
560+
$response = [
561+
'id' => $this->generator->uuid(),
562+
'url' => $this->generator->url(),
563+
'email' => $this->generator->email(),
564+
'linktype' => $this->generator->randomElement(MultiLinkType::cases())->value,
565+
'fieldtype' => 'multilink',
566+
];
567+
568+
return array_replace_recursive(
569+
$response,
570+
$overrides,
571+
);
572+
}
573+
540574
/**
541575
* @return array{
542576
* type: 'doc',

src/Domain/Type/MultiLink.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of storyblok/php-content-api-client.
7+
*
8+
* (c) Storyblok GmbH <[email protected]>
9+
* in cooperation with SensioLabs Deutschland <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Storyblok\Api\Domain\Type;
16+
17+
use OskarStark\Value\TrimmedNonEmptyString;
18+
use Storyblok\Api\Domain\Value\Uuid;
19+
use Webmozart\Assert\Assert;
20+
21+
/**
22+
* @experimental This class is experimental and may change in future versions.
23+
*
24+
* @author Silas Joisten <[email protected]>
25+
*/
26+
final readonly class MultiLink
27+
{
28+
public MultiLinkType $type;
29+
public ?Uuid $id;
30+
public ?string $url;
31+
32+
/**
33+
* @param array<string, mixed> $values
34+
*/
35+
public function __construct(array $values)
36+
{
37+
Assert::keyExists($values, 'fieldtype');
38+
Assert::same($values['fieldtype'], 'multilink');
39+
40+
Assert::keyExists($values, 'linktype');
41+
TrimmedNonEmptyString::fromString($values['linktype']);
42+
$this->type = MultiLinkType::from($values['linktype']);
43+
44+
$url = null;
45+
$id = null;
46+
47+
if ($this->type->equals(MultiLinkType::Asset)) {
48+
Assert::keyExists($values, 'url');
49+
$url = TrimmedNonEmptyString::fromString($values['url'])->toString();
50+
Assert::startsWith($url, 'http');
51+
}
52+
53+
if ($this->type->equals(MultiLinkType::Email)) {
54+
Assert::keyExists($values, 'email');
55+
$email = TrimmedNonEmptyString::fromString($values['email'])->toString();
56+
Assert::email($email);
57+
$url = \sprintf('mailto:%s', $email);
58+
}
59+
60+
if ($this->type->equals(MultiLinkType::Story)) {
61+
Assert::keyExists($values, 'id');
62+
$id = new Uuid($values['id']);
63+
}
64+
65+
if ($this->type->equals(MultiLinkType::Url)) {
66+
Assert::keyExists($values, 'url');
67+
$url = TrimmedNonEmptyString::fromString($values['url'])->toString();
68+
}
69+
70+
$this->id = $id;
71+
$this->url = $url;
72+
}
73+
}

src/Domain/Type/MultiLinkType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of storyblok/php-content-api-client.
7+
*
8+
* (c) Storyblok GmbH <[email protected]>
9+
* in cooperation with SensioLabs Deutschland <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Storyblok\Api\Domain\Type;
16+
17+
use OskarStark\Enum\Trait\Comparable;
18+
19+
/**
20+
* @experimental This class is experimental and may change in future versions.
21+
*
22+
* @author Silas Joisten <[email protected]>
23+
*/
24+
enum MultiLinkType: string
25+
{
26+
use Comparable;
27+
28+
case Asset = 'asset';
29+
case Email = 'email';
30+
case Story = 'story';
31+
case Url = 'url';
32+
}

0 commit comments

Comments
 (0)