diff --git a/src/ContainerForm.php b/src/ContainerForm.php index bd5fc827d43f3df517581eaf6e5b404a686e3687..d79b57f1f3513659926a2ba8bee926df3b67e047 100644 --- a/src/ContainerForm.php +++ b/src/ContainerForm.php @@ -4,7 +4,6 @@ namespace WPDesk\Forms; use Psr\Container\ContainerInterface; use WPDesk\Persistence\PersistentContainer; -use Psr\Container\ContainerInterface; /** * Persistent container support for forms. diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 2855357bf077cc810390d3246ed5fb02bf37220d..8e1a7e000938fad7154f3bc485e4dd6fa1e5f961 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -6,7 +6,6 @@ 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; @@ -33,6 +32,35 @@ abstract class BasicField implements Field { 'serializer' => null, ]; + public function should_override_form_template(): bool { + return false; + } + + public function get_type(): string { + return 'text'; + } + + public function get_validator(): Validator { + $chain = new ChainValidator(); + if ( $this->is_required() ) { + $chain->attach( new RequiredValidator() ); + } + + return $chain; + } + + public function get_sanitizer(): Sanitizer { + return new NoSanitize(); + } + + public function get_serializer(): Serializer { + return null; + } + + final public function get_name(): string { + return $this->attributes['name']; + } + final public function get_label(): string { return $this->meta['label']; } @@ -51,11 +79,6 @@ abstract class BasicField implements Field { return ! empty( $this->meta['description_tip'] ); } - /** Override method if you need. */ - public function should_override_form_template(): bool { - return false; - } - final public function get_description(): string { return $this->meta['description']; } @@ -80,10 +103,6 @@ abstract class BasicField implements Field { return $this; } - public function get_type(): string { - return 'text'; - } - final public function set_placeholder( string $value ): Field { $this->attributes['placeholder'] = $value; @@ -132,9 +151,6 @@ abstract class BasicField implements Field { return $this->attributes['id'] ?? sanitize_title( $this->get_name() ); } - public function get_name(): string { - return $this->attributes['name']; - } final public function is_multiple(): bool { return $this->attributes['multiple']; @@ -211,35 +227,12 @@ abstract class BasicField implements Field { return $this; } - public function get_validator(): Validator { - $chain = new ChainValidator(); - if ( $this->is_required() ) { - $chain->attach( new RequiredValidator() ); - } - - return $chain; - } - final public function is_required(): bool { return $this->attributes['required']; } - public function get_sanitizer(): Sanitizer { - return new NoSanitize(); - } - - final public function get_serializer(): Serializer { - if ( ! empty( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) { - return $this->meta['serializer']; - } - - return new NoSerialize(); - } - - public function set_serializer( Serializer $serializer ): Field { - $this->meta['serializer'] = $serializer; - - return $this; + final public function has_serializer(): bool { + return ! empty( $this->meta['serializer'] ); } final public function get_priority(): int { diff --git a/src/Field/NoValueField.php b/src/Field/NoValueField.php index 90bbecb1d4c622e128c3b95c8d7a934ea2e6598d..53091700da201919178d7b2317dc7e222df105e2 100644 --- a/src/Field/NoValueField.php +++ b/src/Field/NoValueField.php @@ -8,7 +8,7 @@ namespace WPDesk\Forms\Field; * @package WPDesk\Forms */ abstract class NoValueField extends BasicField { - public function get_name(): string { - return ''; + public function __construct() { + $this->set_name( '' ); } } diff --git a/src/Form.php b/src/Form.php index 5f8725d781e898d708d99a1c0fc4839291935417..6fc5c0f98f80701d4480d49e6773107323925c9e 100644 --- a/src/Form.php +++ b/src/Form.php @@ -2,7 +2,6 @@ namespace WPDesk\Forms; -use WPDesk\Persistence\PersistentContainer; use WPDesk\View\Renderer\Renderer; /** diff --git a/src/Form/FormWithFields.php b/src/Form/FormWithFields.php index ac0f159bda5a0c721ecb9ecd8995b902ed91b73a..de48e33d76b70c89a5f5aba5980b81e5f40260fc 100644 --- a/src/Form/FormWithFields.php +++ b/src/Form/FormWithFields.php @@ -94,8 +94,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { /** * Add array to update data. - * - * @param array|ContainerInterface $request new data to update. */ public function handle_request( array $request = [] ) { if ( $this->updated_data === null ) { diff --git a/src/Persistence/FieldPersistenceStrategy.php b/src/Persistence/FieldPersistenceStrategy.php index 02f2d4ffd3a051c7abd34ca5c01d179afa7ad907..7a70cd5e429e9b8e3e9545a9af85fe951fd4aabd 100644 --- a/src/Persistence/FieldPersistenceStrategy.php +++ b/src/Persistence/FieldPersistenceStrategy.php @@ -26,7 +26,11 @@ class FieldPersistenceStrategy { public function persist_fields( FieldProvider $fields_provider, array $data ) { foreach ( $fields_provider->get_fields() as $field ) { $field_key = $field->get_name(); - $this->persistence->set( $field_key, $field->get_serializer()->serialize( $data[ $field_key ] ) ); + if ( $field->has_serializer() ) { + $this->persistence->set( $field_key, $field->get_serializer()->serialize( $data[ $field_key ] ) ); + } else { + $this->persistence->set( $field_key, $data[ $field_key ] ); + } } } @@ -38,7 +42,11 @@ class FieldPersistenceStrategy { foreach ( $fields_provider->get_fields() as $field ) { $field_key = $field->get_name(); try { - $data[ $field_key ] = $field->get_serializer()->unserialize( $this->persistence->get( $field_key ) ); + if ( $field->has_serializer() ) { + $data[ $field_key ] = $field->get_serializer()->unserialize( $this->persistence->get( $field_key ) ); + } else { + $data[ $field_key ] = $this->persistence->get( $field_key ); + } } catch ( NotFoundExceptionInterface $not_found ) { // TODO: Logger // LoggerFactory::get_logger()->info( "FieldPersistenceStrategy:: Field {$field_key} not found" ); diff --git a/src/Serializer/NoSerialize.php b/src/Serializer/NoSerialize.php deleted file mode 100644 index 371e8e75db3dd44eda7e9a0db99f9355e709729c..0000000000000000000000000000000000000000 --- a/src/Serializer/NoSerialize.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace WPDesk\Forms\Serializer; - -use WPDesk\Forms\Serializer; - -class NoSerialize implements Serializer { - public function serialize( $value ): string { - return (string) $value; - } - - public function unserialize( string $value ) { - return $value; - } - -}