<commit_before><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace Essence\Provider;

use Essence\Exception;
use Essence\Media;
use Essence\Provider;
use Essence\Dom\Consumer as DomConsumer;
use Essence\Http\Consumer as HttpConsumer;
use Essence\Utility\Hash;
use Essence\Utility\Json;
use Essence\Utility\Xml;



/**
 *	Base class for an OEmbed provider.
 *	This kind of provider extracts embed informations through the OEmbed protocol.
 *
 *	@package fg.Essence.Provider
 */

class OEmbed extends Provider {

	use DomConsumer;
	use HttpConsumer;



	/**
	 *	JSON response format.
	 *
	 *	@var string
	 */

	const json = 'json';



	/**
	 *	XML response format.
	 *
	 *	@var string
	 */

	const xml = 'xml';



	/**
	 *	### Options
	 *
	 *	- 'endpoint' string The OEmbed endpoint.
	 *	- 'format' string The expected response format.
	 */

	protected $_options = array(
		'prepare' => 'OEmbed::prepare',
		'endpoint' => '',
		'format' => self::json
	);



	/**
	 *	Strips arguments and anchors from the given URL.
	 *
	 *	@param string $url Url to prepare.
	 *	@return string Prepared url.
	 */

	public static function prepare( $url ) {

		$url = trim( $url );

		if ( !self::strip( $url, '?' )) {
			self::strip( $url, '#' );
		}

		return $url;
	}



	/**
	 *	Strips the end of a string after a delimiter.
	 *
	 *	@param string $string The string to strip.
	 *	@param string $delimiter The delimiter from which to strip the string.
	 *	@return boolean True if the string was modified, otherwise false.
	 */

	public static function strip( &$string, $delimiter ) {

		$position = strrpos( $string, $delimiter );
		$found = ( $position !== false );

		if ( $found ) {
			$string = substr( $string, 0, $position );
		}

		return $found;
	}



	/**
	 *	{@inheritDoc}
	 *
	 *	@note If no endpoint was specified in the configuration, the page at
	 *		the given URL will be parsed to find one.
	 */

	protected function _embed( $url, $options ) {

		$Media = null;

		if ( empty( $this->_options['endpoint'])) {
			list( $endpoint, $format ) = $this->_extractEndpoint( $url );

			if ( $endpoint ) {
				$Media = $this->_embedEndpoint(
					$endpoint,
					$format,
					$options
				);
			}
		} else {
			$Media = $this->_embedEndpoint(
				sprintf( $this->_options['endpoint'], urlencode( $url )),
				$this->_options['format'],
				$options
			);
		}

		return $Media;
	}



	/**
	 *	Extracts an oEmbed endpoint from the given URL.
	 *
	 *	@param string $url URL from which to extract an endpoint.
	 *	@return array An array containing the endpoint URL and format.
	 */

	protected function _extractEndpoint( $url ) {

		$attributes = $this->_domParser( )->extractAttributes(
			$this->_httpClient( )->get( $url ),
			array(
				'link' => array(
					'rel' => '#alternate#i',
					'type',
					'href'
				)
			)
		);

		foreach ( $attributes['link'] as $link ) {
			if ( preg_match( '#(?<format>json|xml)#i', $link['type'], $matches )) {
				return array(
					$link['href'],
					$matches['format']
				);
			}
		}

		return array( '', '' );
	}



	/**
	 *	Fetches embed information from the given endpoint.
	 *
	 *	@param string $endpoint Endpoint to fetch informations from.
	 *	@param string $format Response format.
	 *	@return Media Embed informations.
	 */

	protected function _embedEndpoint( $endpoint, $format, $options ) {

		$response = $this->_httpClient( )->get(
			$this->_completeEndpoint( $endpoint, $options )
		);

		switch ( $format ) {
			case self::json:
				$data = Json::parse( $response );
				break;

			case self::xml:
				$data = Xml::parse( $response );
				break;

			default:
				throw new Exception( 'Unsupported format.' );
		}

		return new Media(
			Hash::reindex(
				$data,
				array(
					'author_name' => 'authorName',
					'author_url' => 'authorUrl',
					'provider_name' => 'providerName',
					'provider_url' => 'providerUrl',
					'cache_age' => 'cacheAge',
					'thumbnail_url' => 'thumbnailUrl',
					'thumbnail_width' => 'thumbnailWidth',
					'thumbnail_height' => 'thumbnailHeight',
				)
			)<commit_msg>Removed unnecessary coma<commit_after><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace Essence\Provider;

use Essence\Exception;
use Essence\Media;
use Essence\Provider;
use Essence\Dom\Consumer as DomConsumer;
use Essence\Http\Consumer as HttpConsumer;
use Essence\Utility\Hash;
use Essence\Utility\Json;
use Essence\Utility\Xml;



/**
 *	Base class for an OEmbed provider.
 *	This kind of provider extracts embed informations through the OEmbed protocol.
 *
 *	@package fg.Essence.Provider
 */

class OEmbed extends Provider {

	use DomConsumer;
	use HttpConsumer;



	/**
	 *	JSON response format.
	 *
	 *	@var string
	 */

	const json = 'json';



	/**
	 *	XML response format.
	 *
	 *	@var string
	 */

	const xml = 'xml';



	/**
	 *	### Options
	 *
	 *	- 'endpoint' string The OEmbed endpoint.
	 *	- 'format' string The expected response format.
	 */

	protected $_options = array(
		'prepare' => 'OEmbed::prepare',
		'endpoint' => '',
		'format' => self::json
	);



	/**
	 *	Strips arguments and anchors from the given URL.
	 *
	 *	@param string $url Url to prepare.
	 *	@return string Prepared url.
	 */

	public static function prepare( $url ) {

		$url = trim( $url );

		if ( !self::strip( $url, '?' )) {
			self::strip( $url, '#' );
		}

		return $url;
	}



	/**
	 *	Strips the end of a string after a delimiter.
	 *
	 *	@param string $string The string to strip.
	 *	@param string $delimiter The delimiter from which to strip the string.
	 *	@return boolean True if the string was modified, otherwise false.
	 */

	public static function strip( &$string, $delimiter ) {

		$position = strrpos( $string, $delimiter );
		$found = ( $position !== false );

		if ( $found ) {
			$string = substr( $string, 0, $position );
		}

		return $found;
	}



	/**
	 *	{@inheritDoc}
	 *
	 *	@note If no endpoint was specified in the configuration, the page at
	 *		the given URL will be parsed to find one.
	 */

	protected function _embed( $url, $options ) {

		$Media = null;

		if ( empty( $this->_options['endpoint'])) {
			list( $endpoint, $format ) = $this->_extractEndpoint( $url );

			if ( $endpoint ) {
				$Media = $this->_embedEndpoint(
					$endpoint,
					$format,
					$options
				);
			}
		} else {
			$Media = $this->_embedEndpoint(
				sprintf( $this->_options['endpoint'], urlencode( $url )),
				$this->_options['format'],
				$options
			);
		}

		return $Media;
	}



	/**
	 *	Extracts an oEmbed endpoint from the given URL.
	 *
	 *	@param string $url URL from which to extract an endpoint.
	 *	@return array An array containing the endpoint URL and format.
	 */

	protected function _extractEndpoint( $url ) {

		$attributes = $this->_domParser( )->extractAttributes(
			$this->_httpClient( )->get( $url ),
			array(
				'link' => array(
					'rel' => '#alternate#i',
					'type',
					'href'
				)
			)
		);

		foreach ( $attributes['link'] as $link ) {
			if ( preg_match( '#(?<format>json|xml)#i', $link['type'], $matches )) {
				return array(
					$link['href'],
					$matches['format']
				);
			}
		}

		return array( '', '' );
	}



	/**
	 *	Fetches embed information from the given endpoint.
	 *
	 *	@param string $endpoint Endpoint to fetch informations from.
	 *	@param string $format Response format.
	 *	@return Media Embed informations.
	 */

	protected function _embedEndpoint( $endpoint, $format, $options ) {

		$response = $this->_httpClient( )->get(
			$this->_completeEndpoint( $endpoint, $options )
		);

		switch ( $format ) {
			case self::json:
				$data = Json::parse( $response );
				break;

			case self::xml:
				$data = Xml::parse( $response );
				break;

			default:
				throw new Exception( 'Unsupported format.' );
		}

		return new Media(
			Hash::reindex(
				$data,
				array(
					'author_name' => 'authorName',
					'author_url' => 'authorUrl',
					'provider_name' => 'providerName',
					'provider_url' => 'providerUrl',
					'cache_age' => 'cacheAge',
					'thumbnail_url' => 'thumbnailUrl',
					'thumbnail_width' => 'thumbnailWidth',
					'thumbnail_height' => 'thumbnailHeight'
				)
			)
{'fraction_non_alphanumeric': 0.15251572327044025, 'fraction_numerical': 0.0004837929366231253, 'mean_word_length': 2.943252265140677, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6177971', 'n_tokens_mistral': 3215, 'n_tokens_neox': 2658, 'n_words': 758}