Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wp-forms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
wp-forms
Merge requests
!28
release: 3.0.0
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
release: 3.0.0
devel
into
master
Overview
0
Commits
46
Pipelines
1
Changes
30
Merged
Krzysztof Dyszczyk
requested to merge
devel
into
master
2 years ago
Overview
0
Commits
46
Pipelines
1
Changes
30
0
0
Merge request reports
Viewing commit
87f91d17
Prev
Next
Show latest version
30 files
+
149
−
178
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
30
Unverified
87f91d17
feat: hermetize BasicField methods
· 87f91d17
Bartek Jaskulski
authored
3 years ago
src/Field/Traits/HtmlAttributes.php
+
30
−
12
View file @ 87f91d17
Edit in single-file editor
Open in Web IDE
Show full file
@@ -9,30 +9,44 @@ namespace WPDesk\Forms\Field\Traits;
*/
trait
HtmlAttributes
{
/** @var string[] */
protected
$attributes
;
/** @var array{placeholder: string, name: string, id: string, class: string[], readonly: bool, multiple: bool, disabled: bool, required: bool, method: string, action: string} */
protected
$attributes
=
[
'placeholder'
=>
''
,
'name'
=>
''
,
'id'
=>
''
,
'class'
=>
[],
'action'
=>
''
,
'method'
=>
'POST'
,
'readonly'
=>
false
,
'multiple'
=>
false
,
'disabled'
=>
false
,
'required'
=>
false
,
];
/**
* Get list of all attributes except given.
*
* @param string[] $except
*
* @return string[]
* @return
array<
string[]
|string|bool>
*/
public
function
get_attributes
(
array
$except
=
[
'name'
,
'type'
]
)
:
array
{
final
public
function
get_attributes
(
array
$except
=
[
'name'
]
):
array
{
return
array_filter
(
$this
->
attributes
,
static
function
(
$value
,
$key
)
use
(
$except
)
{
static
function
(
$key
)
use
(
$except
)
{
return
!
in_array
(
$key
,
$except
,
true
);
},
ARRAY_FILTER_USE_
BOTH
ARRAY_FILTER_USE_
KEY
);
}
/**
* @param string $name
* @param string[]|string|bool $value
*
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
*/
public
function
set_attribute
(
string
$name
,
string
$value
)
{
final
public
function
set_attribute
(
string
$name
,
$value
)
{
$this
->
attributes
[
$name
]
=
$value
;
return
$this
;
@@ -41,17 +55,21 @@ trait HtmlAttributes {
/**
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
*/
public
function
unset_attribute
(
string
$name
)
{
final
public
function
unset_attribute
(
string
$name
)
{
unset
(
$this
->
attributes
[
$name
]
);
return
$this
;
}
public
function
is_attribute_set
(
string
$name
):
bool
{
return
isset
(
$this
->
attributes
[
$name
]
);
final
public
function
is_attribute_set
(
string
$name
):
bool
{
return
!
empty
(
$this
->
attributes
[
$name
]
);
}
public
function
get_attribute
(
string
$name
,
string
$default
=
null
):
string
{
return
$this
->
attributes
[
$name
]
??
$default
??
''
;
final
public
function
get_attribute
(
string
$name
,
string
$default
=
null
):
string
{
if
(
is_array
(
$this
->
attributes
[
$name
]
)
)
{
// Be aware of coercing - if implode returns string(0) '', then return $default value.
return
implode
(
' '
,
$this
->
attributes
[
$name
]
)
?:
$default
??
''
;
}
return
(
string
)
$this
->
attributes
[
$name
]
??
$default
??
''
;
}
}
Loading