Skip to content

Commit e0f1c5e

Browse files
feat(dx): Introduce ValueObjectTrait with helper methods (#50)
Co-authored-by: edodusi <[email protected]>
1 parent 48b1415 commit e0f1c5e

File tree

4 files changed

+1629
-0
lines changed

4 files changed

+1629
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,37 @@ new BlogPostCollection(
649649
);
650650
```
651651

652+
### Helpers
653+
654+
The `Storyblok\Api\Util\ValueObjectTrait` provides utility methods for mapping raw Storyblok data arrays into strong PHP
655+
value objects, enums, and domain models. These helpers reduce boilerplate code and improve readability in DTO
656+
constructors or factory methods.
657+
658+
Use this trait in your value objects or models to simplify the parsing and validation of Storyblok field values.
659+
660+
#### Available Methods
661+
662+
| Method | Description |
663+
|-----------------------|------------------------------------------------------------------------------------------------------------------|
664+
| `one()` | Expects exactly one item (e.g. from a `blocks` field). Instantiates one object from it. |
665+
| `list()` | Maps a list of items to objects. Allows setting `$min`, `$max`, or exact `$count` constraints. |
666+
| `nullOrOne()` | Same as `one()`, but allows the field to be optional (returns `null` if empty). |
667+
| `enum()` | Maps a string value to a backed enum. Supports default value and whitelisting of allowed values. |
668+
| `DateTimeImmutable()` | Returns a `Safe\DateTimeImmutable` object from a given date string. |
669+
| `Uuid()` | Returns a `Storyblok\Api\Domain\Value\Uuid` instance from a string. |
670+
| `Asset()` | Maps an asset array to a `Storyblok\Api\Domain\Type\Asset` object. |
671+
| `nullOrAsset()` | Same as `Asset()`, but allows null or invalid input. |
672+
| `MultiLink()` | Maps a multilink array to a `Storyblok\Api\Domain\Type\MultiLink` object. |
673+
| `nullOrMultiLink()` | Same as `MultiLink()`, but returns `null` if `url` and `id` are missing or empty. |
674+
| `RichText()` | Maps rich text content to a `Storyblok\Api\Domain\Type\RichText` object. |
675+
| `nullOrRichText()` | Same as `RichText()`, but returns `null` if content is empty or only contains whitespace. |
676+
| `boolean()` | Returns `true` if the key exists and its value is `true`, otherwise `false`. |
677+
| `zeroOrInteger()` | Returns an integer from the field, or `0` if missing. |
678+
| `zeroOrFloat()` | Returns a float from the field, or `0.0` if missing. |
679+
| `string()` | Returns a trimmed non-empty string (using `TrimmedNonEmptyString`). Optional max length check. |
680+
| `nullOrString()` | Same as `string()`, but returns `null` if missing or invalid. |
681+
| `nullOrEditable()` | Returns an `Editable` instance or `null`. |
682+
652683
[actions]: https://github.com/sensiolabs-de/storyblok-api/actions
653684
[codecov]: https://codecov.io/gh/sensiolabs-de/storyblok-api
654685

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,9 @@ parameters:
185185
identifier: method.nonObject
186186
count: 1
187187
path: tests/Unit/Domain/Value/AssetTest.php
188+
189+
-
190+
message: '#^Parameter \#3 \$class of static method class@anonymous/tests/Unit/Util/ValueObjectTraitTest\.php\:331\:\:enum\(\) expects class\-string\<BackedEnum\>, string given\.$#'
191+
identifier: argument.type
192+
count: 1
193+
path: tests/Unit/Util/ValueObjectTraitTest.php

0 commit comments

Comments
 (0)