Skip to content
Snippets Groups Projects
Select Git revision
  • fb4e5c476782cb60cc0f78cfbf42ae76848cfd9b
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.2
  • 2.3.1
  • 2.3
25 results

HtmlAttributes.php

Blame
  • HtmlAttributes.php 1.07 KiB
    <?php
    
    namespace WPDesk\Forms\Field\Traits;
    
    /**
     * Implementation of HTML attributes like id, name, action etc.
     *
     * @package WPDesk\Forms\Field\Traits
     */
    trait HtmlAttributes {
    
    	/** @var string[] */
    	protected $attributes;
    
    	/**
    	 * Get list of all attributes except given.
    	 *
    	 * @param string[] $except
    	 *
    	 * @return string[]
    	 */
    	public function get_attributes( array $except = [ 'name', 'type' ] ): array {
    		return array_filter(
    			$this->attributes,
    			static function ( $value, $key ) use ( $except ) {
    				return ! in_array( $key, $except, true );
    			},
    			ARRAY_FILTER_USE_BOTH
    		);
    	}
    
    	public function set_attribute( string $name, string $value ): self {
    		$this->attributes[ $name ] = $value;
    
    		return $this;
    	}
    
    	public function unset_attribute( string $name ): self {
    		unset( $this->attributes[ $name ] );
    
    		return $this;
    	}
    
    	public function is_attribute_set( string $name ): bool {
    		return isset( $this->attributes[ $name ] );
    	}
    
    	public function get_attribute( string $name, string $default = null ): string {
    		return $this->attributes[ $name ] ?? $default;
    	}
    }