Skip to content
Snippets Groups Projects
Select Git revision
  • 811581efb484758956d4e388d8ef5a9e1f1cc2c7
  • main default protected
  • v0.10
  • 0.10.6
  • 0.10.5
  • 0.10.4
  • 0.10.3
  • 0.10.2
  • 0.10.1
  • 0.10.0
  • 0.9.1
  • 0.9.0
12 results

ArrayBindingLoader.php

Blame
  • ArrayBindingLoader.php 1.10 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 ) ) {
    				foreach ( $value as $unit ) {
    					yield $this->create( $unit, $key );
    				}
    			} else {
    				yield $this->create( $value, $key );
    			}
    		}
    	}
    
    	/**
         * @param mixed $value
    	 * @param int|string $hook
    	 */
    	private function create( $value, $hook ): Definition {
    		return $this->factory->create( $value, is_int( $hook ) ? null : $hook );
    	}
    }