Skip to content
Snippets Groups Projects
Select Git revision
  • b514317c45c23b6d5d6bae26a682ce567f6646db
  • 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
  • ArrayDefinitions.php 1.45 KiB
    <?php
    declare( strict_types=1 );
    
    namespace WPDesk\Init\Binding\Loader;
    
    use WPDesk\Init\Binding\Definition;
    use WPDesk\Init\Binding\DefinitionFactory;
    use WPDesk\Init\Configuration\ReadableConfig;
    use WPDesk\Init\Plugin\Plugin;
    
    class ArrayDefinitions implements BindingDefinitions {
    
    	/** @var array */
    	private $bindings;
    
    	/** @var DefinitionFactory */
    	private $factory;
    
    	public function __construct( array $bindings, ?DefinitionFactory $factory = null ) {
    		$this->bindings = $bindings;
    		$this->factory  = $factory ?? new DefinitionFactory();
    	}
    
    	public function load(): iterable {
    		yield from $this->normalize( $this->bindings );
    	}
    
    	private function normalize( iterable $bindings ): iterable {
    		foreach ( $bindings as $key => $value ) {
    			if ( is_array( $value ) ) {
    				if ( isset( $value['handler'] ) ) {
    					// Single item with handler
    					yield $this->create( $value['handler'], $key, $value );
    				} else {
    					// Multiple items
    					foreach ( $value as $unit ) {
    						if ( is_array( $unit ) && isset( $unit['handler'] ) ) {
    							yield $this->create( $unit['handler'], $key, $unit );
    						} else {
    							yield $this->create( $unit, $key );
    						}
    					}
    				}
    			} else {
    				yield $this->create( $value, $key );
    			}
    		}
    	}
    
    	/**
    	 * @param mixed $value
    	 * @param int|string $hook
    	 */
    	private function create( $value, $hook, array $options = [] ): Definition {
    		return $this->factory->create( $value, is_int( $hook ) ? null : $hook, $options );
    	}
    }