Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
48.39% |
15 / 31 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| FormName | |
48.39% |
15 / 31 |
|
63.64% |
7 / 11 |
51.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| hasChildFormFieldName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getChildFormFieldName | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| createChildForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toValidationErrorKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPrototypeName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createTranslationString | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| createSchema | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\SchemaMethod; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Exceptions\InvalidTypeException; |
| 8 | use Apie\Core\Lists\StringList; |
| 9 | use Apie\Core\Translator\ValueObjects\FormFieldProperty; |
| 10 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 11 | use Apie\HtmlBuilders\Exceptions\EmptyFormNameException; |
| 12 | use ReflectionClass; |
| 13 | use Stringable; |
| 14 | |
| 15 | #[Description('Represents a form name field, for example form[a][b]')] |
| 16 | #[SchemaMethod('createSchema')] |
| 17 | final class FormName implements ValueObjectInterface, Stringable |
| 18 | { |
| 19 | /** @var array<int, string> */ |
| 20 | private array $internal = []; |
| 21 | |
| 22 | public function __construct(string... $parts) |
| 23 | { |
| 24 | $this->internal = $parts; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return static |
| 29 | */ |
| 30 | public static function fromNative(mixed $input): self |
| 31 | { |
| 32 | if (is_string($input) && str_starts_with($input, 'form[')) { |
| 33 | // remove 'form[' |
| 34 | $input = preg_replace('/^form\[/', '', $input); |
| 35 | // remove trailing ']' |
| 36 | $input = preg_replace('/\]$/', '', $input); |
| 37 | $input = str_replace('][', ',', $input); |
| 38 | return new static(...explode(',', $input)); |
| 39 | } |
| 40 | if (is_iterable($input)) { |
| 41 | return new static(...$input); |
| 42 | } |
| 43 | throw new InvalidTypeException($input, 'string|array'); |
| 44 | } |
| 45 | |
| 46 | public function hasChildFormFieldName(): bool |
| 47 | { |
| 48 | return !empty($this->internal); |
| 49 | } |
| 50 | |
| 51 | public function getChildFormFieldName(): string |
| 52 | { |
| 53 | if (empty($this->internal)) { |
| 54 | throw new EmptyFormNameException(); |
| 55 | } |
| 56 | return end($this->internal); |
| 57 | } |
| 58 | |
| 59 | public function createChildForm(string $formFieldName): self |
| 60 | { |
| 61 | return new self(...[...$this->internal, $formFieldName]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return array<string|int, string> |
| 66 | */ |
| 67 | public function toNative(): array |
| 68 | { |
| 69 | return $this->internal; |
| 70 | } |
| 71 | |
| 72 | public function toValidationErrorKey(): string |
| 73 | { |
| 74 | return implode('.', $this->internal); |
| 75 | } |
| 76 | |
| 77 | public function getPrototypeName(): string |
| 78 | { |
| 79 | return '__' . end($this->internal); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param ReflectionClass<covariant object> $class |
| 84 | */ |
| 85 | public function createTranslationString(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): FormFieldProperty |
| 86 | { |
| 87 | return FormFieldProperty::createForProperty( |
| 88 | new StringList($this->internal), |
| 89 | $class, |
| 90 | $boundedContextId |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | public function __toString(): string |
| 95 | { |
| 96 | if (empty($this->internal)) { |
| 97 | return 'form'; |
| 98 | } |
| 99 | return 'form[' . implode('][', $this->internal) . ']'; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return array<string, mixed> |
| 104 | */ |
| 105 | public static function createSchema(): array |
| 106 | { |
| 107 | return [ |
| 108 | 'type' => 'array', |
| 109 | 'items' => [ |
| 110 | 'type' => 'string', |
| 111 | ] |
| 112 | ]; |
| 113 | } |
| 114 | } |