-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.php
86 lines (73 loc) · 2 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Qubus\Form
*
* @link https://github.com/QubusPHP/form
* @copyright 2023
* @author Joshua Parker <joshua@joshuaparker.dev>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 1.0.0
*/
declare(strict_types=1);
namespace Qubus\Form;
use Qubus\Form\FormBuilder\Group;
use function base_convert;
use function strtolower;
use function uniqid;
/**
* Representation of an HTML <form>.
*
* @option method Form method attribute
* @option action Form action attribute
*/
class Form extends Group
{
/** @var string */
public const TAGNAME = 'form';
/**
* @param array $options Element options
* @param array $attr HTML attributes
*/
public function __construct(array $options = [], array $attr = [])
{
if (isset($options['method'])) {
$attr['method'] = $options['method'];
}
$attr += ['method' => 'post'];
if (isset($options['action'])) {
$attr['action'] = $options['action'];
}
unset($options['method'], $options['action']);
parent::__construct($options, $attr);
}
/**
* Get unique identifier.
*/
public function getId(): string
{
if (! isset($this->options['id'])) {
$this->options['id'] = isset($this->options['name']) ?
$this->options['name'] . '-form' :
base_convert(uniqid(), 16, 36);
}
return $this->options['id'];
}
/**
* Check if method matches and apply $_POST or $_GET parameters.
*
* @param bool $apply Set values using $_POST or $_GET parameters
* @return bool
*/
public function isSubmitted(bool $apply = true): bool
{
$method = strtolower($_SERVER['REQUEST_METHOD']);
if ($method !== strtolower($this->getAttr('method'))) {
return false;
}
if ($apply) {
$this->setValues($method === 'GET' ? $_GET : $_POST + $_FILES);
}
return true;
}
}