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

feat: throw Exception, when accessing non-existing Serializer

parent 10f426a4
No related branches found
No related tags found
No related merge requests found
Pipeline #6151 passed with stages
in 1 minute and 25 seconds
......@@ -50,11 +50,11 @@ interface SettingsTab {
/**
* Use to set settings from database or defaults.
*
* @param array|ContainerInterface $data Data to render.
* @param ContainerInterface $data Data to render.
*
* @return void
*/
public function set_data( $data );
public function set_data( ContainerInterface $data );
/**
* Use to handle request data from POST.
......@@ -102,11 +102,11 @@ abstract class FieldSettingsTab implements SettingsTab {
return $this->get_form()->render_form( $renderer );
}
public function set_data( $data ) {
public function set_data( ContainerInterface $data ) {
$this->get_form()->set_data( $data );
}
public function handle_request( $request ) {
public function handle_request( array $request ) {
$this->get_form()->handle_request( $request );
}
......
# Changelog
## [3.0.0] - 2021-09-08
## [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.6] - 2021-08-23
### Fixed
......
......@@ -91,5 +91,7 @@ interface Field {
public function get_serializer(): Serializer;
public function has_serializer(): bool;
public function get_priority(): int;
}
......@@ -21,7 +21,7 @@ abstract class BasicField implements Field {
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 = [
'priority' => self::DEFAULT_PRIORITY,
'default_value' => '',
......@@ -29,7 +29,6 @@ abstract class BasicField implements Field {
'description' => '',
'description_tip' => '',
'data' => [],
'serializer' => null,
];
public function should_override_form_template(): bool {
......@@ -53,8 +52,12 @@ abstract class BasicField implements Field {
return new NoSanitize();
}
public function has_serializer(): bool {
return false;
}
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 {
......@@ -231,10 +234,6 @@ abstract class BasicField implements Field {
return $this->attributes['required'];
}
final public function has_serializer(): bool {
return ! empty( $this->meta['serializer'] );
}
final public function get_priority(): int {
return $this->meta['priority'];
}
......
......@@ -2,11 +2,22 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Serializer\ProductSelectSerializer;
use WPDesk\Forms\Serializer;
class ProductSelect extends SelectField {
public function __construct() {
$this->set_multiple();
}
public function has_serializer(): bool {
return true;
}
public function get_serializer(): Serializer {
return new ProductSelectSerializer();
}
public function get_template_name(): string {
return 'product-select';
}
......
......@@ -2,7 +2,18 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer\JsonSerializer;
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 {
return 'timepicker';
}
......
......@@ -20,9 +20,7 @@ class FieldPersistenceStrategy {
$this->persistence = $persistence;
}
/**
* Save fields data.
*/
/** @return void */
public function persist_fields( FieldProvider $fields_provider, array $data ) {
foreach ( $fields_provider->get_fields() as $field ) {
$field_key = $field->get_name();
......@@ -34,9 +32,7 @@ class FieldPersistenceStrategy {
}
}
/**
* Load fields data.
*/
/** @return void */
public function load_fields( FieldProvider $fields_provider ): array {
$data = [];
foreach ( $fields_provider->get_fields() as $field ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment