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

feat: add strong typing to the package

parent 398bb2eb
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 #5964 passed
Showing
with 81 additions and 71 deletions
......@@ -18,7 +18,7 @@ class JsonNormalizedRenderer implements FieldRenderer {
*
* @return array Normalized fields with data.
*/
public function render_fields( FieldProvider $provider, array $fields_data, $name_prefix = '' ) {
public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array {
$rendered_fields = [];
foreach ( $provider->get_fields() as $field ) {
$rendered = [
......@@ -29,7 +29,7 @@ class JsonNormalizedRenderer implements FieldRenderer {
'readonly' => $field->is_readonly(),
'required' => $field->is_required(),
'prefix' => $name_prefix,
'value ' => isset( $fields_data[ $field->get_name() ] ) ? $fields_data[ $field->get_name() ] : $field->get_default_value()
'value ' => isset( $fields_data[ $field->get_name() ] ) ? $fields_data[ $field->get_name() ] : $field->get_default_value(),
];
if ( $field->has_classes() ) {
......
......@@ -12,6 +12,7 @@ use WPDesk\View\Resolver\Resolver;
* @package WPDesk\Forms\Resolver
*/
class DefaultFormFieldResolver implements Resolver {
/** @var Resolver */
private $dir_resolver;
......@@ -19,7 +20,7 @@ class DefaultFormFieldResolver implements Resolver {
$this->dir_resolver = new DirResolver( __DIR__ . '/../../templates' );
}
public function resolve( $name, Renderer $renderer = null ) {
public function resolve( $name, Renderer $renderer = null ): string {
return $this->dir_resolver->resolve( $name, $renderer );
}
......
......@@ -3,10 +3,6 @@
namespace WPDesk\Forms;
interface Sanitizer {
/**
* @param mixed $value
*
* @return string
*/
public function sanitize( $value );
/** @param mixed $value */
public function sanitize( $value ): string;
}
......@@ -5,13 +5,15 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class CallableSanitizer implements Sanitizer {
/** @var callable */
private $callable;
public function __construct( $callable ) {
public function __construct( callable $callable ) {
$this->callable = $callable;
}
public function sanitize( $value ) {
public function sanitize( $value ): string {
return call_user_func( $this->callable, $value );
}
......
......@@ -5,7 +5,7 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class NoSanitize implements Sanitizer {
public function sanitize( $value ) {
public function sanitize( $value ): string {
return $value;
}
......
......@@ -5,7 +5,7 @@ namespace WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer;
class TextFieldSanitizer implements Sanitizer {
public function sanitize( $value ) {
public function sanitize( $value ): string {
return sanitize_text_field( $value );
}
......
......@@ -3,7 +3,17 @@
namespace WPDesk\Forms;
interface Serializer {
/**
* @param mixed $value
*
* @return mixed
*/
public function serialize( $value );
/**
* @param mixed $value
*
* @return mixed
*/
public function unserialize( $value );
}
......@@ -5,8 +5,8 @@ namespace WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer;
class JsonSerializer implements Serializer {
public function serialize( $value ) {
return json_encode( $value );
public function serialize( $value ): string {
return (string) json_encode( $value );
}
public function unserialize( $value ) {
......
......@@ -5,7 +5,7 @@ namespace WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer;
class NoSerialize implements Serializer {
public function serialize( $value ) {
public function serialize( $value ): string {
return $value;
}
......
......@@ -10,9 +10,6 @@ use WPDesk\Forms\Serializer;
* @package WPDesk\Forms\Serializer
*/
class ProductSelectSerializer implements Serializer {
/**
* @param $value
*/
public function serialize( $value ) {
$products_with_names = [];
if ( is_array( $value ) ) {
......
......@@ -3,15 +3,9 @@
namespace WPDesk\Forms;
interface Validator {
/**
* @param mixed $value
*
* @return bool
*/
public function is_valid( $value );
/** @param mixed $value */
public function is_valid( $value ): bool;
/**
* @return string[]
*/
public function get_messages();
/** @return string[] */
public function get_messages(): array;
}
......@@ -5,9 +5,11 @@ namespace WPDesk\Forms\Validator;
use WPDesk\Forms\Validator;
class ChainValidator implements Validator {
/** @var Validator[] */
private $validators;
/** @var array */
private $messages;
public function __construct() {
......@@ -20,13 +22,13 @@ class ChainValidator implements Validator {
*
* @return $this
*/
public function attach( Validator $validator ) {
public function attach( Validator $validator ): self {
$this->validators[] = $validator;
return $this;
}
public function is_valid( $value ) {
public function is_valid( $value ): bool {
$result = true;
$messages = [ [] ];
foreach ( $this->validators as $validator ) {
......@@ -40,7 +42,7 @@ class ChainValidator implements Validator {
return $result;
}
public function get_messages() {
public function get_messages(): array {
return $this->messages;
}
......
......@@ -5,11 +5,11 @@ namespace WPDesk\Forms\Validator;
use WPDesk\Forms\Validator;
class NoValidateValidator implements Validator {
public function is_valid( $value ) {
public function is_valid( $value ): bool {
return true;
}
public function get_messages() {
public function get_messages(): array {
return [];
}
......
......@@ -5,19 +5,18 @@ namespace WPDesk\Forms\Validator;
use WPDesk\Forms\Validator;
class NonceValidator implements Validator {
private $action;
public function __construct( $action ) {
$this->action = $action;
}
public function is_valid( $value ) {
$valid = wp_verify_nonce( $value, $this->action );
return $valid;
public function is_valid( $value ): bool {
return wp_verify_nonce( $value, $this->action );
}
public function get_messages() {
public function get_messages(): array {
return [];
}
......
......@@ -5,11 +5,11 @@ namespace WPDesk\Forms\Validator;
use WPDesk\Forms\Validator;
class RequiredValidator implements Validator {
public function is_valid( $value ) {
public function is_valid( $value ): bool {
return $value !== null;
}
public function get_messages() {
public function get_messages(): array {
return [];
}
......
......@@ -6,8 +6,8 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<button
......@@ -20,12 +20,17 @@
<?php endforeach; ?>
type="<?php echo \esc_attr( $field->get_type() ); ?>"
name="<?php echo \esc_attr($name_prefix).'['.\esc_attr($field->get_name()).']'?>"
name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>"
id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $value ); ?>"
<?php if ($field->is_required()): ?>required="required"<?php endif; ?>
<?php if ($field->is_disabled()): ?>disabled="disabled"<?php endif; ?>
<?php if ($field->is_readonly()): ?>readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
><?php echo \esc_html( $field->get_label() ); ?></button>
......@@ -6,7 +6,6 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
......@@ -16,12 +15,17 @@
<?php endif; ?>
<td class="forminp">
<?php echo $renderer->render( $template_name, [
<?php
echo $renderer->render(
$template_name,
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
] ); ?>
]
);
?>
<?php if ( $field->has_description() ) : ?>
<p class="description"><?php echo wp_kses_post( $field->get_description() ); ?></p>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment