Skip to content
Snippets Groups Projects
Select Git revision
  • b537f22a56b5ee2134a255af82456b9aed6a706a
  • main default protected
  • devel
  • 1.0.0
4 results

Email.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;
    	}
    }