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

feat: hermetize BasicField methods

parent 47c8e032
No related branches found
No related tags found
3 merge requests!28release: 3.0.0,!23Feature/strong typing pp,!19Add strong typing for 3.0 version
Pipeline #6112 passed
Showing
with 123 additions and 140 deletions
......@@ -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.
......
......@@ -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;
......
......@@ -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;
......
......@@ -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';
}
......
......@@ -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 {
......
......@@ -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 {
......
......@@ -6,7 +6,6 @@ use WPDesk\Forms\Field;
class Header extends NoValueField {
public function __construct() {
parent::__construct();
$this->meta['header_size'] = '';
}
......
......@@ -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 {
......
......@@ -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';
}
......
......@@ -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 {
......
......@@ -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 {
......
......@@ -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();
}
......
......@@ -3,7 +3,6 @@
namespace WPDesk\Forms\Field;
class MultipleInputTextField extends InputTextField {
public function get_template_name(): string {
return 'input-text-multiple';
}
......
......@@ -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;
}
......
......@@ -4,7 +4,6 @@ namespace WPDesk\Forms\Field;
class ProductSelect extends SelectField {
public function __construct() {
parent::__construct();
$this->set_multiple();
}
......
......@@ -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';
......
......@@ -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 ?? '';
}
}
......@@ -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' );
}
......
......@@ -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';
}
......
......@@ -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 );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment