• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 12021710761

25 Nov 2024 06:54PM UTC coverage: 79.441% (-2.6%) from 81.993%
12021710761

push

github

web-flow
ci: close stale issues and pull requests

7879 of 9918 relevant lines covered (79.44%)

61.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.76
/src/Tempest/View/src/Components/Input.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\View\Components;
6

7
use Tempest\Http\Session\Session;
8
use Tempest\Validation\Rule;
9
use Tempest\View\Elements\ViewComponentElement;
10
use Tempest\View\ViewComponent;
11

12
final readonly class Input implements ViewComponent
13
{
14
    public function __construct(
2✔
15
        private Session $session,
16
    ) {
17
    }
2✔
18

19
    public static function getName(): string
×
20
    {
21
        return 'x-input';
×
22
    }
23

24
    public function compile(ViewComponentElement $element): string
2✔
25
    {
26
        $name = $element->getAttribute('name');
2✔
27
        $label = $element->getAttribute('label');
2✔
28
        $type = $element->getAttribute('type');
2✔
29
        $default = $element->getAttribute('default');
2✔
30

31
        $errors = $this->getErrorsFor($name);
2✔
32

33
        $errorHtml = '';
2✔
34

35
        if ($errors) {
2✔
36
            $errorHtml = '<div>' . implode('', array_map(
1✔
37
                fn (Rule $failingRule) => "<div>{$failingRule->message()}</div>",
1✔
38
                $errors,
1✔
39
            )) . '</div>';
1✔
40
        }
41

42
        if ($type === 'textarea') {
2✔
43
            return <<<HTML
×
44
<div>
×
45
    <label for="{$name}">{$label}</label>
×
46
    <textarea name="{$name}" id="{$name}">{$this->original($name, $default)}</textarea>
×
47
    {$errorHtml}
×
48
</div>
49
HTML;
×
50
        }
51

52
        return <<<HTML
2✔
53
<div>
2✔
54
    <label for="{$name}">{$label}</label>
2✔
55
    <input type="{$type}" name="{$name}" id="{$name}" value="{$this->original($name, $default)}" />
2✔
56
    {$errorHtml}
2✔
57
</div>
58
HTML;
2✔
59
    }
60

61
    public function original(string $name, mixed $default = ''): mixed
2✔
62
    {
63
        return $this->session->get(Session::ORIGINAL_VALUES)[$name] ?? $default;
2✔
64
    }
65

66
    /** @return \Tempest\Validation\Rule[] */
67
    public function getErrorsFor(string $name): array
2✔
68
    {
69
        return $this->session->get(Session::VALIDATION_ERRORS)[$name] ?? [];
2✔
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc