<commit_before><?php

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

namespace fg\Essence\Dom;

use fg\Essence\Dom;
use fg\Essence\Exception;
use fg\Essence\Utility\Hash;



/**
 *	Handles HTML related operations.
 *
 *	@package fg.Essence.Dom
 */

class Native implements Dom {

	/**
	 *	{@inheritDoc}
	 */

	public function extractAttributes( $html, array $options ) {

		$reporting = error_reporting( 0 );
		$Document = \DomDocument::loadHTML( $html );
		error_reporting( $reporting );

		if ( $Document === false ) {
			throw new Exception( 'Unable to load HTML document.' );
		}

		$options = Hash::normalize( $options, array( ));
		$data = array( );

		foreach ( $options as $tag => $required ) {
			$tags = $Document->getElementsByTagName( $tag );
			$required = Hash::normalize( $required, '' );
			$data[ $tag ] = array( );

			foreach ( $tags as $Tag ) {
				if ( $Tag->hasAttributes( )) {
					$attributes = $this->_extractAttributesFromTag(
						$Tag,
						$required
					);

					if ( !empty( $attributes )) {
						$data[ $tag ][ ] = $attributes;
					}
				}
			}
		}

		return $data;
	}



	/**
	 *	Extracts attributes from the given tag.
	 *
	 *	@param \DOMNode $Tag Tag to extract attributes from.
	 *	@param array $required Required attributes.
	 *	@return array Extracted attributes.
	 */

	protected function _extractAttributesFromTag( \DOMNode $Tag, array $required ) {

		$attributes = array( );

		foreach ( $Tag->attributes as $a => $Attribute ) {
			if ( !empty( $required )) {
				if ( isset( $required[ $Attribute->name ])) {
					$pattern = $required[ $Attribute->name ];

					if ( $pattern && !preg_match( $pattern, $Attribute->value )) {
						return array( );
					}
				} else {
					continue;
				}
			}

			$attributes[ $Attribute->name ] = $Attribute->value;
		}

		$diff = array_diff_key( $required, $attributes );

		return empty( $diff )
			? $attributes
			: array( );
	}
}<commit_msg>Light refactoring<commit_after><?php

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

namespace fg\Essence\Dom;

use fg\Essence\Dom;
use fg\Essence\Exception;
use fg\Essence\Utility\Hash;



/**
 *	Handles HTML related operations through DomDocument.
 *
 *	@package fg.Essence.Dom
 */

class Native implements Dom {

	/**
	 *	{@inheritDoc}
	 */

	public function extractAttributes( $html, array $options ) {

		$reporting = error_reporting( 0 );
		$Document = \DomDocument::loadHTML( $html );
		error_reporting( $reporting );

		if ( $Document === false ) {
			throw new Exception( 'Unable to load HTML document.' );
		}

		$options = Hash::normalize( $options, array( ));
		$data = array( );

		foreach ( $options as $name => $required ) {
			$tags = $Document->getElementsByTagName( $name );
			$required = Hash::normalize( $required, '' );
			$data[ $name ] = array( );

			foreach ( $tags as $Tag ) {
				if ( $Tag->hasAttributes( )) {
					$attributes = $this->_extractAttributesFromTag( $Tag, $required );

					if ( !empty( $attributes )) {
						$data[ $name ][ ] = $attributes;
					}
				}
			}
		}

		return $data;
	}



	/**
	 *	Extracts attributes from the given tag.
	 *
	 *	@param \DOMNode $Tag Tag to extract attributes from.
	 *	@param array $required Required attributes.
	 *	@return array Extracted attributes.
	 */

	protected function _extractAttributesFromTag( \DOMNode $Tag, array $required ) {

		$attributes = array( );

		foreach ( $Tag->attributes as $name => $Attribute ) {
			if ( !empty( $required )) {
				if ( isset( $required[ $name ])) {
					$pattern = $required[ $name ];

					if ( $pattern && !preg_match( $pattern, $Attribute->value )) {
						return array( );
					}
				} else {
					continue;
				}
			}

			$attributes[ $name ] = $Attribute->value;
		}

		$diff = array_diff_key( $required, $attributes );

		return empty( $diff )
			? $attributes
			: array( );
	}
}
{'fraction_non_alphanumeric': 0.16242118537200503, 'fraction_numerical': 0.001008827238335435, 'mean_word_length': 2.966, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7562729', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1287, 'n_words': 319}