Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Fieldsets | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
7.02 | |
0.00% |
0 / 1 |
| serialize | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
7.02 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | |
| 9 | /** Serializes fieldset descriptors shared by node and entry schemas. */ |
| 10 | final class Fieldsets |
| 11 | { |
| 12 | /** |
| 13 | * @param list<FieldsetDefinition> $fieldsets |
| 14 | * @param list<string> $ordered |
| 15 | * @param array<string, Field> $fields |
| 16 | * @param class-string $owner |
| 17 | * |
| 18 | * @return list<array{name: string, label: ?string, description: ?string, width: int, fields: list<string>}> |
| 19 | */ |
| 20 | public static function serialize( |
| 21 | array $fieldsets, |
| 22 | array $ordered, |
| 23 | array $fields, |
| 24 | string $owner, |
| 25 | ): array { |
| 26 | $result = []; |
| 27 | |
| 28 | foreach ($fieldsets as $fieldset) { |
| 29 | $members = array_values(array_filter( |
| 30 | $ordered, |
| 31 | static fn(string $name): bool => in_array($name, $fieldset->fields, true), |
| 32 | )); |
| 33 | |
| 34 | if ($members === []) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | $first = (int) array_search($members[0], $ordered, true); |
| 39 | |
| 40 | if (array_slice($ordered, $first, count($members)) !== $members) { |
| 41 | throw new RuntimeException( |
| 42 | "Field order for '{$owner}' splits fieldset '{$fieldset->name}'.", |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | $visible = array_values(array_filter( |
| 47 | $members, |
| 48 | static fn(string $name): bool => !($fields[$name]->properties()['hidden'] ?? false), |
| 49 | )); |
| 50 | |
| 51 | if ($visible === []) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | $result[] = [ |
| 56 | 'name' => $fieldset->name, |
| 57 | 'label' => $fieldset->label === null ? null : __($fieldset->label), |
| 58 | 'description' => $fieldset->description === null ? null : __($fieldset->description), |
| 59 | 'width' => $fieldset->width, |
| 60 | 'fields' => $visible, |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | return $result; |
| 65 | } |
| 66 | } |