|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Html\Tag; |
| 6 | + |
| 7 | +use Stringable; |
| 8 | +use Yiisoft\Html\Tag\Base\NormalTag; |
| 9 | +use Yiisoft\Html\Tag\Base\TagContentTrait; |
| 10 | + |
| 11 | +/** |
| 12 | + * @link https://html.spec.whatwg.org/multipage/forms.html#the-form-element |
| 13 | + */ |
| 14 | +final class Form extends NormalTag |
| 15 | +{ |
| 16 | + use TagContentTrait; |
| 17 | + |
| 18 | + private ?string $csrfToken = null; |
| 19 | + private ?string $csrfName = null; |
| 20 | + |
| 21 | + public function get(?string $url = null): self |
| 22 | + { |
| 23 | + $new = clone $this; |
| 24 | + $new->attributes['method'] = 'GET'; |
| 25 | + if ($url !== null) { |
| 26 | + $new->attributes['action'] = $url; |
| 27 | + } |
| 28 | + return $new; |
| 29 | + } |
| 30 | + |
| 31 | + public function post(?string $url = null): self |
| 32 | + { |
| 33 | + $new = clone $this; |
| 34 | + $new->attributes['method'] = 'POST'; |
| 35 | + if ($url !== null) { |
| 36 | + $new->attributes['action'] = $url; |
| 37 | + } |
| 38 | + return $new; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param string|Stringable|null $token |
| 43 | + */ |
| 44 | + public function csrf($token, string $name = '_csrf'): self |
| 45 | + { |
| 46 | + $new = clone $this; |
| 47 | + $new->csrfToken = $token === null ? null : (string)$token; |
| 48 | + $new->csrfName = $name; |
| 49 | + return $new; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Character encodings to use for form submission. |
| 54 | + * |
| 55 | + * @link https://html.spec.whatwg.org/multipage/forms.html#attr-form-accept-charset |
| 56 | + */ |
| 57 | + public function acceptCharset(?string $charset): self |
| 58 | + { |
| 59 | + $new = clone $this; |
| 60 | + $new->attributes['accept-charset'] = $charset; |
| 61 | + return $new; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The URL to use for form submission. |
| 66 | + * |
| 67 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-action |
| 68 | + */ |
| 69 | + public function action(?string $url): self |
| 70 | + { |
| 71 | + $new = clone $this; |
| 72 | + $new->attributes['action'] = $url; |
| 73 | + return $new; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Default setting for autofill feature for controls in the form. |
| 78 | + * |
| 79 | + * @link https://html.spec.whatwg.org/multipage/forms.html#attr-form-autocomplete |
| 80 | + */ |
| 81 | + public function autocomplete(bool $value = true): self |
| 82 | + { |
| 83 | + $new = clone $this; |
| 84 | + $new->attributes['autocomplete'] = $value ? 'on' : 'off'; |
| 85 | + return $new; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Entry list encoding type to use for form submission. |
| 90 | + * |
| 91 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-enctype |
| 92 | + */ |
| 93 | + public function enctype(?string $enctype): self |
| 94 | + { |
| 95 | + $new = clone $this; |
| 96 | + $new->attributes['enctype'] = $enctype; |
| 97 | + return $new; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The method content attribute specifies how the form-data should be submitted. |
| 102 | + * |
| 103 | + * @param string $method The method attribute value. |
| 104 | + * |
| 105 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method |
| 106 | + */ |
| 107 | + public function method(?string $method): self |
| 108 | + { |
| 109 | + $new = clone $this; |
| 110 | + $new->attributes['method'] = $method; |
| 111 | + return $new; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A boolean attribute, which, if present, indicate that the form is not to be validated during submission. |
| 116 | + * |
| 117 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-novalidate |
| 118 | + */ |
| 119 | + public function noValidate(bool $noValidate = true): self |
| 120 | + { |
| 121 | + $new = clone $this; |
| 122 | + $new->attributes['novalidate'] = $noValidate; |
| 123 | + return $new; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Browsing context for form submission. |
| 128 | + * |
| 129 | + * @param string|null $target The target attribute value. |
| 130 | + * |
| 131 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-target |
| 132 | + * @link https://html.spec.whatwg.org/multipage/browsers.html#valid-browsing-context-name-or-keyword |
| 133 | + */ |
| 134 | + public function target(?string $target): self |
| 135 | + { |
| 136 | + $new = clone $this; |
| 137 | + $new->attributes['target'] = $target; |
| 138 | + return $new; |
| 139 | + } |
| 140 | + |
| 141 | + protected function prepend(): string |
| 142 | + { |
| 143 | + return $this->csrfToken !== null |
| 144 | + ? PHP_EOL . Input::hidden($this->csrfName, $this->csrfToken) |
| 145 | + : ''; |
| 146 | + } |
| 147 | + |
| 148 | + protected function getName(): string |
| 149 | + { |
| 150 | + return 'form'; |
| 151 | + } |
| 152 | +} |
0 commit comments