Skip to content
Snippets Groups Projects
Select Git revision
  • 8673ae272e363dfecd6697d7bc2b8832a5786a96
  • master default protected
  • bugfix/wordpress-review
  • bugfix/prevent-error-notice
  • remove-arrow
  • feature/update-message
  • feature/minimum-plugin-version-check-demo1
  • feature/plugin-name
  • 3.7.1
  • 3.7.0
  • 3.6.3
  • 3.6.2
  • 3.6.1
  • 3.6.0
  • 3.6.0-beta3
  • 3.6.0-beta2
  • 3.6.0-beta1
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.0
  • 3.3.0
  • 3.2.8
  • 3.2.7
  • 3.2.6
  • 3.2.5
  • 3.2.4
  • 3.2.3
28 results

Basic_Requirement_Checker_Factory.php

Blame
  • HtmlAttributes.php 1.89 KiB
    <?php
    
    namespace WPDesk\Forms\Field\Traits;
    
    /**
     * Implementation of HTML attributes like id, name, action etc.
     *
     * @package 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,
    	];
    
    	/**
    	 * Get list of all attributes except given.
    	 *
    	 * @param string[] $except
    	 *
    	 * @return array<string[]|string|bool>
    	 */
    	final public function get_attributes( array $except = [ 'name' ] ): 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[]|string|bool $value
    	 *
    	 * @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
    	 */
    	final public function set_attribute( string $name, $value ) {
    		$this->attributes[ $name ] = $value;
    
    		return $this;
    	}
    
    	/**
    	 * @return \WPDesk\Forms\Field|\WPDesk\Forms\Form
    	 */
    	final public function unset_attribute( string $name ) {
    		unset( $this->attributes[ $name ] );
    
    		return $this;
    	}
    
    	final public function is_attribute_set( string $name ): bool {
    		return ! empty( $this->attributes[ $name ] );
    	}
    
    	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 ?? '';
    	}
    }