Skip to content
Snippets Groups Projects
Unverified Commit deee9690 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: hermetize BasicField methods

parent 47c8e032
No related branches found
No related tags found
3 merge requests!28release: 3.0.0,!23Feature/strong typing pp,!19Add strong typing for 3.0 version
Pipeline #6112 passed
Showing
with 123 additions and 140 deletions
...@@ -4,6 +4,7 @@ namespace WPDesk\Forms; ...@@ -4,6 +4,7 @@ namespace WPDesk\Forms;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use WPDesk\Persistence\PersistentContainer; use WPDesk\Persistence\PersistentContainer;
use Psr\Container\ContainerInterface;
/** /**
* Persistent container support for forms. * Persistent container support for forms.
...@@ -16,7 +17,7 @@ interface ContainerForm { ...@@ -16,7 +17,7 @@ interface ContainerForm {
* *
* @return void * @return void
*/ */
public function set_data( $data ); public function set_data( ContainerInterface $data );
/** /**
* Put data from form into a container. * Put data from form into a container.
......
...@@ -58,19 +58,22 @@ interface Field { ...@@ -58,19 +58,22 @@ interface Field {
public function is_attribute_set( string $name ): bool; public function is_attribute_set( string $name ): bool;
public function get_meta_value( string $name ): string; /** @return mixed */
public function get_meta_value( string $name );
public function is_meta_value_set( string $name ): bool; public function is_meta_value_set( string $name ): bool;
public function get_classes(): string; public function get_classes(): string;
public function get_type(): string;
public function has_classes(): bool; public function has_classes(): bool;
public function is_class_set( string $name ): bool; public function is_class_set( string $name ): bool;
public function has_data(): bool; public function has_data(): bool;
/** @return array<string,int> */ /** @return array<string|int> */
public function get_data(): array; public function get_data(): array;
public function add_data( string $data_name, string $data_value ): Field; public function add_data( string $data_name, string $data_value ): Field;
......
...@@ -22,112 +22,113 @@ abstract class BasicField implements Field { ...@@ -22,112 +22,113 @@ abstract class BasicField implements Field {
const DEFAULT_PRIORITY = 10; const DEFAULT_PRIORITY = 10;
/** @var array<string,int,bool> */ /** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>, serializer: ?Serializer} */
protected $meta; protected $meta = [
'priority' => self::DEFAULT_PRIORITY,
/** @var string */ 'default_value' => '',
protected $default_value; 'label' => '',
'description' => '',
public function __construct() { 'description_tip' => '',
$this->meta['class'] = []; 'data' => [],
$this->meta['priority'] = self::DEFAULT_PRIORITY; 'serializer' => null,
} ];
public function get_label(): string { final public function get_label(): string {
return $this->meta['label']; return $this->meta['label'];
} }
public function set_label( string $value ): Field { final public function set_label( string $value ): Field {
$this->meta['label'] = $value; $this->meta['label'] = $value;
return $this; return $this;
} }
public function get_description_tip(): string { final public function get_description_tip(): string {
return $this->meta['description_tip']; return $this->meta['description_tip'];
} }
public function has_description_tip(): bool { final public function has_description_tip(): bool {
return isset( $this->meta['description_tip'] ); return ! empty( $this->meta['description_tip'] );
} }
/** Override method if you need. */
public function should_override_form_template(): bool { public function should_override_form_template(): bool {
return $this->attributes['overrite_template'] ?? false; return false;
} }
public function get_description(): string { final public function get_description(): string {
return $this->meta['description']; return $this->meta['description'];
} }
public function has_label(): bool { final public function has_label(): bool {
return isset( $this->meta['label'] ); return ! empty( $this->meta['label'] );
} }
public function has_description(): bool { final public function has_description(): bool {
return isset( $this->meta['description'] ); return ! empty( $this->meta['description'] );
} }
public function set_description( string $value ): Field { final public function set_description( string $value ): Field {
$this->meta['description'] = $value; $this->meta['description'] = $value;
return $this; return $this;
} }
public function set_description_tip( string $value ): Field { final public function set_description_tip( string $value ): Field {
$this->meta['description_tip'] = $value; $this->meta['description_tip'] = $value;
return $this; return $this;
} }
public function get_type(): string { public function get_type(): string {
return $this->attributes['type']; return 'text';
} }
public function set_placeholder( string $value ): Field { final public function set_placeholder( string $value ): Field {
$this->meta['placeholder'] = $value; $this->attributes['placeholder'] = $value;
return $this; return $this;
} }
public function has_placeholder(): bool { final public function has_placeholder(): bool {
return isset( $this->meta['placeholder'] ); return ! empty( $this->attributes['placeholder'] );
} }
public function get_placeholder(): string { final public function get_placeholder(): string {
return $this->meta['placeholder']; return $this->attributes['placeholder'];
} }
public function set_name( string $name ): Field { final public function set_name( string $name ): Field {
$this->attributes['name'] = $name; $this->attributes['name'] = $name;
return $this; return $this;
} }
public function get_meta_value( string $name ): string { final public function get_meta_value( string $name ) {
return $this->meta[ $name ]; return $this->meta[ $name ];
} }
public function get_classes(): string { final public function get_classes(): string {
return implode( ' ', $this->meta['class'] ); return implode( ' ', $this->attributes['class'] );
} }
public function has_classes(): bool { final public function has_classes(): bool {
return ! empty( $this->meta['class'] ); return ! empty( $this->attributes['class'] );
} }
public function has_data(): bool { final public function has_data(): bool {
return ! empty( $this->meta['data'] ); return ! empty( $this->meta['data'] );
} }
public function get_data(): array { final public function get_data(): array {
return $this->meta['data'] ?: []; return $this->meta['data'];
} }
public function get_possible_values() { final public function get_possible_values() {
return $this->meta['possible_values'] ?? []; return ! empty( $this->meta['possible_values'] ) ? $this->meta['possible_values'] : [];
} }
public function get_id(): string { final public function get_id(): string {
return $this->attributes['id'] ?? sanitize_title( $this->get_name() ); return $this->attributes['id'] ?? sanitize_title( $this->get_name() );
} }
...@@ -135,50 +136,50 @@ abstract class BasicField implements Field { ...@@ -135,50 +136,50 @@ abstract class BasicField implements Field {
return $this->attributes['name']; return $this->attributes['name'];
} }
public function is_multiple(): bool { final public function is_multiple(): bool {
return $this->attributes['multiple'] ?? false; return $this->attributes['multiple'];
} }
public function set_disabled(): Field { final public function set_disabled(): Field {
$this->attributes['disabled'] = true; $this->attributes['disabled'] = true;
return $this; return $this;
} }
public function is_disabled(): bool { final public function is_disabled(): bool {
return $this->attributes['disabled'] ?? false; return $this->attributes['disabled'];
} }
public function set_readonly(): Field { final public function set_readonly(): Field {
$this->attributes['readonly'] = true; $this->attributes['readonly'] = true;
return $this; return $this;
} }
public function is_readonly(): bool { final public function is_readonly(): bool {
return $this->attributes['readonly'] ?? false; return $this->attributes['readonly'];
} }
public function set_required(): Field { final public function set_required(): Field {
$this->meta['required'] = true; $this->attributes['required'] = true;
return $this; return $this;
} }
public function add_class( string $class_name ): Field { final public function add_class( string $class_name ): Field {
$this->meta['class'][ $class_name ] = $class_name; $this->attributes['class'][ $class_name ] = $class_name;
return $this; return $this;
} }
public function unset_class( string $class_name ): Field { final public function unset_class( string $class_name ): Field {
unset( $this->meta['class'][ $class_name ] ); unset( $this->attributes['class'][ $class_name ] );
return $this; return $this;
} }
public function add_data( string $data_name, string $data_value ): Field { final public function add_data( string $data_name, string $data_value ): Field {
if ( ! isset( $this->meta['data'] ) ) { if ( empty( $this->meta['data'] ) ) {
$this->meta['data'] = []; $this->meta['data'] = [];
} }
$this->meta['data'][ $data_name ] = $data_value; $this->meta['data'][ $data_name ] = $data_value;
...@@ -186,28 +187,26 @@ abstract class BasicField implements Field { ...@@ -186,28 +187,26 @@ abstract class BasicField implements Field {
return $this; return $this;
} }
public function unset_data( string $data_name ): Field { final public function unset_data( string $data_name ): Field {
unset( $this->meta['data'][ $data_name ] ); unset( $this->meta['data'][ $data_name ] );
return $this; return $this;
} }
public function is_meta_value_set( string $name ): bool { final public function is_meta_value_set( string $name ): bool {
return isset( $this->meta[ $name ] ); return ! empty( $this->meta[ $name ] );
} }
public function is_class_set( string $name ): bool { final public function is_class_set( string $name ): bool {
return isset( $this->meta['class'][ $name ] ); return ! empty( $this->attributes['class'][ $name ] );
} }
/** @return mixed */ final public function get_default_value(): string {
public function get_default_value() { return $this->meta['default_value'];
return $this->default_value;
} }
/** @param mixed $value */ final public function set_default_value( string $value ): Field {
public function set_default_value( $value ): Field { $this->meta['default_value'] = $value;
$this->default_value = $value;
return $this; return $this;
} }
...@@ -221,16 +220,16 @@ abstract class BasicField implements Field { ...@@ -221,16 +220,16 @@ abstract class BasicField implements Field {
return $chain; return $chain;
} }
public function is_required(): bool { final public function is_required(): bool {
return $this->meta['required'] ?? false; return $this->attributes['required'];
} }
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
return new NoSanitize(); return new NoSanitize();
} }
public function get_serializer(): Serializer { final public function get_serializer(): Serializer {
if ( isset( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) { if ( ! empty( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) {
return $this->meta['serializer']; return $this->meta['serializer'];
} }
...@@ -243,7 +242,7 @@ abstract class BasicField implements Field { ...@@ -243,7 +242,7 @@ abstract class BasicField implements Field {
return $this; return $this;
} }
public function get_priority(): int { final public function get_priority(): int {
return $this->meta['priority']; return $this->meta['priority'];
} }
...@@ -252,7 +251,7 @@ abstract class BasicField implements Field { ...@@ -252,7 +251,7 @@ abstract class BasicField implements Field {
* *
* @see FormWithFields::get_fields() * @see FormWithFields::get_fields()
*/ */
public function set_priority( int $priority ): Field { final public function set_priority( int $priority ): Field {
$this->meta['priority'] = $priority; $this->meta['priority'] = $priority;
return $this; return $this;
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
class ButtonField extends NoValueField { class ButtonField extends NoValueField {
public function get_template_name(): string { public function get_template_name(): string {
return 'button'; return 'button';
} }
public function get_type(): string { public function get_type(): string {
return 'button'; return 'button';
} }
......
...@@ -8,9 +8,8 @@ class CheckboxField extends BasicField { ...@@ -8,9 +8,8 @@ class CheckboxField extends BasicField {
const VALUE_TRUE = 'yes'; const VALUE_TRUE = 'yes';
const VALUE_FALSE = 'no'; const VALUE_FALSE = 'no';
public function __construct() { public function get_type(): string {
parent::__construct(); return 'checkbox';
$this->set_attribute( 'type', 'checkbox' );
} }
public function get_template_name(): string { public function get_template_name(): string {
......
...@@ -7,11 +7,8 @@ use WPDesk\Forms\Sanitizer\TextFieldSanitizer; ...@@ -7,11 +7,8 @@ use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class DatePickerField extends BasicField { class DatePickerField extends BasicField {
public function __construct() { public function __construct() {
parent::__construct();
$this->set_default_value( '' );
$this->add_class( 'date-picker' ); $this->add_class( 'date-picker' );
$this->set_placeholder( 'YYYY-MM-DD' ); $this->set_placeholder( 'YYYY-MM-DD' );
$this->set_attribute( 'type', 'text' );
} }
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
......
...@@ -6,7 +6,6 @@ use WPDesk\Forms\Field; ...@@ -6,7 +6,6 @@ use WPDesk\Forms\Field;
class Header extends NoValueField { class Header extends NoValueField {
public function __construct() { public function __construct() {
parent::__construct();
$this->meta['header_size'] = ''; $this->meta['header_size'] = '';
} }
......
...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; ...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class HiddenField extends BasicField { class HiddenField extends BasicField {
public function __construct() { public function get_type(): string {
parent::__construct(); return 'hidden';
$this->set_default_value( '' );
$this->set_attribute( 'type', 'hidden' );
} }
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
......
...@@ -4,12 +4,6 @@ namespace WPDesk\Forms\Field; ...@@ -4,12 +4,6 @@ namespace WPDesk\Forms\Field;
class ImageInputField extends BasicField { class ImageInputField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
$this->set_attribute( 'type', 'text' );
}
public function get_template_name(): string { public function get_template_name(): string {
return 'input-image'; return 'input-image';
} }
......
...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; ...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\EmailSanitizer; use WPDesk\Forms\Sanitizer\EmailSanitizer;
class InputEmailField extends BasicField { class InputEmailField extends BasicField {
public function __construct() { public function get_type(): string {
parent::__construct(); return 'email';
$this->set_default_value( '' );
$this->set_attribute( 'type', 'email' );
} }
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
......
...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; ...@@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class InputNumberField extends BasicField { class InputNumberField extends BasicField {
public function __construct() { public function get_type(): string {
parent::__construct(); return 'number';
$this->set_default_value( '' );
$this->set_attribute( 'type', 'number' );
} }
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
......
...@@ -6,12 +6,6 @@ use WPDesk\Forms\Sanitizer; ...@@ -6,12 +6,6 @@ use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class InputTextField extends BasicField { class InputTextField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
$this->set_attribute( 'type', 'text' );
}
public function get_sanitizer(): Sanitizer { public function get_sanitizer(): Sanitizer {
return new TextFieldSanitizer(); return new TextFieldSanitizer();
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
class MultipleInputTextField extends InputTextField { class MultipleInputTextField extends InputTextField {
public function get_template_name(): string { public function get_template_name(): string {
return 'input-text-multiple'; return 'input-text-multiple';
} }
......
...@@ -7,8 +7,7 @@ use WPDesk\Forms\Validator\NonceValidator; ...@@ -7,8 +7,7 @@ use WPDesk\Forms\Validator\NonceValidator;
class NoOnceField extends BasicField { class NoOnceField extends BasicField {
public function __construct( $action_name ) { public function __construct( string $action_name ) {
parent::__construct();
$this->meta['action'] = $action_name; $this->meta['action'] = $action_name;
} }
......
...@@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field; ...@@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field;
class ProductSelect extends SelectField { class ProductSelect extends SelectField {
public function __construct() { public function __construct() {
parent::__construct();
$this->set_multiple(); $this->set_multiple();
} }
......
...@@ -3,10 +3,6 @@ ...@@ -3,10 +3,6 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
class TextAreaField extends BasicField { class TextAreaField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
}
public function get_template_name(): string { public function get_template_name(): string {
return 'textarea'; return 'textarea';
......
...@@ -9,30 +9,44 @@ namespace WPDesk\Forms\Field\Traits; ...@@ -9,30 +9,44 @@ namespace WPDesk\Forms\Field\Traits;
*/ */
trait HtmlAttributes { trait HtmlAttributes {
/** @var string[] */ /** @var array{placeholder: string, name: string, id: string, class: string[], readonly: bool, multiple: bool, disabled: bool, required: bool, method: string, action: string} */
protected $attributes; protected $attributes = [
'placeholder' => '',
'name' => '',
'id' => '',
'class' => [],
'action' => '',
'method' => 'POST',
'readonly' => false,
'multiple' => false,
'disabled' => false,
'required' => false,
];
/** /**
* 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( array $except = [ 'name', 'type' ] ): array { final public function get_attributes( array $except = [ 'name' ] ): array {
return array_filter( return array_filter(
$this->attributes, $this->attributes,
static function ( $value, $key ) use ( $except ) { 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[]|string|bool $value
*
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form * @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; $this->attributes[ $name ] = $value;
return $this; return $this;
...@@ -41,17 +55,21 @@ trait HtmlAttributes { ...@@ -41,17 +55,21 @@ trait HtmlAttributes {
/** /**
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form * @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
*/ */
public function unset_attribute( string $name ) { final public function unset_attribute( string $name ) {
unset( $this->attributes[ $name ] ); unset( $this->attributes[ $name ] );
return $this; return $this;
} }
public function is_attribute_set( string $name ): bool { final public function is_attribute_set( string $name ): bool {
return isset( $this->attributes[ $name ] ); return ! empty( $this->attributes[ $name ] );
} }
public function get_attribute( string $name, string $default = null ): string { final public function get_attribute( string $name, string $default = null ): string {
return $this->attributes[ $name ] ?? $default ?? ''; 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 ?? '';
} }
} }
...@@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field; ...@@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field;
class WooSelect extends SelectField { class WooSelect extends SelectField {
public function __construct() { public function __construct() {
parent::__construct();
$this->set_multiple(); $this->set_multiple();
$this->add_class( 'wc-enhanced-select' ); $this->add_class( 'wc-enhanced-select' );
} }
......
...@@ -3,11 +3,6 @@ ...@@ -3,11 +3,6 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
class WyswigField extends BasicField { class WyswigField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
}
public function get_template_name(): string { public function get_template_name(): string {
return 'wyswig'; return 'wyswig';
} }
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
namespace WPDesk\Forms; namespace WPDesk\Forms;
use Psr\Container\ContainerInterface;
/** /**
* Some field owners can receive and process field data. * Some field owners can receive and process field data.
* Probably should be used with FieldProvider interface. * Probably should be used with FieldProvider interface.
...@@ -16,5 +14,5 @@ interface FieldsDataReceiver { ...@@ -16,5 +14,5 @@ interface FieldsDataReceiver {
* *
* @return void * @return void
*/ */
public function update_fields_data( ContainerInterface $data ); public function update_fields_data( \Psr\Container\ContainerInterface $data );
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment