Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Heading | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| render | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Block; |
| 6 | |
| 7 | use Cosray\Contract\Block; |
| 8 | use Cosray\Field; |
| 9 | use Cosray\Schema\DefaultValue; |
| 10 | use Cosray\Schema\Label; |
| 11 | use Cosray\Schema\Options; |
| 12 | use Cosray\Schema\Placeholder; |
| 13 | use Cosray\Schema\Required; |
| 14 | use Cosray\Schema\Translate; |
| 15 | use Cosray\Schema\Validate; |
| 16 | use Cosray\Value\Block as BlockValue; |
| 17 | |
| 18 | #[Label('block:heading')] |
| 19 | final class Heading implements Block |
| 20 | { |
| 21 | public const int DEFAULT_LEVEL = 2; |
| 22 | |
| 23 | #[Label('block:heading-text'), Required, Translate, Placeholder('block:heading-placeholder')] |
| 24 | protected Field\Text $text; |
| 25 | |
| 26 | // The option shape alone accepts any string, hence the rule. |
| 27 | #[Label('block:heading-level'), Options(['1', '2', '3', '4', '5', '6'])] |
| 28 | #[DefaultValue('2'), Validate('in:1,2,3,4,5,6')] |
| 29 | protected Field\Option $level; |
| 30 | |
| 31 | public function render(BlockValue $block, RenderContext $ctx): string |
| 32 | { |
| 33 | $level = max(1, min(6, (int) $block->level->unwrap() ?: self::DEFAULT_LEVEL)); |
| 34 | |
| 35 | return "<h{$level}>{$block->text}</h{$level}>"; |
| 36 | } |
| 37 | } |