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
!19
Add strong typing for 3.0 version
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Add strong typing for 3.0 version
feature/strong-typing
into
devel
Overview
4
Commits
47
Pipelines
1
Changes
71
Merged
Krzysztof Dyszczyk
requested to merge
feature/strong-typing
into
devel
3 years ago
Overview
3
Commits
47
Pipelines
1
Changes
71
0
0
Merge request reports
Compare
devel
devel (base)
and
latest version
latest version
aef962c8
47 commits,
2 years ago
71 files
+
822
−
1298
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
71
src/Field/Traits/HtmlAttributes.php
+
29
−
30
View file @ aef962c8
Edit in single-file editor
Open in Web IDE
Show full file
@@ -2,6 +2,9 @@
@@ -2,6 +2,9 @@
namespace
WPDesk\Forms\Field\Traits
;
namespace
WPDesk\Forms\Field\Traits
;
use
WPDesk\Forms\Field
;
use
WPDesk\Forms\Form
;
/**
/**
* Implementation of HTML attributes like id, name, action etc.
* Implementation of HTML attributes like id, name, action etc.
*
*
@@ -9,61 +12,57 @@ namespace WPDesk\Forms\Field\Traits;
@@ -9,61 +12,57 @@ namespace WPDesk\Forms\Field\Traits;
*/
*/
trait
HtmlAttributes
{
trait
HtmlAttributes
{
/** @var string[] */
/** @var
array{placeholder: string, name: string, id: string, class:
string[]
}
*/
protected
$attributes
;
protected
$attributes
=
[]
;
/**
/**
* Get list of all attributes except given.
* Get list of all attributes except given.
*
*
* @param string[] $except
* @param string[] $except
*
*
* @return string[]
* @return
array<
string[]
|string|bool>
*/
*/
public
function
get_attributes
(
$except
=
[
'name'
,
'type'
]
)
{
final
public
function
get_attributes
(
array
$except
=
[
'name'
,
'class'
]
)
:
array
{
return
array_filter
(
$this
->
attributes
,
function
(
$value
,
$key
)
use
(
$except
)
{
return
array_filter
(
$this
->
attributes
,
static
function
(
$key
)
use
(
$except
)
{
return
!
in_array
(
$key
,
$except
,
true
);
return
!
in_array
(
$key
,
$except
,
true
);
},
ARRAY_FILTER_USE_BOTH
);
},
ARRAY_FILTER_USE_KEY
);
}
}
/**
/**
* @param string $name
* @param string $name
* @param string $value
* @param string
[]|string|bool
$value
*
*
* @return
$this
* @return
Field|Form
*/
*/
public
function
set_attribute
(
$name
,
$value
)
{
final
public
function
set_attribute
(
string
$name
,
$value
)
{
$this
->
attributes
[
$name
]
=
$value
;
$this
->
attributes
[
$name
]
=
$value
;
return
$this
;
return
$this
;
}
}
/**
/**
* @param string $name
* @return HtmlAttributes
*
* @return $this
*/
*/
public
function
unset_attribute
(
$name
)
{
final
public
function
unset_attribute
(
string
$name
)
{
unset
(
$this
->
attributes
[
$name
]
);
unset
(
$this
->
attributes
[
$name
]
);
return
$this
;
return
$this
;
}
}
/**
final
public
function
is_attribute_set
(
string
$name
):
bool
{
* @param string $name
return
!
empty
(
$this
->
attributes
[
$name
]
);
*
* @return bool
*/
public
function
is_attribute_set
(
$name
)
{
return
isset
(
$this
->
attributes
[
$name
]
);
}
}
/**
final
public
function
get_attribute
(
string
$name
,
string
$default
=
null
):
string
{
* @param string $name
if
(
is_array
(
$this
->
attributes
[
$name
]
)
)
{
* @param mixed $default
// Be aware of coercing - if implode returns string(0) '', then return $default value.
*
return
implode
(
' '
,
$this
->
attributes
[
$name
]
)
?:
$default
??
''
;
* @return string
}
*/
public
function
get_attribute
(
$name
,
$default
=
null
)
{
return
(
string
)
(
$this
->
attributes
[
$name
]
??
$default
??
''
);
return
$this
->
attributes
[
$name
]
??
$default
;
}
}
}
}
Loading