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
2
Merged
Krzysztof Dyszczyk
requested to merge
feature/strong-typing
into
devel
3 years ago
Overview
3
Commits
47
Pipelines
1
Changes
2
0
0
Merge request reports
Viewing commit
7596dd72
Prev
Next
Show latest version
2 files
+
3
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
7596dd72
fix: reformat
· 7596dd72
potreb
authored
3 years ago
src/Field/BasicField.php
+
122
−
166
View file @ aef962c8
Edit in single-file editor
Open in Web IDE
Show full file
@@ -2,225 +2,209 @@
namespace
WPDesk\Forms\Field
;
use
BadMethodCallException
;
use
WPDesk\Forms\Field
;
use
WPDesk\Forms\Sanitizer
;
use
WPDesk\Forms\Sanitizer\NoSanitize
;
use
WPDesk\Forms\Serializer
;
use
WPDesk\Forms\
Serializer\NoSerialize
;
use
WPDesk\Forms\
Validator
;
use
WPDesk\Forms\Validator\ChainValidator
;
use
WPDesk\Forms\Validator\RequiredValidator
;
/**
* Base class for fields. Is responsible for settings all required field values and provides standard implementation for
* the field interface.
*
* @package WPDesk\Forms
*/
abstract
class
BasicField
implements
Field
{
use
Field\Traits\HtmlAttributes
;
/** @var array[] */
protected
$meta
;
const
DEFAULT_PRIORITY
=
10
;
protected
$default_value
;
/** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */
protected
$meta
=
[
'priority'
=>
self
::
DEFAULT_PRIORITY
,
'default_value'
=>
''
,
'label'
=>
''
,
'description'
=>
''
,
'description_tip'
=>
''
,
'data'
=>
[],
'type'
=>
'text'
,
];
public
function
__construct
()
{
$this
->
meta
[
'class'
]
=
[]
;
public
function
should_override_form_template
():
bool
{
return
false
;
}
public
function
get_
label
()
{
return
$this
->
meta
[
'
label
'
];
public
function
get_
type
():
string
{
return
$this
->
meta
[
'
type
'
];
}
/**
* @param string $value
*
* @return $this
*/
public
function
set_label
(
$value
)
{
$this
->
meta
[
'label'
]
=
$value
;
public
function
set_type
(
string
$type
):
Field
{
$this
->
meta
[
'type'
]
=
$type
;
return
$this
;
}
public
function
get_description_tip
()
{
return
$this
->
meta
[
'description_tip'
];
public
function
get_validator
():
Validator
{
$chain
=
new
ChainValidator
();
if
(
$this
->
is_required
()
)
{
$chain
->
attach
(
new
RequiredValidator
()
);
}
return
$chain
;
}
public
function
has_description_tip
()
{
return
isset
(
$this
->
meta
[
'description_tip'
]
);
public
function
get_sanitizer
():
Sanitizer
{
return
new
NoSanitize
(
);
}
public
function
should_override_form_template
()
{
return
isset
(
$this
->
attributes
[
'overrite_template'
]
)
?
$this
->
attributes
[
'overrite_template'
]
:
false
;
public
function
has_serializer
():
bool
{
return
false
;
}
public
function
get_
description
()
{
return
$this
->
meta
[
'description'
]
;
public
function
get_
serializer
():
Serializer
{
throw
new
BadMethodCallException
(
'You must define your serializer in a child class.'
)
;
}
public
function
has_label
()
{
return
isset
(
$this
->
meta
[
'label'
]
)
;
final
public
function
get_name
():
string
{
return
$this
->
attributes
[
'name'
]
??
''
;
}
public
function
has_description
()
{
return
isset
(
$this
->
meta
[
'
description'
]
)
;
final
public
function
get_label
():
string
{
return
$this
->
meta
[
'
label'
]
??
''
;
}
public
function
set_
description
(
$value
)
{
$this
->
meta
[
'
description
'
]
=
$value
;
final
public
function
set_
label
(
string
$value
)
:
Field
{
$this
->
meta
[
'
label
'
]
=
$value
;
return
$this
;
}
public
function
set_description_tip
(
$value
)
{
$this
->
meta
[
'description_tip'
]
=
$value
;
final
public
function
get_description_tip
():
string
{
return
$this
->
meta
[
'description_tip'
]
??
''
;
}
final
public
function
has_description_tip
():
bool
{
return
!
empty
(
$this
->
meta
[
'description_tip'
]
);
}
final
public
function
get_description
():
string
{
return
$this
->
meta
[
'description'
]
??
''
;
}
final
public
function
has_label
():
bool
{
return
!
empty
(
$this
->
meta
[
'label'
]
);
}
final
public
function
has_description
():
bool
{
return
!
empty
(
$this
->
meta
[
'description'
]
);
}
final
public
function
set_description
(
string
$value
):
Field
{
$this
->
meta
[
'description'
]
=
$value
;
return
$this
;
}
/**
* @return array
*
* @deprecated not sure if needed. TODO: Check later.
*/
public
function
get_type
()
{
return
$this
->
attributes
[
'type'
];
final
public
function
set_description_tip
(
string
$value
):
Field
{
$this
->
meta
[
'description_tip'
]
=
$value
;
return
$this
;
}
/**
* @param string $value
*
* @return $this
*/
public
function
set_placeholder
(
$value
)
{
$this
->
meta
[
'placeholder'
]
=
$value
;
final
public
function
set_placeholder
(
string
$value
):
Field
{
$this
->
attributes
[
'placeholder'
]
=
$value
;
return
$this
;
}
public
function
has_placeholder
()
{
return
isset
(
$this
->
meta
[
'placeholder'
]
);
final
public
function
has_placeholder
()
:
bool
{
return
!
empty
(
$this
->
attributes
[
'placeholder'
]
);
}
public
function
get_placeholder
()
{
return
$this
->
meta
[
'placeholder'
];
final
public
function
get_placeholder
()
:
string
{
return
$this
->
attributes
[
'placeholder'
]
??
''
;
}
/**
* @param string $name
*
* @return $this
*/
public
function
set_name
(
$name
)
{
final
public
function
set_name
(
string
$name
):
Field
{
$this
->
attributes
[
'name'
]
=
$name
;
return
$this
;
}
public
function
get_meta_value
(
$name
)
{
return
$this
->
meta
[
$name
];
final
public
function
get_meta_value
(
string
$name
)
{
return
$this
->
meta
[
$name
]
??
''
;
}
public
function
get_classes
()
{
return
implode
(
' '
,
$this
->
meta
[
'class'
]
);
final
public
function
get_classes
()
:
string
{
return
implode
(
' '
,
$this
->
attributes
[
'class'
]
??
[]
);
}
public
function
has_classes
()
{
return
!
empty
(
$this
->
meta
[
'class'
]
);
final
public
function
has_classes
()
:
bool
{
return
!
empty
(
$this
->
attributes
[
'class'
]
);
}
public
function
has_data
()
{
final
public
function
has_data
()
:
bool
{
return
!
empty
(
$this
->
meta
[
'data'
]
);
}
/**
* @return array
*/
public
function
get_data
()
{
return
empty
(
$this
->
meta
[
'data'
]
)
?
[]
:
$this
->
meta
[
'data'
];
}
public
function
get_possible_values
()
{
return
isset
(
$this
->
meta
[
'possible_values'
]
)
?
$this
->
meta
[
'possible_values'
]
:
[];
final
public
function
get_data
():
array
{
return
$this
->
meta
[
'data'
]
??
[];
}
public
function
get_
id
()
{
return
isset
(
$this
->
attributes
[
'id'
]
)
?
$this
->
attributes
[
'id'
]
:
sanitize_title
(
$this
->
get_name
()
)
;
final
public
function
get_
possible_values
()
{
return
!
empty
(
$this
->
meta
[
'possible_values'
]
)
?
$this
->
meta
[
'possible_values'
]
:
[]
;
}
public
function
get_
name
()
{
return
$this
->
attributes
[
'
name'
]
;
final
public
function
get_
id
():
string
{
return
$this
->
attributes
[
'
id'
]
??
sanitize_title
(
$this
->
get_name
()
)
;
}
public
function
is_multiple
()
{
return
isset
(
$this
->
attributes
[
'multiple'
]
)
?
$this
->
attributes
[
'multiple'
]
:
false
;
final
public
function
is_multiple
()
:
bool
{
return
isset
(
$this
->
attributes
[
'multiple'
]
);
}
/**
* @return $this
*/
public
function
set_disabled
()
{
$this
->
attributes
[
'disabled'
]
=
true
;
final
public
function
set_disabled
():
Field
{
$this
->
attributes
[
'disabled'
]
=
'disabled'
;
return
$this
;
}
public
function
is_disabled
()
{
return
isset
(
$this
->
attributes
[
'disabled'
]
)
?
$this
->
attributes
[
'disabled'
]
:
false
;
final
public
function
is_disabled
()
:
bool
{
return
$this
->
attributes
[
'disabled'
]
??
false
;
}
/**
* @return $this
*/
public
function
set_readonly
()
{
$this
->
attributes
[
'readonly'
]
=
true
;
final
public
function
set_readonly
():
Field
{
$this
->
attributes
[
'readonly'
]
=
'readonly'
;
return
$this
;
}
public
function
is_readonly
()
{
return
isset
(
$this
->
attributes
[
'readonly'
]
)
?
$this
->
attributes
[
'readonly'
]
:
false
;
final
public
function
is_readonly
()
:
bool
{
return
$this
->
attributes
[
'readonly'
]
??
false
;
}
/**
* @return $this
*/
public
function
set_required
()
{
$this
->
meta
[
'required'
]
=
true
;
final
public
function
set_required
():
Field
{
$this
->
attributes
[
'required'
]
=
'required'
;
return
$this
;
}
/**
* @param string $class_name
*
* @return $this
*/
public
function
add_class
(
$class_name
)
{
$this
->
meta
[
'class'
][
$class_name
]
=
$class_name
;
final
public
function
add_class
(
string
$class_name
):
Field
{
$this
->
attributes
[
'class'
][
$class_name
]
=
$class_name
;
return
$this
;
}
/**
* @param string $class_name
*
* @return $this
*/
public
function
unset_class
(
$class_name
)
{
unset
(
$this
->
meta
[
'class'
][
$class_name
]
);
final
public
function
unset_class
(
string
$class_name
):
Field
{
unset
(
$this
->
attributes
[
'class'
][
$class_name
]
);
return
$this
;
}
/**
* @param string $data_name
* @param string $data_value
*
* @return $this
*/
public
function
add_data
(
$data_name
,
$data_value
)
{
if
(
!
isset
(
$this
->
meta
[
'data'
]
)
)
{
final
public
function
add_data
(
string
$data_name
,
string
$data_value
):
Field
{
if
(
empty
(
$this
->
meta
[
'data'
]
)
)
{
$this
->
meta
[
'data'
]
=
[];
}
$this
->
meta
[
'data'
][
$data_name
]
=
$data_value
;
@@ -228,73 +212,45 @@ abstract class BasicField implements Field {
return
$this
;
}
/**
* @param string $data_name
*
* @return $this
*/
public
function
unset_data
(
$data_name
)
{
final
public
function
unset_data
(
string
$data_name
):
Field
{
unset
(
$this
->
meta
[
'data'
][
$data_name
]
);
return
$this
;
}
public
function
is_meta_value_set
(
$name
)
{
return
isset
(
$this
->
meta
[
$name
]
);
final
public
function
is_meta_value_set
(
string
$name
)
:
bool
{
return
!
empty
(
$this
->
meta
[
$name
]
);
}
public
function
is_class_set
(
$name
)
{
return
isset
(
$this
->
meta
[
'class'
][
$name
]
);
final
public
function
is_class_set
(
string
$name
)
:
bool
{
return
!
empty
(
$this
->
attributes
[
'class'
][
$name
]
);
}
public
function
get_default_value
()
{
return
$this
->
default_value
;
final
public
function
get_default_value
()
:
string
{
return
$this
->
meta
[
'
default_value
'
]
??
''
;
}
/**
* @param string $value
*
* @return $this
*/
public
function
set_default_value
(
$value
)
{
$this
->
default_value
=
$value
;
final
public
function
set_default_value
(
string
$value
):
Field
{
$this
->
meta
[
'default_value'
]
=
$value
;
return
$this
;
}
/**
* @return ChainValidator
*/
public
function
get_validator
()
{
$chain
=
new
ChainValidator
();
if
(
$this
->
is_required
()
)
{
$chain
->
attach
(
new
RequiredValidator
()
);
}
return
$chain
;
final
public
function
is_required
():
bool
{
return
isset
(
$this
->
attributes
[
'required'
]
);
}
public
function
is_required
()
{
return
isset
(
$this
->
meta
[
'required'
]
)
?
$this
->
meta
[
'required'
]
:
false
;
}
public
function
get_sanitizer
()
{
return
new
NoSanitize
();
final
public
function
get_priority
():
int
{
return
$this
->
meta
[
'priority'
];
}
/**
* @return Serializer
* Fields are sorted by lowest priority value first, when getting FormWithFields
*
* @see FormWithFields::get_fields()
*/
public
function
get_serializer
()
{
if
(
isset
(
$this
->
meta
[
'serializer'
]
)
&&
$this
->
meta
[
'serializer'
]
instanceof
Serializer
)
{
return
$this
->
meta
[
'serializer'
];
}
return
new
NoSerialize
();
}
public
function
set_serializer
(
Serializer
$serializer
)
{
$this
->
meta
[
'serializer'
]
=
$serializer
;
final
public
function
set_priority
(
int
$priority
):
Field
{
$this
->
meta
[
'priority'
]
=
$priority
;
return
$this
;
}
Loading