<commit_before><?php
namespace WFV\Collection;
defined( 'ABSPATH' ) or die();

use WFV\Abstraction\Collectable;

/**
 *
 *
 * @since 0.10.0
 */
class RuleCollection extends Collectable {

	/**
	 *
	 *
	 * @since 0.10.0
	 *
	 * @param array $rules
	 */
	public function __construct( array $rules ) {
		$this->data = $this->parse_rules( $rules );
	}


	/**
	 * Get rules array
	 *
	 * @since 0.10.0
	 *
	 * @return array
	 */
	public function get_array() {
		return $this->data;
	}

	/**
	 *
	 *
	 * @since 0.11.0
	 * @access private
	 *
	 * @param string $rule
	 * @return bool
	 */
	private function extract_name( $rule ) {
		return strstr( $rule, ':', true );
	}

	/**
	 *
	 *
	 * @since 0.11.0
	 * @access private
	 *
	 * @param string $rule
	 * @return bool
	 */
	private function extract_params( $rule ) {
		return ltrim( strstr($rule, ':'), ':');
	}

	/**
	 * Checks if a rule string has parameters
	 *
	 * @since 0.11.0
	 * @access private
	 *
	 * @param string $rule
	 * @return bool
	 */
	private function has_parameters( $rule ) {
		return strpos( $rule, ':' );
	}

	/**
	 *
	 *
	 * @since 0.11.0
	 * @access private
	 *
	 * @param array $rules
	 * @return array
	 */
	private function parse_rules( array $rules ) {

		// WIP - not working properly... simplify!
		$rules = $this->split_rules( $rules );
		return array_map( function( $item ) {
			foreach( $item as $rule ) {
				if ( $this->has_parameters( $rule ) ) {
					$array_rule = array([
						'rule' => $this->extract_name( $rule ),
						'params' => explode( ',', $this->extract_params( $rule ) )
					]);
				}
			}
			return ( $array_rule ) ? $array_rule : $item;
		}, $rules );
	}

	/**
	 * Converts string ruleset to index array
	 *
	 * @since 0.11.0
	 * @access private
	 *
	 * @param array $rules
	 * @return array
	 */
	private function split_rules( array $rules ) {
		// perhaps the $rules array structure should be validated here?...
		return array_map( function( $item ) {
			return explode( '|', $item );
		}, $rules );
	}

	/**
	 * Check if rule is custom
	 *
	 * @since 0.10.0
	 *
	 * @param string $rule
	 * @return bool
	 */
	public function is_custom( $rule ) {
		// WIP - needs adjusting to new rule string format
		if ( false === is_array( $rule ) ){
			return ( false !== strpos( $rule, 'custom:' ) ) ? true : false;
		}
	}
}<commit_msg>RuleCollection - parse_rules now works as expected<commit_after><?php
namespace WFV\Collection;
defined( 'ABSPATH' ) or die();

use WFV\Abstraction\Collectable;

/**
 *
 *
 * @since 0.10.0
 */
class RuleCollection extends Collectable {

	/**
	 *
	 *
	 * @since 0.10.0
	 *
	 * @param array $rules
	 */
	public function __construct( array $rules ) {
		$this->data = $this->parse_rules( $rules );
	}


	/**
	 * Get rules array
	 *
	 * @since 0.10.0
	 *
	 * @return array
	 */
	public function get_array() {
		return $this->data;
	}

	/**
	 * Check if rule is custom
	 *
	 * @since 0.10.0
	 *
	 * @param string $rule
	 * @return bool
	 */
	public function is_custom( $rule ) {
		// WIP - needs adjusting to new rule string format
		if ( false === is_array( $rule ) ){
			return ( false !== strpos( $rule, 'custom:' ) ) ? true : false;
		}
	}

	/**
	 * Extract rule name from a rule string
	 *
	 * @since 0.11.0
	 * @access protected
	 *
	 * @param string $rule
	 * @return bool
	 */
	protected function extract_name( $rule ) {
		return strstr( $rule, ':', true );
	}

	/**
	 * Extract rule parameters from a rule string
	 *
	 * @since 0.11.0
	 * @access protected
	 *
	 * @param string $rule
	 * @return bool
	 */
	protected function extract_params( $rule ) {
		return ltrim( strstr($rule, ':'), ':');
	}

	/**
	 * Checks if a rule string has parameters
	 *
	 * @since 0.11.0
	 * @access protected
	 *
	 * @param string $rule
	 * @return bool
	 */
	protected function has_parameters( $rule ) {
		return strpos( $rule, ':' );
	}

	/**
	 * Split each string ruleset from config array
	 *  into a machine friendly multi-dimensional array
	 *
	 * @since 0.11.0
	 * @access protected
	 *
	 * @param array $rules
	 * @return array
	 */
	protected function parse_rules( array $rules ) {
		$this->split_rules( $rules );
		foreach( $rules as $field => $ruleset ) {
			$parsed[ $field ] = array_map( function( $rule ) {
				if ( $this->has_parameters( $rule ) ) {
					return array(
						'rule' => $this->extract_name( $rule ),
						'params' => explode( ',', $this->extract_params( $rule ) )
					);
				}
				return $rule;
			}, $ruleset );
		}
		return $parsed;
	}

	/**
	 * Converts string ruleset to index array
	 *
	 * @since 0.11.0
	 * @access protected
	 *
	 * @param array $rules
	 * @return array
	 */
	protected function split_rules( array &$rules ) {
		// perhaps the $rules array structure should be validated here?...
		$rules = array_map( function( $item ) {
			return explode( '|', $item );
		}, $rules );
	}
}
{'fraction_non_alphanumeric': 0.16187719660946867, 'fraction_numerical': 0.014885259458341947, 'mean_word_length': 2.5890207715133533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '344375', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1611, 'n_words': 458}