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

feat: throw Exception, when accessing non-existing Serializer

parent dbac4097
No related branches found
No related tags found
2 merge requests!28release: 3.0.0,!19Add strong typing for 3.0 version
...@@ -50,11 +50,11 @@ interface SettingsTab { ...@@ -50,11 +50,11 @@ interface SettingsTab {
/** /**
* Use to set settings from database or defaults. * Use to set settings from database or defaults.
* *
* @param array|ContainerInterface $data Data to render. * @param ContainerInterface $data Data to render.
* *
* @return void * @return void
*/ */
public function set_data( $data ); public function set_data( ContainerInterface $data );
/** /**
* Use to handle request data from POST. * Use to handle request data from POST.
...@@ -102,11 +102,11 @@ abstract class FieldSettingsTab implements SettingsTab { ...@@ -102,11 +102,11 @@ abstract class FieldSettingsTab implements SettingsTab {
return $this->get_form()->render_form( $renderer ); return $this->get_form()->render_form( $renderer );
} }
public function set_data( $data ) { public function set_data( ContainerInterface $data ) {
$this->get_form()->set_data( $data ); $this->get_form()->set_data( $data );
} }
public function handle_request( $request ) { public function handle_request( array $request ) {
$this->get_form()->handle_request( $request ); $this->get_form()->handle_request( $request );
} }
......
# Changelog # Changelog
## [3.0.0]
### Added
- Added strong typing to all of the interfaces
- Added fields sorting by priority field
- Normalized string escaping in all template files
- Added InputEmailField and EmailSerializer classes
### Changed
- All getters and setter in BasicField are now declared final
- FormWithFields accepts only ContainerInterface in ::set_data() method. Prepare data before passing it
### Removed
- Removed deprecated classes
- Removed NoSerialize class
## [2.4.7] - 2021-09-20 ## [2.4.7] - 2021-09-20
### Fixed ### Fixed
- Add missing escaping functions in templates - Add missing escaping functions in templates
......
...@@ -91,5 +91,7 @@ interface Field { ...@@ -91,5 +91,7 @@ interface Field {
public function get_serializer(): Serializer; public function get_serializer(): Serializer;
public function has_serializer(): bool;
public function get_priority(): int; public function get_priority(): int;
} }
...@@ -21,7 +21,7 @@ abstract class BasicField implements Field { ...@@ -21,7 +21,7 @@ abstract class BasicField implements Field {
const DEFAULT_PRIORITY = 10; const DEFAULT_PRIORITY = 10;
/** @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} */ /** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */
protected $meta = [ protected $meta = [
'priority' => self::DEFAULT_PRIORITY, 'priority' => self::DEFAULT_PRIORITY,
'default_value' => '', 'default_value' => '',
...@@ -29,7 +29,6 @@ abstract class BasicField implements Field { ...@@ -29,7 +29,6 @@ abstract class BasicField implements Field {
'description' => '', 'description' => '',
'description_tip' => '', 'description_tip' => '',
'data' => [], 'data' => [],
'serializer' => null,
]; ];
public function should_override_form_template(): bool { public function should_override_form_template(): bool {
...@@ -53,8 +52,12 @@ abstract class BasicField implements Field { ...@@ -53,8 +52,12 @@ abstract class BasicField implements Field {
return new NoSanitize(); return new NoSanitize();
} }
public function has_serializer(): bool {
return false;
}
public function get_serializer(): Serializer { public function get_serializer(): Serializer {
return null; throw new \BadMethodCallException('You must define your serializer in a child class.');
} }
final public function get_name(): string { final public function get_name(): string {
...@@ -231,10 +234,6 @@ abstract class BasicField implements Field { ...@@ -231,10 +234,6 @@ abstract class BasicField implements Field {
return $this->attributes['required']; return $this->attributes['required'];
} }
final public function has_serializer(): bool {
return ! empty( $this->meta['serializer'] );
}
final public function get_priority(): int { final public function get_priority(): int {
return $this->meta['priority']; return $this->meta['priority'];
} }
......
...@@ -2,11 +2,22 @@ ...@@ -2,11 +2,22 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
use WPDesk\Forms\Serializer\ProductSelectSerializer;
use WPDesk\Forms\Serializer;
class ProductSelect extends SelectField { class ProductSelect extends SelectField {
public function __construct() { public function __construct() {
$this->set_multiple(); $this->set_multiple();
} }
public function has_serializer(): bool {
return true;
}
public function get_serializer(): Serializer {
return new ProductSelectSerializer();
}
public function get_template_name(): string { public function get_template_name(): string {
return 'product-select'; return 'product-select';
} }
......
...@@ -2,7 +2,18 @@ ...@@ -2,7 +2,18 @@
namespace WPDesk\Forms\Field; namespace WPDesk\Forms\Field;
use WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer\JsonSerializer;
class TimepickerField extends BasicField { class TimepickerField extends BasicField {
public function has_serializer(): bool {
return true;
}
public function get_serializer(): Serializer {
return new JsonSerializer();
}
public function get_template_name(): string { public function get_template_name(): string {
return 'timepicker'; return 'timepicker';
} }
......
...@@ -20,9 +20,7 @@ class FieldPersistenceStrategy { ...@@ -20,9 +20,7 @@ class FieldPersistenceStrategy {
$this->persistence = $persistence; $this->persistence = $persistence;
} }
/** /** @return void */
* Save fields data.
*/
public function persist_fields( FieldProvider $fields_provider, array $data ) { public function persist_fields( FieldProvider $fields_provider, array $data ) {
foreach ( $fields_provider->get_fields() as $field ) { foreach ( $fields_provider->get_fields() as $field ) {
$field_key = $field->get_name(); $field_key = $field->get_name();
...@@ -34,9 +32,7 @@ class FieldPersistenceStrategy { ...@@ -34,9 +32,7 @@ class FieldPersistenceStrategy {
} }
} }
/** /** @return void */
* Load fields data.
*/
public function load_fields( FieldProvider $fields_provider ): array { public function load_fields( FieldProvider $fields_provider ): array {
$data = []; $data = [];
foreach ( $fields_provider->get_fields() as $field ) { foreach ( $fields_provider->get_fields() as $field ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment