<?php

namespace wpsolr\core\classes\engines\configuration\builder\solr\tokenizer_filter;

class WPSOLR_Configuration_Builder_Solr_SnowballPorterFilterFactory extends WPSOLR_Configuration_Builder_Solr_Tokenizer_Filter_Abstract {

	/**
	 * Parameters name
	 */
	const PARAMETER_LANGUAGE = 'format';
	const PARAMETER_PROTECTED = 'protected';

	/**
	 * Parameters value
	 */
	const PARAMETER_FORMAT_LANGUAGE_FRENCH = 'French';
	const PARAMETER_FORMAT_LANGUAGE_ARMENIAN = 'Armenian';
	const PARAMETER_FORMAT_LANGUAGE_BASQUE = 'Basque';
	const PARAMETER_FORMAT_LANGUAGE_CATALAN = 'Catalan';
	const PARAMETER_FORMAT_LANGUAGE_DANISH = 'Danish';
	const PARAMETER_FORMAT_LANGUAGE_DUTCH = 'Dutch';
	const PARAMETER_FORMAT_LANGUAGE_ENGLISH = 'English';
	const PARAMETER_FORMAT_LANGUAGE_FINNISH = 'Finnish';
	const PARAMETER_FORMAT_LANGUAGE_GERMAN = 'German';
	const PARAMETER_FORMAT_LANGUAGE_HUNGARIAN = 'Hungarian';
	const PARAMETER_FORMAT_LANGUAGE_ITALIAN = 'Italian';
	const PARAMETER_FORMAT_LANGUAGE_NORWEGIAN = 'Norwegian';
	const PARAMETER_FORMAT_LANGUAGE_PORTUGUESE = 'Portuguese';
	const PARAMETER_FORMAT_LANGUAGE_ROMANIAN = 'Romanian';
	const PARAMETER_FORMAT_LANGUAGE_RUSSIAN = 'Russian';
	const PARAMETER_FORMAT_LANGUAGE_SPANISH = 'Spanish';
	const PARAMETER_FORMAT_LANGUAGE_SWEDISH = 'Swedish';
	const PARAMETER_FORMAT_LANGUAGE_TURKISH = 'Turkish';


	const PARAMETER_FORMAT_LANGUAGES = [
		self::PARAMETER_FORMAT_LANGUAGE_FRENCH,
		self::PARAMETER_FORMAT_LANGUAGE_ARMENIAN,
		self::PARAMETER_FORMAT_LANGUAGE_BASQUE,
		self::PARAMETER_FORMAT_LANGUAGE_CATALAN,
		self::PARAMETER_FORMAT_LANGUAGE_DANISH,
		self::PARAMETER_FORMAT_LANGUAGE_DUTCH,
		self::PARAMETER_FORMAT_LANGUAGE_ENGLISH,
		self::PARAMETER_FORMAT_LANGUAGE_FINNISH,
		self::PARAMETER_FORMAT_LANGUAGE_GERMAN,
		self::PARAMETER_FORMAT_LANGUAGE_HUNGARIAN,
		self::PARAMETER_FORMAT_LANGUAGE_ITALIAN,
		self::PARAMETER_FORMAT_LANGUAGE_NORWEGIAN,
		self::PARAMETER_FORMAT_LANGUAGE_PORTUGUESE,
		self::PARAMETER_FORMAT_LANGUAGE_ROMANIAN,
		self::PARAMETER_FORMAT_LANGUAGE_RUSSIAN,
		self::PARAMETER_FORMAT_LANGUAGE_SPANISH,
		self::PARAMETER_FORMAT_LANGUAGE_SWEDISH,
		self::PARAMETER_FORMAT_LANGUAGE_TURKISH,
	];

	const DESCRIPTION_PARAM_LANGUAGE = '(default "English") The name of a language, used to select the appropriate Porter stemmer to use. 
Case is significant. This string is used to select a package name in the org.tartarus.snowball.ext class hierarchy.';

	const DESCRIPTION_PARAM_PROTECTED = 'Path of a text file containing a list of protected words, one per line. Protected words will not be stemmed. 
Blank lines and lines that begin with "#" are ignored. This may be an absolute path, or a simple file name in the Solr conf directory.';

	/**
	 * @inheritdoc
	 */
	static function get_factory_class_name() {
		return 'solr.SnowballPorterFilterFactory';
	}

	/**
	 * @inheritdoc
	 */
	public function get_documentation_link() {
		return 'https://lucene.apache.org/solr/guide/6_6/filter-descriptions.html#FilterDescriptions-SnowballPorterStemmerFilter';
	}

	/**
	 * @inheritDoc
	 */
	public function get_description() {
		return 'This filter factory instantiates a language-specific stemmer generated by Snowball. Snowball is a software package that generates pattern-based word stemmers. This type of stemmer is not as accurate as a table-based stemmer, but is faster and less complex. Table-driven stemmers are labor intensive to create and maintain and so are typically commercial products.

Solr contains Snowball stemmers for Armenian, Basque, Catalan, Danish, Dutch, English, Finnish, French, German, Hungarian, Italian, Norwegian, Portuguese, Romanian, Russian, Spanish, Swedish and Turkish. For more information on Snowball, visit http://snowball.tartarus.org/.

StopFilterFactory, CommonGramsFilterFactory, and CommonGramsQueryFilterFactory can optionally read stopwords in Snowball format (specify format="snowball" in the configuration of those FilterFactories).';
	}


	/**
	 * @param string $language
	 *
	 * @return $this
	 * @throws \Exception
	 */
	public function set_parameter_language( $language ) {
		return $this->set_parameter_value( self::PARAMETER_LANGUAGE, $language );
	}


	/**
	 * @inheritdoc
	 */
	protected function get_inner_parameters() {

		// Generate all languages list values
		$languages = [
			[
				self::PARAMETER_LIST_ID    => '',
				self::PARAMETER_LIST_LABEL => self::PARAMETER_LIST_USE_DEFAULT
			]
		];
		foreach ( self::PARAMETER_FORMAT_LANGUAGES as $language ) {
			$languages[] = [
				self::PARAMETER_LIST_ID    => $language,
				self::PARAMETER_LIST_LABEL => $language
			];
		}

		$this->add_parameter_drop_down_list( self::PARAMETER_LANGUAGE, '', $languages, false, self::DESCRIPTION_PARAM_LANGUAGE )
		     ->add_parameter_file( self::PARAMETER_PROTECTED, '', true, self::DESCRIPTION_PARAM_PROTECTED );
	}


}
