Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Heading
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Block;
6
7use Cosray\Contract\Block;
8use Cosray\Field;
9use Cosray\Schema\DefaultValue;
10use Cosray\Schema\Label;
11use Cosray\Schema\Options;
12use Cosray\Schema\Placeholder;
13use Cosray\Schema\Required;
14use Cosray\Schema\Translate;
15use Cosray\Schema\Validate;
16use Cosray\Value\Block as BlockValue;
17
18#[Label('block:heading')]
19final 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}