<?php
/**
 * This is a simple filter that removes config entries for plugins that are not installed.
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer\Utility\DeployerConfigRepository;

use Ealab\Deployer\Utility\Plugin;

/**
 * This is a simple filter that removes config entries for plugins that are not installed.
 *
 * It checks only the key of the config entry, not the value.
 * If you want to filter by version, use InstalledPluginsConfigFilter instead.
 */
class InstalledPluginKeyConfigFilter implements ConfigFilterInterface {
	/**
	 * {@inheritDoc}
	 */
	public static function filter( $config = null, ?array $options = null ) {
		if ( empty( $config ) || ! is_array( $config ) ) {
			return $config;
		}

		// The array keys are the plugin slugs.
		$installed_plugins = array_keys( Plugin::getInstalledPlugins( array( 'include_deployer' => true ) ) );

		// Add the "core" entry, represented by an empty string.
		$installed_plugins[] = '';

		// Filter out the configuration for plugins that are not installed.
		return array_intersect_key( $config, array_flip( $installed_plugins ) );
	}
}
