<commit_before><?php
namespace Reeska\Semrush;

use Reeska\Semrush\Factory\ResultFactory;
use Reeska\Semrush\Factory\DomainOrganicResultFactory;
use Reeska\Semrush\Factory\DomainRanksResultFactory;
use Reeska\Semrush\Result\DomainRanksResult;
use Reeska\Semrush\Result\DomainOrganicResult;
use Reeska\Semrush\Factory\DomainRankHistoryResultFactory;

/**
 * Semrush Service class to request Semrush API.
 * Usage: 
 * $semrush = new Semrush('api_key');
 * $results = $semrush->organicSearchKeywords('domain.tld');
 * 
 * @author Reeska
 */
class SemrushAPI {
	protected static $endpoint = 'http://api.semrush.com';
	protected static $options = array(
		'domain_organic' => array(
			'domain' => '',
			'database' => 'fr',
			'display_limit' => 10000, 	/* default: 10000 */
			'display_offset' => 0,
			'export_escape' => 1, 		/* 0: no protect, 1: protect by " */
			'export_decode' => 1, 		/* 0: url encoded, 1: no encoding */
			'display_date' => '', 		/* format YYYYMM15 */
			'export_columns' => 'Ph,Po,Pp,Pd,Nq,Cp,Ur,Tr,Tc,Co,Nr,Td', /* Ph, Po, Pp, Pd, Nq, Cp, Ur, Tr, Tc, Co, Nr, Td */
			'display_sort' => '', 		/* sort by tr_asc, tr_desc, po_asc, po_desc, tc_asc, tc_desc */
			'display_positions' => '', 	/* new, lost, rise ou fall */
			'display_filter' => ''		/* <sign>|<field>|<operation>|<value> */
		),
		'domain_ranks' => array(
			'domain' => '',
			'database' => 'fr',
			'display_date' => '',
			'export_columns' => 'Db,Dn,Rk,Or,Ot,Oc,Ad,At,Ac',
			'export_escape' => 1 	/* 0: no protect, 1: protect by " */
		),
		'domain_rank_history' => array(
			'domain' => '',
			'database' => 'fr',
			'display_date' => '',
			'export_columns' => 'Rk,Or,Ot,Oc,Ad,At,Ac,Dt',
			'export_escape' => 1 	/* 0: no protect, 1: protect by " */
		)
	);
	
	protected $debug = false;
	private $key;
	
	public function __construct($key) {
		$this->key = $key;
	}
	
	/**
	 * Enable or disable showing debug information.
	 * @param boolean $debug
	 * @return SemrushAPI
	 */
	public function setDebug($debug) {
		$this->debug = $debug;
		return $this;
	}
	
	/**
	 * Change some default options.
	 * 
	 * @param string $type Service name.
	 * @param array $params Options to override.
	 */
	public static function configure($type, $params) {
		self::$options = array_merge(self::$options, $params);
	}

	/**
	 * Do an domain organic search keywords.
	 * @param string|array $params Domain or multiple params.
	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).
	 * @param int $maxlength Max result count (display_limit option).
	 * @return DomainOrganicResult[]
	 */
	public function organicSearchKeywords($params, $date = '', $maxlength = '') {
		return $this->dispatch('domain_organic', DomainOrganicResultFactory::instance(), $params, $date, $maxlength);
	}
	
	/**
	 * Do a domain ranks query.
	 * @param string|array $params Domain or multiple params.
 	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).
	 * @param int $maxlength Max result count (display_limit option).
	 * @return DomainRanksResult[]
	 */
	public function domainRanks($params, $date = '', $maxlength = '') {
		return $this->dispatch('domain_ranks', DomainRanksResultFactory::instance(), $params, $date, $maxlength);
	}
	
	/**
	 * Do a domain rank history query.
	 * @param string|array $params Domain or multiple params.
	 * @param int $maxlength Max result count (display_limit option).
	 * @param string $sort Sort order in dt_asc or dt_desc.
	 * @return DomainRankHistoryResult[]
	 */
	public function domainRankHistory($params, $maxlength = '', $sort = 'dt_asc') {
		if (!is_array($params)) {
			$params = array(
				'domain' => $params,
				'display_limit' => $maxlength,
				'display_sort' => $sort
			);
		}		
		
		return $this->dispatch('domain_rank_history', DomainRankHistoryResultFactory::instance(), $params, null, $maxlength);
	}	
	
	/**
	 * Dispatch the request.
	 * @param string Request type.
	 * @param ResultFactory $factory Factory to make result instance.
	 * @param string|array $params Domain or multiple params.
	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).<commit_msg>Add method to get API units<commit_after><?php
namespace Reeska\Semrush;

use Reeska\Semrush\Factory\ResultFactory;
use Reeska\Semrush\Factory\DomainOrganicResultFactory;
use Reeska\Semrush\Factory\DomainRanksResultFactory;
use Reeska\Semrush\Result\DomainRanksResult;
use Reeska\Semrush\Result\DomainOrganicResult;
use Reeska\Semrush\Factory\DomainRankHistoryResultFactory;

/**
 * Semrush Service class to request Semrush API.
 * Usage: 
 * $semrush = new Semrush('api_key');
 * $results = $semrush->organicSearchKeywords('domain.tld');
 * 
 * @author Reeska
 */
class SemrushAPI {
	protected static $endpoint = 'http://api.semrush.com';
	protected static $domain = 'http://www.semrush.com';
	protected static $options = array(
		'domain_organic' => array(
			'domain' => '',
			'database' => 'fr',
			'display_limit' => 10000, 	/* default: 10000 */
			'display_offset' => 0,
			'export_escape' => 1, 		/* 0: no protect, 1: protect by " */
			'export_decode' => 1, 		/* 0: url encoded, 1: no encoding */
			'display_date' => '', 		/* format YYYYMM15 */
			'export_columns' => 'Ph,Po,Pp,Pd,Nq,Cp,Ur,Tr,Tc,Co,Nr,Td', /* Ph, Po, Pp, Pd, Nq, Cp, Ur, Tr, Tc, Co, Nr, Td */
			'display_sort' => '', 		/* sort by tr_asc, tr_desc, po_asc, po_desc, tc_asc, tc_desc */
			'display_positions' => '', 	/* new, lost, rise ou fall */
			'display_filter' => ''		/* <sign>|<field>|<operation>|<value> */
		),
		'domain_ranks' => array(
			'domain' => '',
			'database' => 'fr',
			'display_date' => '',
			'export_columns' => 'Db,Dn,Rk,Or,Ot,Oc,Ad,At,Ac',
			'export_escape' => 1 	/* 0: no protect, 1: protect by " */
		),
		'domain_rank_history' => array(
			'domain' => '',
			'database' => 'fr',
			'display_date' => '',
			'export_columns' => 'Rk,Or,Ot,Oc,Ad,At,Ac,Dt',
			'export_escape' => 1 	/* 0: no protect, 1: protect by " */
		)
	);
	
	protected $debug = false;
	private $key;
	
	public function __construct($key) {
		$this->key = $key;
	}
	
	/**
	 * Enable or disable showing debug information.
	 * @param boolean $debug
	 * @return SemrushAPI
	 */
	public function setDebug($debug) {
		$this->debug = $debug;
		return $this;
	}
	
	/**
	 * Change some default options.
	 * 
	 * @param string $type Service name.
	 * @param array $params Options to override.
	 */
	public static function configure($type, $params) {
		self::$options = array_merge(self::$options, $params);
	}

	/**
	 * Do an domain organic search keywords.
	 * @param string|array $params Domain or multiple params.
	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).
	 * @param int $maxlength Max result count (display_limit option).
	 * @return DomainOrganicResult[]
	 */
	public function organicSearchKeywords($params, $date = '', $maxlength = '') {
		return $this->dispatch('domain_organic', DomainOrganicResultFactory::instance(), $params, $date, $maxlength);
	}
	
	/**
	 * Do a domain ranks query.
	 * @param string|array $params Domain or multiple params.
 	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).
	 * @param int $maxlength Max result count (display_limit option).
	 * @return DomainRanksResult[]
	 */
	public function domainRanks($params, $date = '', $maxlength = '') {
		return $this->dispatch('domain_ranks', DomainRanksResultFactory::instance(), $params, $date, $maxlength);
	}
	
	/**
	 * Do a domain rank history query.
	 * @param string|array $params Domain or multiple params.
	 * @param int $maxlength Max result count (display_limit option).
	 * @param string $sort Sort order in dt_asc or dt_desc.
	 * @return DomainRankHistoryResult[]
	 */
	public function domainRankHistory($params, $maxlength = '', $sort = 'dt_asc') {
		if (!is_array($params)) {
			$params = array(
				'domain' => $params,
				'display_limit' => $maxlength,
				'display_sort' => $sort
			);
		}		
		
		return $this->dispatch('domain_rank_history', DomainRankHistoryResultFactory::instance(), $params, null, $maxlength);
	}	
	
	/**
	 * Get current API units for this current account.
	 * If there is an error to get data, null is returned.
	 * @return int|NULL
	 */
	public function units() {
		$content = @file_get_contents(self::$domain.'/users/countapiunits.html?key='.$this->key);
		
		if (is_numeric($content)) {
			return $content;
		}
		
		return null;
	}
	
	/**
	 * Dispatch the request.
	 * @param string Request type.
	 * @param ResultFactory $factory Factory to make result instance.
	 * @param string|array $params Domain or multiple params.
	 * @param string $date Date to filter results, format: YYYYMM15 (display_date option).
{'fraction_non_alphanumeric': 0.15752171925011432, 'fraction_numerical': 0.007087334247828075, 'mean_word_length': 3.954133635334088, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4492341', 'n_tokens_mistral': 3247, 'n_tokens_neox': 2923, 'n_words': 760}