<?php
/**
 * Tags Extractor class.
 *
 * @package AMVE\Classes\Tags_Extractor
 */

namespace Amve;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Tags Extractor class.
 */
final class Tags_Extractor {
	/**
	 * Tags list. The list of tags that will be used to extract tags from title.
	 *
	 * @var array
	 */
	private $tags_list;

	/**
	 * Tags extracted. The list of tags extracted from title.
	 *
	 * @var array
	 */
	private $extracted_tags;

	/**
	 * Extract tags in the given title.
	 *
	 * @param string $title The title to extract tags from.
	 *
	 * @return array The list of tags extracted from title.
	 */
	public function extract_in_title( $title ) {
		$memory_limit = ini_get( 'memory_limit' );
		ini_set( 'memory_limit', '256M' );

		$title = trim( strtolower( $title ) );

		$unwanted_chars  = array( ',', '!', '?', ';', ':', '.' );
		$title_words     = explode( ' ', strtolower( str_replace( $unwanted_chars, ' ', $title ) ) );
		$rephrased_title = implode( ' ', $title_words );

		require AMVE_DIR . 'admin/data/tags-x/tags.php';

		$extracted_tags = array();

		foreach ( (array) $data['tags'] as $tag ) {
			// if ( strpos( $rephrased_title, $tag ) !== false && count( explode( ' ', $tag ) ) >= 2 ) {
			if ( strpos( $rephrased_title, $tag ) ) {
				$extracted_tags[] = $tag;
			}
		}

		ini_set( 'memory_limit', $memory_limit );

		return $extracted_tags;
	}
}
