From deee969042d4643c1571bd880ad338ca6516ea7d Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bartek.jaskulski@wpdesk.net> Date: Wed, 15 Sep 2021 13:21:39 +0200 Subject: [PATCH] feat: hermetize BasicField methods --- src/ContainerForm.php | 3 +- src/Field.php | 7 +- src/Field/BasicField.php | 151 ++++++++++----------- src/Field/ButtonField.php | 2 +- src/Field/CheckboxField.php | 5 +- src/Field/DatePickerField.php | 3 - src/Field/Header.php | 1 - src/Field/HiddenField.php | 6 +- src/Field/ImageInputField.php | 6 - src/Field/InputEmailField.php | 6 +- src/Field/InputNumberField.php | 6 +- src/Field/InputTextField.php | 6 - src/Field/MultipleInputTextField.php | 1 - src/Field/NoOnceField.php | 3 +- src/Field/ProductSelect.php | 1 - src/Field/TextAreaField.php | 4 - src/Field/Traits/HtmlAttributes.php | 42 ++++-- src/Field/WooSelect.php | 1 - src/Field/WyswigField.php | 5 - src/FieldsDataReceiver.php | 4 +- src/Form.php | 12 +- src/Form/FormWithFields.php | 12 +- src/Sanitizer.php | 8 +- src/Sanitizer/NoSanitize.php | 2 +- src/Serializer.php | 8 +- src/Serializer/JsonSerializer.php | 2 +- src/Serializer/NoSerialize.php | 6 +- src/Serializer/ProductSelectSerializer.php | 6 +- src/Serializer/SerializeSerializer.php | 4 +- src/Validator/NonceValidator.php | 4 +- 30 files changed, 149 insertions(+), 178 deletions(-) diff --git a/src/ContainerForm.php b/src/ContainerForm.php index 9c7e339..bd5fc82 100644 --- a/src/ContainerForm.php +++ b/src/ContainerForm.php @@ -4,6 +4,7 @@ namespace WPDesk\Forms; use Psr\Container\ContainerInterface; use WPDesk\Persistence\PersistentContainer; +use Psr\Container\ContainerInterface; /** * Persistent container support for forms. @@ -16,7 +17,7 @@ interface ContainerForm { * * @return void */ - public function set_data( $data ); + public function set_data( ContainerInterface $data ); /** * Put data from form into a container. diff --git a/src/Field.php b/src/Field.php index 42ea129..311d8eb 100644 --- a/src/Field.php +++ b/src/Field.php @@ -58,19 +58,22 @@ interface Field { 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 get_classes(): string; + public function get_type(): string; + public function has_classes(): bool; public function is_class_set( string $name ): bool; public function has_data(): bool; - /** @return array<string,int> */ + /** @return array<string|int> */ public function get_data(): array; public function add_data( string $data_name, string $data_value ): Field; diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 9444933..2855357 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -22,112 +22,113 @@ abstract class BasicField implements Field { const DEFAULT_PRIORITY = 10; - /** @var array<string,int,bool> */ - protected $meta; - - /** @var string */ - protected $default_value; - - public function __construct() { - $this->meta['class'] = []; - $this->meta['priority'] = self::DEFAULT_PRIORITY; - } - - public function get_label(): string { + /** @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 = [ + 'priority' => self::DEFAULT_PRIORITY, + 'default_value' => '', + 'label' => '', + 'description' => '', + 'description_tip' => '', + 'data' => [], + 'serializer' => null, + ]; + + final public function get_label(): string { return $this->meta['label']; } - public function set_label( string $value ): Field { + final public function set_label( string $value ): Field { $this->meta['label'] = $value; return $this; } - public function get_description_tip(): string { + final public function get_description_tip(): string { return $this->meta['description_tip']; } - public function has_description_tip(): bool { - return isset( $this->meta['description_tip'] ); + final public function has_description_tip(): bool { + return ! empty( $this->meta['description_tip'] ); } + /** Override method if you need. */ 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']; } - public function has_label(): bool { - return isset( $this->meta['label'] ); + final public function has_label(): bool { + return ! empty( $this->meta['label'] ); } - public function has_description(): bool { - return isset( $this->meta['description'] ); + final public function has_description(): bool { + return ! empty( $this->meta['description'] ); } - public function set_description( string $value ): Field { + final public function set_description( string $value ): Field { $this->meta['description'] = $value; return $this; } - public function set_description_tip( string $value ): Field { + final public function set_description_tip( string $value ): Field { $this->meta['description_tip'] = $value; return $this; } public function get_type(): string { - return $this->attributes['type']; + return 'text'; } - public function set_placeholder( string $value ): Field { - $this->meta['placeholder'] = $value; + final public function set_placeholder( string $value ): Field { + $this->attributes['placeholder'] = $value; return $this; } - public function has_placeholder(): bool { - return isset( $this->meta['placeholder'] ); + final public function has_placeholder(): bool { + return ! empty( $this->attributes['placeholder'] ); } - public function get_placeholder(): string { - return $this->meta['placeholder']; + final public function get_placeholder(): string { + return $this->attributes['placeholder']; } - public function set_name( string $name ): Field { + final public function set_name( string $name ): Field { $this->attributes['name'] = $name; return $this; } - public function get_meta_value( string $name ): string { + final public function get_meta_value( string $name ) { return $this->meta[ $name ]; } - public function get_classes(): string { - return implode( ' ', $this->meta['class'] ); + final public function get_classes(): string { + return implode( ' ', $this->attributes['class'] ); } - public function has_classes(): bool { - return ! empty( $this->meta['class'] ); + final public function has_classes(): bool { + return ! empty( $this->attributes['class'] ); } - public function has_data(): bool { + final public function has_data(): bool { return ! empty( $this->meta['data'] ); } - public function get_data(): array { - return $this->meta['data'] ?: []; + final public function get_data(): array { + return $this->meta['data']; } - public function get_possible_values() { - return $this->meta['possible_values'] ?? []; + final public function get_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() ); } @@ -135,50 +136,50 @@ abstract class BasicField implements Field { return $this->attributes['name']; } - public function is_multiple(): bool { - return $this->attributes['multiple'] ?? false; + final public function is_multiple(): bool { + return $this->attributes['multiple']; } - public function set_disabled(): Field { + final public function set_disabled(): Field { $this->attributes['disabled'] = true; return $this; } - public function is_disabled(): bool { - return $this->attributes['disabled'] ?? false; + final public function is_disabled(): bool { + return $this->attributes['disabled']; } - public function set_readonly(): Field { + final public function set_readonly(): Field { $this->attributes['readonly'] = true; return $this; } - public function is_readonly(): bool { - return $this->attributes['readonly'] ?? false; + final public function is_readonly(): bool { + return $this->attributes['readonly']; } - public function set_required(): Field { - $this->meta['required'] = true; + final public function set_required(): Field { + $this->attributes['required'] = true; return $this; } - public function add_class( string $class_name ): Field { - $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; } - public function unset_class( string $class_name ): Field { - unset( $this->meta['class'][ $class_name ] ); + final public function unset_class( string $class_name ): Field { + unset( $this->attributes['class'][ $class_name ] ); return $this; } - public function add_data( string $data_name, string $data_value ): Field { - 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; @@ -186,28 +187,26 @@ abstract class BasicField implements Field { 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 ] ); return $this; } - public function is_meta_value_set( string $name ): bool { - 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( string $name ): bool { - return isset( $this->meta['class'][ $name ] ); + final public function is_class_set( string $name ): bool { + return ! empty( $this->attributes['class'][ $name ] ); } - /** @return mixed */ - public function get_default_value() { - return $this->default_value; + final public function get_default_value(): string { + return $this->meta['default_value']; } - /** @param mixed $value */ - public function set_default_value( $value ): Field { - $this->default_value = $value; + final public function set_default_value( string $value ): Field { + $this->meta['default_value'] = $value; return $this; } @@ -221,16 +220,16 @@ abstract class BasicField implements Field { return $chain; } - public function is_required(): bool { - return $this->meta['required'] ?? false; + final public function is_required(): bool { + return $this->attributes['required']; } public function get_sanitizer(): Sanitizer { return new NoSanitize(); } - public function get_serializer(): Serializer { - if ( isset( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) { + final public function get_serializer(): Serializer { + if ( ! empty( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) { return $this->meta['serializer']; } @@ -243,7 +242,7 @@ abstract class BasicField implements Field { return $this; } - public function get_priority(): int { + final public function get_priority(): int { return $this->meta['priority']; } @@ -252,7 +251,7 @@ abstract class BasicField implements Field { * * @see FormWithFields::get_fields() */ - public function set_priority( int $priority ): Field { + final public function set_priority( int $priority ): Field { $this->meta['priority'] = $priority; return $this; diff --git a/src/Field/ButtonField.php b/src/Field/ButtonField.php index f0a4023..6d01a1f 100644 --- a/src/Field/ButtonField.php +++ b/src/Field/ButtonField.php @@ -3,10 +3,10 @@ namespace WPDesk\Forms\Field; class ButtonField extends NoValueField { - public function get_template_name(): string { return 'button'; } + public function get_type(): string { return 'button'; } diff --git a/src/Field/CheckboxField.php b/src/Field/CheckboxField.php index 507cda0..d55269e 100644 --- a/src/Field/CheckboxField.php +++ b/src/Field/CheckboxField.php @@ -8,9 +8,8 @@ class CheckboxField extends BasicField { const VALUE_TRUE = 'yes'; const VALUE_FALSE = 'no'; - public function __construct() { - parent::__construct(); - $this->set_attribute( 'type', 'checkbox' ); + public function get_type(): string { + return 'checkbox'; } public function get_template_name(): string { diff --git a/src/Field/DatePickerField.php b/src/Field/DatePickerField.php index 7060d90..7a69247 100644 --- a/src/Field/DatePickerField.php +++ b/src/Field/DatePickerField.php @@ -7,11 +7,8 @@ use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class DatePickerField extends BasicField { public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); $this->add_class( 'date-picker' ); $this->set_placeholder( 'YYYY-MM-DD' ); - $this->set_attribute( 'type', 'text' ); } public function get_sanitizer(): Sanitizer { diff --git a/src/Field/Header.php b/src/Field/Header.php index cebe249..a704f40 100644 --- a/src/Field/Header.php +++ b/src/Field/Header.php @@ -6,7 +6,6 @@ use WPDesk\Forms\Field; class Header extends NoValueField { public function __construct() { - parent::__construct(); $this->meta['header_size'] = ''; } diff --git a/src/Field/HiddenField.php b/src/Field/HiddenField.php index 9c3856c..e48742c 100644 --- a/src/Field/HiddenField.php +++ b/src/Field/HiddenField.php @@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class HiddenField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - $this->set_attribute( 'type', 'hidden' ); + public function get_type(): string { + return 'hidden'; } public function get_sanitizer(): Sanitizer { diff --git a/src/Field/ImageInputField.php b/src/Field/ImageInputField.php index 74c8410..1f58ec5 100644 --- a/src/Field/ImageInputField.php +++ b/src/Field/ImageInputField.php @@ -4,12 +4,6 @@ namespace WPDesk\Forms\Field; class ImageInputField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - $this->set_attribute( 'type', 'text' ); - } - public function get_template_name(): string { return 'input-image'; } diff --git a/src/Field/InputEmailField.php b/src/Field/InputEmailField.php index ee18412..a54ab4a 100644 --- a/src/Field/InputEmailField.php +++ b/src/Field/InputEmailField.php @@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\EmailSanitizer; class InputEmailField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - $this->set_attribute( 'type', 'email' ); + public function get_type(): string { + return 'email'; } public function get_sanitizer(): Sanitizer { diff --git a/src/Field/InputNumberField.php b/src/Field/InputNumberField.php index 9ee7405..380da44 100644 --- a/src/Field/InputNumberField.php +++ b/src/Field/InputNumberField.php @@ -6,10 +6,8 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputNumberField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - $this->set_attribute( 'type', 'number' ); + public function get_type(): string { + return 'number'; } public function get_sanitizer(): Sanitizer { diff --git a/src/Field/InputTextField.php b/src/Field/InputTextField.php index 7ca6a96..24b0fa6 100644 --- a/src/Field/InputTextField.php +++ b/src/Field/InputTextField.php @@ -6,12 +6,6 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputTextField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - $this->set_attribute( 'type', 'text' ); - } - public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } diff --git a/src/Field/MultipleInputTextField.php b/src/Field/MultipleInputTextField.php index cf976fe..6648e2a 100644 --- a/src/Field/MultipleInputTextField.php +++ b/src/Field/MultipleInputTextField.php @@ -3,7 +3,6 @@ namespace WPDesk\Forms\Field; class MultipleInputTextField extends InputTextField { - public function get_template_name(): string { return 'input-text-multiple'; } diff --git a/src/Field/NoOnceField.php b/src/Field/NoOnceField.php index 6419a37..17ac6e0 100644 --- a/src/Field/NoOnceField.php +++ b/src/Field/NoOnceField.php @@ -7,8 +7,7 @@ use WPDesk\Forms\Validator\NonceValidator; class NoOnceField extends BasicField { - public function __construct( $action_name ) { - parent::__construct(); + public function __construct( string $action_name ) { $this->meta['action'] = $action_name; } diff --git a/src/Field/ProductSelect.php b/src/Field/ProductSelect.php index 8960304..2a10e46 100644 --- a/src/Field/ProductSelect.php +++ b/src/Field/ProductSelect.php @@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field; class ProductSelect extends SelectField { public function __construct() { - parent::__construct(); $this->set_multiple(); } diff --git a/src/Field/TextAreaField.php b/src/Field/TextAreaField.php index 0f83ffe..f22b2bb 100644 --- a/src/Field/TextAreaField.php +++ b/src/Field/TextAreaField.php @@ -3,10 +3,6 @@ namespace WPDesk\Forms\Field; class TextAreaField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - } public function get_template_name(): string { return 'textarea'; diff --git a/src/Field/Traits/HtmlAttributes.php b/src/Field/Traits/HtmlAttributes.php index 5ae190a..71eccba 100644 --- a/src/Field/Traits/HtmlAttributes.php +++ b/src/Field/Traits/HtmlAttributes.php @@ -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 ?? ''; } } diff --git a/src/Field/WooSelect.php b/src/Field/WooSelect.php index bc59f9c..ec2719c 100644 --- a/src/Field/WooSelect.php +++ b/src/Field/WooSelect.php @@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field; class WooSelect extends SelectField { public function __construct() { - parent::__construct(); $this->set_multiple(); $this->add_class( 'wc-enhanced-select' ); } diff --git a/src/Field/WyswigField.php b/src/Field/WyswigField.php index 06db980..a2f4168 100644 --- a/src/Field/WyswigField.php +++ b/src/Field/WyswigField.php @@ -3,11 +3,6 @@ namespace WPDesk\Forms\Field; class WyswigField extends BasicField { - public function __construct() { - parent::__construct(); - $this->set_default_value( '' ); - } - public function get_template_name(): string { return 'wyswig'; } diff --git a/src/FieldsDataReceiver.php b/src/FieldsDataReceiver.php index 86a1583..e41743d 100644 --- a/src/FieldsDataReceiver.php +++ b/src/FieldsDataReceiver.php @@ -2,8 +2,6 @@ namespace WPDesk\Forms; -use Psr\Container\ContainerInterface; - /** * Some field owners can receive and process field data. * Probably should be used with FieldProvider interface. @@ -16,5 +14,5 @@ interface FieldsDataReceiver { * * @return void */ - public function update_fields_data( ContainerInterface $data ); + public function update_fields_data( \Psr\Container\ContainerInterface $data ); } diff --git a/src/Form.php b/src/Form.php index 504ebbe..5f8725d 100644 --- a/src/Form.php +++ b/src/Form.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms; +use WPDesk\Persistence\PersistentContainer; use WPDesk\View\Renderer\Renderer; /** @@ -35,15 +36,6 @@ interface Form { */ public function handle_request( array $request = [] ); - /** - * Data could be saved in some place. Use this method to transmit them to form. - * - * @param array $data Data for form. - * - * @return void - */ - public function set_data( $data ); - /** * Use to render the form to string. * @@ -54,7 +46,7 @@ interface Form { /** * Get data from form. Use after handle_request or set_data. * - * @return array<int,string> + * @return array<int|string> */ public function get_data(): array; diff --git a/src/Form/FormWithFields.php b/src/Form/FormWithFields.php index ba13928..ac0f159 100644 --- a/src/Form/FormWithFields.php +++ b/src/Form/FormWithFields.php @@ -7,7 +7,6 @@ use WPDesk\Forms\ContainerForm; use WPDesk\Forms\Field; use WPDesk\Forms\FieldProvider; use WPDesk\Forms\Form; -use WPDesk\Persistence\Adapter\ArrayContainer; use WPDesk\Persistence\ElementNotExistsException; use WPDesk\Persistence\PersistentContainer; use WPDesk\View\Renderer\Renderer; @@ -50,11 +49,11 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { } public function get_method(): string { - return $this->attributes['method'] ?? 'POST'; + return $this->attributes['method']; } public function get_action(): string { - return $this->attributes['action'] ?? ''; + return $this->attributes['action']; } public function is_submitted(): bool { @@ -113,14 +112,9 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { /** * Data could be saved in some place. Use this method to transmit them to form. * - * @param array|ContainerInterface $data Data consistent with Form or ContainerForm interface. - * * @return void */ - public function set_data( $data ) { - if ( is_array( $data ) ) { - $data = new ArrayContainer( $data ); - } + public function set_data( ContainerInterface $data ) { foreach ( $this->fields as $field ) { $data_key = $field->get_name(); if ( $data->has( $data_key ) ) { diff --git a/src/Sanitizer.php b/src/Sanitizer.php index bc8f207..3f57eac 100644 --- a/src/Sanitizer.php +++ b/src/Sanitizer.php @@ -3,6 +3,10 @@ namespace WPDesk\Forms; interface Sanitizer { - /** @param mixed $value */ - public function sanitize( $value ): string; + /** + * @param mixed $value + * + * @return mixed + */ + public function sanitize( $value ); } diff --git a/src/Sanitizer/NoSanitize.php b/src/Sanitizer/NoSanitize.php index 21bce01..a3fe4a6 100644 --- a/src/Sanitizer/NoSanitize.php +++ b/src/Sanitizer/NoSanitize.php @@ -5,7 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class NoSanitize implements Sanitizer { - public function sanitize( $value ): string { + public function sanitize( $value ) { return $value; } diff --git a/src/Serializer.php b/src/Serializer.php index 8302325..2096b0f 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -5,15 +5,11 @@ namespace WPDesk\Forms; interface Serializer { /** * @param mixed $value - * - * @return mixed */ - public function serialize( $value ); + public function serialize( $value ): string; /** - * @param mixed $value - * * @return mixed */ - public function unserialize( $value ); + public function unserialize( string $value ); } diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index bc8c0e0..9ded064 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -9,7 +9,7 @@ class JsonSerializer implements Serializer { return (string) json_encode( $value ); } - public function unserialize( $value ) { + public function unserialize( string $value ) { return json_decode( $value, true ); } } diff --git a/src/Serializer/NoSerialize.php b/src/Serializer/NoSerialize.php index b29e88b..371e8e7 100644 --- a/src/Serializer/NoSerialize.php +++ b/src/Serializer/NoSerialize.php @@ -5,11 +5,11 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class NoSerialize implements Serializer { - public function serialize( $value ) { - return $value; + public function serialize( $value ): string { + return (string) $value; } - public function unserialize( $value ) { + public function unserialize( string $value ) { return $value; } diff --git a/src/Serializer/ProductSelectSerializer.php b/src/Serializer/ProductSelectSerializer.php index e801d4d..7c4f0e8 100644 --- a/src/Serializer/ProductSelectSerializer.php +++ b/src/Serializer/ProductSelectSerializer.php @@ -10,7 +10,7 @@ use WPDesk\Forms\Serializer; * @package WPDesk\Forms\Serializer */ class ProductSelectSerializer implements Serializer { - public function serialize( $value ) { + public function serialize( $value ): string { $products_with_names = []; if ( is_array( $value ) ) { foreach ( $value as $product_id ) { @@ -22,10 +22,10 @@ class ProductSelectSerializer implements Serializer { } } - return $products_with_names; + return implode( ' ', $products_with_names ); } - public function unserialize( $value ) { + public function unserialize( string $value ) { return $value; } diff --git a/src/Serializer/SerializeSerializer.php b/src/Serializer/SerializeSerializer.php index 79d13f6..dde47cc 100644 --- a/src/Serializer/SerializeSerializer.php +++ b/src/Serializer/SerializeSerializer.php @@ -5,11 +5,11 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class SerializeSerializer implements Serializer { - public function serialize( $value ) { + public function serialize( $value ): string { return serialize( $value ); } - public function unserialize( $value ) { + public function unserialize( string $value ) { return unserialize( $value ); } diff --git a/src/Validator/NonceValidator.php b/src/Validator/NonceValidator.php index 8709af6..b887428 100644 --- a/src/Validator/NonceValidator.php +++ b/src/Validator/NonceValidator.php @@ -6,14 +6,16 @@ use WPDesk\Forms\Validator; class NonceValidator implements Validator { + /** @var string */ private $action; + /** @param string $action */ public function __construct( $action ) { $this->action = $action; } public function is_valid( $value ): bool { - return wp_verify_nonce( $value, $this->action ); + return (bool) wp_verify_nonce( $value, $this->action ); } public function get_messages(): array { -- GitLab