<commit_before><?php
/*
 * Copyright (C) 2011-2015 Solver Ltd. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
namespace Solver\Lab;

/**
 * Used by PageLog and ServiceLog.
 * 
 * This trait will probably be refactoring as a part of a base Log class, so don't grow "attached" to it.
 */
trait ImportEventsTrait {
	/**
	 * Imports all events from an ErrorProvider into the log with an optional path (re)map.
	 * 
	 * @param \Solver\Sloppy\EventProvider $eventProvider
	 * An event provider.
	 * 
	 * @param array $map
	 * A dict of "old base" => "new base" rules, which will be used to alter the paths of the imported events.
	 * 
	 * Be aware that for every event path, ONLY ONE rule will be matched and applied, and this is the LAST RULE that
	 * matches the path base in your event (rules are processed in reverse). The reverse processing is designed, so you
	 * can lay out your map naturally from least to most specific rules (the most specific rule will apply to your 
	 * path).
	 * 
	 * Note that events with path set to null or empty string (both semantically identical, meaning "no path") can have
	 * a path assigned to them by passing [null => 'foo.bar']. The result for path '' will be 'foo.bar', non-empty paths
	 * won't be affected.
	 * 
	 * TODO: This certainly needs to be explained better.
	 */
	public function import(EventProvider $eventProvider, array $map = null) {		
		if ($map) {
			// Build a regex with the map keys reverse (later entries take priority, but regex returns first match).	
			$mapKeys = \array_reverse(\array_keys($map));
			
			foreach ($mapKeys as & $val) $val = \preg_quote($val, '%'); 
			
			$regex = '%(' . \implode('|', \array_reverse(\array_keys($map))) . ')(?|$()()|(\.?)(.*))%AD';
							
			foreach ($eventProvider->getAllEvents() as $event) {				
				$path = $event['path'];
				
				// Semantically equivalent (null is the canonical value for it).
				if ($path === null) $path = '';
					
				if (\preg_match($regex, $path, $matches)) {
					$replacement = $map[$matches[1]];
					if ($replacement === '') $path = $matches[3];
					else $path = $replacement . ($matches[3] === '' ? '' : '.') . $matches[3];
				}

				if ($path === '') $path = null;
				
				$this->addError($path, $event['message'], $event['code'], $event['details']);
			}
		} else {
			foreach ($eventProvider->getAllEvents() as $event) {
				$this->addError($event['path'], $event['message'], $event['code'], $event['details']);
			}
		}
	}
}<commit_msg>In ImportEventsTrait, added a TODO for a future revision of this trait.<commit_after><?php
/*
 * Copyright (C) 2011-2015 Solver Ltd. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
namespace Solver\Lab;

/**
 * TODO: Importing should be separated as a concern from the log being imported (or imported into). Static call?
 * 
 * DO NOT use this trait in your log classes, it'll be refactored eventually.
 */
trait ImportEventsTrait {
	/**
	 * Imports all events from an ErrorProvider into the log with an optional path (re)map.
	 * 
	 * @param \Solver\Sloppy\EventProvider $eventProvider
	 * An event provider.
	 * 
	 * @param array $map
	 * A dict of "old base" => "new base" rules, which will be used to alter the paths of the imported events.
	 * 
	 * Be aware that for every event path, ONLY ONE rule will be matched and applied, and this is the LAST RULE that
	 * matches the path base in your event (rules are processed in reverse). The reverse processing is designed, so you
	 * can lay out your map naturally from least to most specific rules (the most specific rule will apply to your 
	 * path).
	 * 
	 * Note that events with path set to null or empty string (both semantically identical, meaning "no path") can have
	 * a path assigned to them by passing [null => 'foo.bar']. The result for path '' will be 'foo.bar', non-empty paths
	 * won't be affected.
	 * 
	 * TODO: This certainly needs to be explained better.
	 */
	public function import(EventProvider $eventProvider, array $map = null) {		
		if ($map) {
			// Build a regex with the map keys reverse (later entries take priority, but regex returns first match).	
			$mapKeys = \array_reverse(\array_keys($map));
			
			foreach ($mapKeys as & $val) $val = \preg_quote($val, '%'); 
			
			$regex = '%(' . \implode('|', \array_reverse(\array_keys($map))) . ')(?|$()()|(\.?)(.*))%AD';
							
			foreach ($eventProvider->getAllEvents() as $event) {				
				$path = $event['path'];
				
				// Semantically equivalent (null is the canonical value for it).
				if ($path === null) $path = '';
					
				if (\preg_match($regex, $path, $matches)) {
					$replacement = $map[$matches[1]];
					if ($replacement === '') $path = $matches[3];
					else $path = $replacement . ($matches[3] === '' ? '' : '.') . $matches[3];
				}

				if ($path === '') $path = null;
				
				$this->addError($path, $event['message'], $event['code'], $event['details']);
			}
		} else {
			foreach ($eventProvider->getAllEvents() as $event) {
				$this->addError($event['path'], $event['message'], $event['code'], $event['details']);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12999041839667838, 'fraction_numerical': 0.005110188438198659, 'mean_word_length': 3.0589760207388204, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2979225', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1737, 'n_words': 733}