Skip to content
Snippets Groups Projects

Add strong typing for 3.0 version

Merged Krzysztof Dyszczyk requested to merge feature/strong-typing into devel
71 files
+ 822
1298
Compare changes
  • Side-by-side
  • Inline
Files
71
@@ -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,61 +12,57 @@ namespace WPDesk\Forms\Field\Traits;
*/
trait HtmlAttributes {
/** @var string[] */
protected $attributes;
/** @var array{placeholder: string, name: string, id: string, class: string[]} */
protected $attributes = [];
/**
* Get list of all attributes except given.
*
* @param string[] $except
*
* @return string[]
* @return array<string[]|string|bool>
*/
public function get_attributes( $except = [ 'name', 'type' ] ) {
return array_filter( $this->attributes, function ( $value, $key ) use ( $except ) {
return ! in_array( $key, $except, true );
}, ARRAY_FILTER_USE_BOTH );
final public function get_attributes( array $except = [ 'name', 'class' ] ): array {
return array_filter(
$this->attributes,
static function ( $key ) use ( $except ) {
return ! in_array( $key, $except, true );
},
ARRAY_FILTER_USE_KEY
);
}
/**
* @param string $name
* @param string $value
* @param string $name
* @param string[]|string|bool $value
*
* @return $this
* @return Field|Form
*/
public function set_attribute( $name, $value ) {
final public function set_attribute( string $name, $value ) {
$this->attributes[ $name ] = $value;
return $this;
}
/**
* @param string $name
*
* @return $this
* @return HtmlAttributes
*/
public function unset_attribute( $name ) {
final public function unset_attribute( string $name ) {
unset( $this->attributes[ $name ] );
return $this;
}
/**
* @param string $name
*
* @return bool
*/
public function is_attribute_set( $name ) {
return isset( $this->attributes[ $name ] );
final public function is_attribute_set( string $name ): bool {
return ! empty( $this->attributes[ $name ] );
}
/**
* @param string $name
* @param mixed $default
*
* @return string
*/
public function get_attribute( $name, $default = null ) {
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 ?? '' );
}
}
Loading