From 10f426a4f677fe22cbd5437137f29ccdd388d090 Mon Sep 17 00:00:00 2001
From: Bart Jaskulski <bartek.jaskulski@wpdesk.net>
Date: Thu, 16 Sep 2021 12:30:40 +0200
Subject: [PATCH] refactor: remove NoSerialize class, due to false serializing
 process

---
 src/ContainerForm.php                        |  1 -
 src/Field/BasicField.php                     | 69 +++++++++-----------
 src/Field/NoValueField.php                   |  4 +-
 src/Form.php                                 |  1 -
 src/Form/FormWithFields.php                  |  2 -
 src/Persistence/FieldPersistenceStrategy.php | 12 +++-
 src/Serializer/NoSerialize.php               | 16 -----
 7 files changed, 43 insertions(+), 62 deletions(-)
 delete mode 100644 src/Serializer/NoSerialize.php

diff --git a/src/ContainerForm.php b/src/ContainerForm.php
index bd5fc82..d79b57f 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 2855357..8e1a7e0 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 90bbecb..5309170 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 5f8725d..6fc5c0f 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 ac0f159..de48e33 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 02f2d4f..7a70cd5 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 371e8e7..0000000
--- 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;
-	}
-
-}
-- 
GitLab