<?php
/**
 * Detection rule set.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Detection\Rules;

use LocalGuardScanner\Detection\Plugin\PluginAdvisory;

/**
 * Immutable versioned collection of rules.
 */
final class RuleSet {
	/** @var string */
	private $version;
	/** @var Rule[] */
	private $rules;
	/** @var array<string,string> */
	private $core_catalog;
	/** @var PluginAdvisory[] */
	private $advisories;
	/** @var string */
	private $advisory_date;

	/**
	 * @param string               $version      Rule version.
	 * @param Rule[]               $rules        Rules.
	 * @param array<string,string> $core_catalog Signed catalog hashes.
	 * @param PluginAdvisory[]     $advisories   Signed plugin advisories.
	 * @param string               $advisory_date Catalog compilation date.
	 */
	public function __construct( $version, array $rules, array $core_catalog = array(), array $advisories = array(), $advisory_date = '' ) {
		$this->version       = $version;
		$this->rules         = $rules;
		$this->core_catalog  = $core_catalog;
		$this->advisories    = $advisories;
		$this->advisory_date = $advisory_date;
	}

	/** @return string */
	public function version() {
		return $this->version;
	}

	/** @return Rule[] */
	public function all() {
		return $this->rules;
	}

	/** @return array<string,string> */
	public function core_catalog() {
		return $this->core_catalog;
	}

	/** @return PluginAdvisory[] */
	public function advisories() {
		return $this->advisories;
	}

	/** @return string */
	public function advisory_date() {
		return $this->advisory_date;
	}

	/**
	 * @param string $type Rule type.
	 * @return Rule[]
	 */
	public function of_type( $type ) {
		return array_values(
			array_filter(
				$this->rules,
				static function ( Rule $rule ) use ( $type ) {
					return $rule->type() === $type;
				}
			)
		);
	}
}
