diff --git a/src/ContainerForm.php b/src/ContainerForm.php index 9c7e339bef86aff91346c8a3dfe9321e384ee481..bd5fc827d43f3df517581eaf6e5b404a686e3687 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 42ea1292182a1ee39441d72fcf806f10b38a5d28..311d8eb2474614314d16e3d948fad8e74e86b427 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 94449338e8e09fd932a81c2f394e3df46d50da44..2855357bf077cc810390d3246ed5fb02bf37220d 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 f0a40238eef5c05f978f31ec899a005903785dac..6d01a1f5f2302cc70fb347e22c54120b58c9ad26 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 507cda08a68bf3e82c32e4c8ceda19f5cd32a344..d55269e464a004ba77fb25bb3b3b62e77b7cea6d 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 7060d9097e1805654f84345133cd5c091622c2ba..7a692476c8768911417278fc4b435389065950dc 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 cebe249b04a16dbbaa2b84a43e07d1020aab8749..a704f4006f50cca89415b439239ee53e722df362 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 9c3856ce13f415c8f0f5adffb728714373da98c3..e48742c8e01e03109a003b2b55797b3966064e48 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 74c84101b7ff0c8994ab19e4caa203496377d74b..1f58ec50e39ba2b49c5e21500452786ea09a96f5 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 ee184120c6a30bceea8e763d81692701e723245f..a54ab4af3c8a36fec22aa793a953970826257ad5 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 9ee74059c414fa025f85495277c11b1421e03623..380da445bde005887fd21ad4854f6da884932a29 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 7ca6a96b43d66d75167ea382a560a37404bb8979..24b0fa6174307da073a4fc6e5ec1e487acfcfdb0 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 cf976fe79f6f1309984fd24886a4187bd76b5cf1..6648e2ab8647a546094bca6ccadc13700d3954a3 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 6419a3796a481839cdd0abf3c4f455ef3ebc346f..17ac6e05315d442399a6f38148879f045309fb49 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 896030424bde4622d2ff35b29e9a9d5f2a198b74..2a10e467a6392525b113efa0bd42202be852755b 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 0f83ffe1dc6836ef947c1c0204a01ad0d03ff87b..f22b2bb28a58b06d2c4545b86507a182b013df61 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 5ae190a135b9316b03ae664121a697151fd7a095..71eccba6e2c40c78bf9560a79b093394b7806c50 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 bc59f9c29fa60251876665ebd60163ac7934129d..ec2719c54f8c72013de838310298c6c90f7271cd 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 06db980a61c1c85597c3f60f319dc912351431f4..a2f41684d3d4441a85fb73f4cbd77773cfc9a962 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 86a158301f7c43dbafe4de4ed876efd5c8f6c179..e41743dfc59ef0c9f78d2ad6f7fe454b74c8e13e 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 504ebbeafeb3159a8e033b36db00939dc9c3c2a7..5f8725d781e898d708d99a1c0fc4839291935417 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 ba13928a94370ce5fcd1eda32de5771e17def667..ac0f159bda5a0c721ecb9ecd8995b902ed91b73a 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 bc8f2070aca82873f5025230032554b485629eb0..3f57eac2413c691d3469cedc909df1ad241f838d 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 21bce01b271ef5333a77fd9c3d56aa7f831fa162..a3fe4a61160f130cae2821f2bf9faa8352d761c7 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 8302325f3414a1a570cc906656b9df54e3406aec..2096b0fe050aebbc6e3c4254a785f7f3b9e7eeb5 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 bc8c0e0baa328261c53eb0b0f4f57b8b4fb4bcc4..9ded0640ffb3cf8663b3c4edbd2eaba2e9b6b42f 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 b29e88b9e23420a2c67b5bd2f10136d8f509edfa..371e8e75db3dd44eda7e9a0db99f9355e709729c 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 e801d4d776b9aa8d5557378497252ac844adaccc..7c4f0e8b97b1500385422f4fccf7d5c82d488eee 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 79d13f6fecfc1d300a4f5a174190b6f4d0e49c0f..dde47cce57c3cb22b340f6c7e276ae57b2478dbb 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 8709af6125e7d31f44cd12d39bd7deac7f09b6b6..b8874289fde8342e868b9df682558bfe75ea3025 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 {