Skip to content

Commit fea1cdb

Browse files
authored
feat(type): Introduce Editable value object (#43)
1 parent 4744a2f commit fea1cdb

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

src/Bridge/Faker/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @method array datasourceEntryResponse(array $overrides = [])
2828
* @method array datasourceResponse(array $overrides = [])
2929
* @method array datasourcesResponse(array $overrides = [])
30+
* @method string editable(?string $uid = null, ?string $id = null, ?string $name = null, ?string $space = null)
3031
* @method array linkAlternateResponse(array $overrides = [])
3132
* @method array linkResponse(array $overrides = [])
3233
* @method array linksResponse(array $overrides = [])

src/Bridge/Faker/Provider/StoryblokProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Faker\Provider\Base as BaseProvider;
1818
use function Safe\array_replace_recursive;
19+
use function Safe\json_encode;
1920

2021
/**
2122
* @author Silas Joisten <[email protected]>
@@ -582,4 +583,20 @@ public function relation(): string
582583
$this->generator->word(),
583584
);
584585
}
586+
587+
public function editable(
588+
?string $uid = null,
589+
?string $id = null,
590+
?string $name = null,
591+
?string $space = null,
592+
): string {
593+
$payload = [
594+
'uid' => $uid ?? $this->generator->uuid(),
595+
'id' => $id ?? (string) $this->generator->numberBetween(1),
596+
'name' => $name ?? $this->generator->word(),
597+
'space' => $space ?? (string) $this->generator->numberBetween(1),
598+
];
599+
600+
return \sprintf('<!--#storyblok#%s-->', json_encode($payload));
601+
}
585602
}

src/Domain/Type/Editable.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\Id;
19+
use Storyblok\Api\Domain\Value\Uuid;
20+
use Webmozart\Assert\Assert;
21+
use function Safe\json_decode;
22+
use function Safe\json_encode;
23+
use function Symfony\Component\String\u;
24+
25+
/**
26+
* @experimental This class is experimental and may change in future versions.
27+
*
28+
* @author Silas Joisten <[email protected]>
29+
*/
30+
final readonly class Editable
31+
{
32+
public Uuid $uuid;
33+
public Id $id;
34+
public string $name;
35+
public string $space;
36+
37+
public function __construct(string $value)
38+
{
39+
TrimmedNonEmptyString::fromString($value);
40+
Assert::startsWith($value, '<!--#storyblok#');
41+
Assert::endsWith($value, '-->');
42+
43+
$values = json_decode(
44+
rtrim(ltrim($value, '<!--#storyblok#'), '-->'),
45+
true,
46+
);
47+
48+
Assert::keyExists($values, 'uid');
49+
Assert::uuid($values['uid']);
50+
$this->uuid = new Uuid($values['uid']);
51+
52+
Assert::keyExists($values, 'id');
53+
$this->id = new Id((int) $values['id']);
54+
55+
Assert::keyExists($values, 'name');
56+
$this->name = TrimmedNonEmptyString::fromString($values['name'])->toString();
57+
58+
Assert::keyExists($values, 'space');
59+
$this->space = $values['space'];
60+
}
61+
62+
public function __toString(): string
63+
{
64+
return \sprintf('<!--#storyblok#%s-->', json_encode($this->toArray()));
65+
}
66+
67+
/**
68+
* @return array{
69+
* uid: string,
70+
* id: string,
71+
* name: string,
72+
* space: string,
73+
* }
74+
*/
75+
public function toArray(): array
76+
{
77+
return [
78+
'uid' => $this->uuid->value,
79+
'id' => (string) $this->id->value,
80+
'name' => $this->name,
81+
'space' => $this->space,
82+
];
83+
}
84+
}
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\Tests\Unit\Domain\Type;
16+
17+
use PHPUnit\Framework\Attributes\Test;
18+
use PHPUnit\Framework\TestCase;
19+
use Storyblok\Api\Domain\Type\Editable;
20+
use Storyblok\Api\Tests\Util\FakerTrait;
21+
22+
/**
23+
* @author Silas Joisten <[email protected]>
24+
*/
25+
final class EditableTest extends TestCase
26+
{
27+
use FakerTrait;
28+
29+
#[Test]
30+
public function uuid(): void
31+
{
32+
$faker = self::faker();
33+
$values = $faker->editable(uid: $uuid = $faker->uuid());
34+
35+
self::assertSame($uuid, (new Editable($values))->uuid->value);
36+
}
37+
38+
#[Test]
39+
public function id(): void
40+
{
41+
$faker = self::faker();
42+
$values = $faker->editable(id: $id = (string) $faker->numberBetween(1, 1000));
43+
44+
self::assertSame((int) $id, (new Editable($values))->id->value);
45+
}
46+
47+
#[Test]
48+
public function validName(): void
49+
{
50+
$faker = self::faker();
51+
$values = $faker->editable(name: $name = $faker->word());
52+
53+
self::assertSame($name, (new Editable($values))->name);
54+
}
55+
56+
#[Test]
57+
public function space(): void
58+
{
59+
$faker = self::faker();
60+
$values = $faker->editable(space: $space = (string) $faker->randomNumber());
61+
62+
self::assertSame($space, (new Editable($values))->space);
63+
}
64+
65+
#[Test]
66+
public function stringable(): void
67+
{
68+
$values = self::faker()->editable();
69+
70+
self::assertSame($values, (new Editable($values))->__toString());
71+
self::assertSame($values, (string) new Editable($values));
72+
}
73+
}

0 commit comments

Comments
 (0)