Lines
0.00%
0 / 12
Functions and Methods
0.00%
0 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| FieldModel | 0.00% | 0 / 12 | 0.00% | 0 / 4 | 182 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| type | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 6 | |||||
| summary | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | |||||
| boundsNote | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 72 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Codegen\Service; | |
| 6 | ||
| 7 | use Stewart\Codegen\Php\PhpType; | |
| 8 | ||
| 9 | final readonly class FieldModel | |
| 10 | { | |
| 11 | public function __construct( | |
| 12 | public string $parameter, | |
| 13 | public ServiceField $field, | |
| 14 | ) {} | |
| 15 | ||
| 16 | public function type(): PhpType | |
| 17 | { | |
| 18 | return $this->field->required ? $this->field->type : $this->field->type->nullable(); | |
| 19 | } | |
| 20 | ||
| 21 | public function summary(): string | |
| 22 | { | |
| 23 | $parts = array_filter([$this->field->label, $this->field->description, $this->boundsNote()], static fn(?string $part): bool => $part !== null && $part !== ''); | |
| 24 | ||
| 25 | return implode('. ', array_map(static fn(string $part): string => rtrim($part, '.'), $parts)); | |
| 26 | } | |
| 27 | ||
| 28 | private function boundsNote(): ?string | |
| 29 | { | |
| 30 | $min = $this->field->min; | |
| 31 | $max = $this->field->max; | |
| 32 | $unit = $this->field->unit === null ? '' : ' ' . $this->field->unit; | |
| 33 | ||
| 34 | return match (true) { | |
| 35 | $min !== null && $max !== null => \sprintf('%s to %s%s', $min, $max, $unit), | |
| 36 | $min !== null => \sprintf('at least %s%s', $min, $unit), | |
| 37 | $max !== null => \sprintf('at most %s%s', $max, $unit), | |
| 38 | $unit !== '' => 'in' . $unit, | |
| 39 | default => null, | |
| 40 | }; | |
| 41 | } | |
| 42 | } |