<?php
/**
 * Sitemaps: PSP_Sitemaps_Registry class
 *
 * Handles registering sitemaps.
 *
 * @package WordPress PlatinumSeo
 * @subpackage Sitemaps
 * @since 5.5.0
 */

/**
 * Class PSP_Sitemaps_Registry.
 *
 * @since 5.5.0
 */
class PSP_Sitemaps_Registry {
	/**
	 * Registered sitemaps.
	 *
	 * @since 5.5.0
	 *
	 * @var array Array of registered sitemaps.
	 */
	private $sitemaps = array();

	/**
	 * Adds a sitemap with route to the registry.
	 *
	 * @since 5.5.0
	 *
	 * @param string               $name     Name of the sitemap.
	 * @param PSP_Sitemaps_Provider $provider Instance of a PSP_Sitemaps_Provider.
	 * @return bool True if the sitemap was added, false if it is already registered.
	 */
	public function add_sitemap( $name, PSP_Sitemaps_Provider $provider ) {
		if ( isset( $this->sitemaps[ $name ] ) ) {
			return false;
		}

		$this->sitemaps[ $name ] = $provider;

		return true;
	}

	/**
	 * Returns a single registered sitemaps provider.
	 *
	 * @since 5.5.0
	 *
	 * @param string $name Sitemap provider name.
	 * @return PSP_Sitemaps_Provider|null Sitemaps provider if it exists, null otherwise.
	 */
	public function get_sitemap( $name ) {
		if ( ! isset( $this->sitemaps[ $name ] ) ) {
			return null;
		}

		return $this->sitemaps[ $name ];
	}

	/**
	 * Lists all registered sitemaps.
	 *
	 * @since 5.5.0
	 *
	 * @return array List of sitemaps.
	 */
	public function get_sitemaps() {
		$total_sitemaps = count( $this->sitemaps );

		if ( $total_sitemaps > WP_SITEMAPS_MAX_SITEMAPS ) {
			return array_slice( $this->sitemaps, 0, WP_SITEMAPS_MAX_SITEMAPS, true );
		}

		return $this->sitemaps;
	}
}
