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

Merge branch 'feature/strong-typing-pp' into 'feature/strong-typing'

Feature/strong typing pp

See merge request !23
parents 679dcfd9 41941369
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 #6505 passed
Showing
with 49 additions and 42 deletions
......@@ -6,6 +6,7 @@ use WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer\JsonSerializer;
class TimepickerField extends BasicField {
public function has_serializer(): bool {
return true;
}
......
......@@ -2,6 +2,9 @@
namespace WPDesk\Forms\Field\Traits;
use WPDesk\Forms\Field;
use WPDesk\Forms\Form;
/**
* Implementation of HTML attributes like id, name, action etc.
*
......@@ -9,19 +12,8 @@ namespace WPDesk\Forms\Field\Traits;
*/
trait HtmlAttributes {
/** @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,
];
/** @var array{placeholder: string, name: string, id: string, class: string[]} */
protected $attributes = [];
/**
* Get list of all attributes except given.
......@@ -30,7 +22,7 @@ trait HtmlAttributes {
*
* @return array<string[]|string|bool>
*/
final public function get_attributes( array $except = [ 'name' ] ): array {
final public function get_attributes( array $except = [ 'name', 'class' ] ): array {
return array_filter(
$this->attributes,
static function ( $key ) use ( $except ) {
......@@ -44,7 +36,7 @@ trait HtmlAttributes {
* @param string $name
* @param string[]|string|bool $value
*
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
* @return Field|Form
*/
final public function set_attribute( string $name, $value ) {
$this->attributes[ $name ] = $value;
......@@ -53,7 +45,7 @@ trait HtmlAttributes {
}
/**
* @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
* @return HtmlAttributes
*/
final public function unset_attribute( string $name ) {
unset( $this->attributes[ $name ] );
......@@ -70,6 +62,7 @@ trait HtmlAttributes {
// 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 ?? '';
}
}
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms\Field;
class WooSelect extends SelectField {
public function __construct() {
$this->set_multiple();
$this->add_class( 'wc-enhanced-select' );
......
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms\Field;
class WyswigField extends BasicField {
public function get_template_name(): string {
return 'wyswig';
}
......
......@@ -6,6 +6,7 @@ namespace WPDesk\Forms;
* FieldProvider is owner of FormFields. These fields can be used to render forms and process values.
*/
interface FieldProvider {
/**
* Returns owned fields.
*
......
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms;
interface FieldRenderer {
/** @return string|array String or normalized array */
public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' );
}
......@@ -2,6 +2,8 @@
namespace WPDesk\Forms;
use Psr\Container\ContainerInterface;
/**
* Some field owners can receive and process field data.
* Probably should be used with FieldProvider interface.
......@@ -9,10 +11,11 @@ namespace WPDesk\Forms;
* @package WPDesk\Forms
*/
interface FieldsDataReceiver {
/**
* Set values corresponding to fields.
*
* @return void
*/
public function update_fields_data( \Psr\Container\ContainerInterface $data );
public function update_fields_data( ContainerInterface $data );
}
......@@ -10,6 +10,7 @@ use WPDesk\View\Renderer\Renderer;
* @package WPDesk\Forms
*/
interface Form {
/**
* For some reason you may want to disable a form. Returns false when disabled.
*/
......
......@@ -32,6 +32,8 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
public function __construct( array $fields, string $form_id = 'form' ) {
$this->fields = $fields;
$this->form_id = $form_id;
$this->set_action( '' );
$this->set_method( 'POST' );
}
/** Set Form action attribute. */
......@@ -41,6 +43,10 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this;
}
public function get_action(): string {
return $this->attributes['action'];
}
/** Set Form method attribute ie. GET/POST. */
public function set_method( string $method ): self {
$this->attributes['method'] = $method;
......@@ -52,9 +58,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this->attributes['method'];
}
public function get_action(): string {
return $this->attributes['action'];
}
public function is_submitted(): bool {
return null !== $this->updated_data;
......
......@@ -21,20 +21,15 @@ class JsonNormalizedRenderer implements FieldRenderer {
public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array {
$rendered_fields = [];
foreach ( $provider->get_fields() as $field ) {
$rendered = [
'name' => $field->get_name(),
'template' => $field->get_template_name(),
'multiple' => $field->is_multiple(),
'disabled' => $field->is_disabled(),
'readonly' => $field->is_readonly(),
'required' => $field->is_required(),
'prefix' => $name_prefix,
'value ' => $fields_data[ $field->get_name() ] ?? $field->get_default_value(),
];
$rendered = [];
foreach ( $field->get_attributes() as $key => $attribute ) {
$rendered[ $key ] = $attribute;
}
$rendered['name'] = $field->get_name();
$rendered['template'] = $field->get_template_name();
$rendered['prefix'] = $name_prefix;
$rendered['value'] = $fields_data[ $field->get_name() ] ?? $field->get_default_value();
if ( $field->has_classes() ) {
$rendered['class'] = $field->get_classes();
}
if ( $field->has_description() ) {
$rendered['description'] = $field->get_description();
}
......@@ -44,9 +39,6 @@ class JsonNormalizedRenderer implements FieldRenderer {
if ( $field->has_label() ) {
$rendered['label'] = $field->get_label();
}
if ( $field->has_placeholder() ) {
$rendered['placeholder'] = $field->get_placeholder();
}
$options = $field->get_possible_values();
if ( $options ) {
$rendered['options'] = $options;
......
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms;
interface Sanitizer {
/**
* @param mixed $value
*
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class EmailSanitizer implements Sanitizer {
public function sanitize( $value ): string {
return sanitize_email( $value );
}
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class NoSanitize implements Sanitizer {
public function sanitize( $value ) {
return $value;
}
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class TextFieldSanitizer implements Sanitizer {
public function sanitize( $value ): string {
return sanitize_text_field( $value );
}
......
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms;
interface Serializer {
/**
* @param mixed $value
*/
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer;
class JsonSerializer implements Serializer {
public function serialize( $value ): string {
return (string) json_encode( $value );
}
......
......@@ -10,6 +10,7 @@ use WPDesk\Forms\Serializer;
* @package WPDesk\Forms\Serializer
*/
class ProductSelectSerializer implements Serializer {
public function serialize( $value ): string {
$products_with_names = [];
if ( is_array( $value ) ) {
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer;
class SerializeSerializer implements Serializer {
public function serialize( $value ): string {
return serialize( $value );
}
......
......@@ -3,6 +3,7 @@
namespace WPDesk\Forms;
interface Validator {
/** @param mixed $value */
public function is_valid( $value ): bool;
......
......@@ -5,6 +5,7 @@ namespace WPDesk\Forms\Validator;
use WPDesk\Forms\Validator;
class NoValidateValidator implements Validator {
public function is_valid( $value ): bool {
return true;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment