Location: PHPKode > scripts > Synchronization Analysis > synchronization-analysis/synchanalysis.class.php
<?php
/*
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    http://www.gnu.org/licenses/gpl.txt
#
*/

 /******
 *
 * class SynchAnalysis - Analisys difference for 2 and more texts
 * 
 * @author	Vladimir S. Bredihin
 * @access	public
 * @version	1.0
 * @package	synchanalysis
 * @link	http://mycms.info
 *
 ******/

define ('SYNCH_ANALYSIS_LINE'	, "[\r\n]+");
define ('SYNCH_ANALYSIS_WORD'	, "[ 	\r\n]+");
define ('SYNCH_ANALYSIS_SYMBOL'	, '');

class SynchAnalysis {
	var $order	= array ();
	var $result	= array ();
	var $set	= array ();
	var $_resultFormat;
	var $error = null;
	/*
		Errors reporting:
		7 - Set on function synch not is array
		8 - Count elements of set < 2
		9 - One element is not string

		//	This errors im extended version class
		//	1 - Not define pattern on constructor [PRIMARY]
		//	2 - Not define pattern on constructor [P_LINE]
		//	3 - Not define pattern on constructor [SECOND]
		//	4 - Not define pattern on constructor [S_LINE]
		//	
		//	5 - Count define pattern on constructor != 4
		//	6 - Dublicate define pattern on constructor
	*/

	function SynchAnalysis ()
	{		
	}
	function synch ($set, $type=SYNCH_ANALYSIS_LINE)
	{	
		$this->_prepare_set ($set);
		$this->_init($set, $type);
		if ($this->error) return $this->error;
		$primary = 0;
		$set_primary = $set[$this->order[$primary]];
		$count_section = count ($this->order);
		for ($second = 1; $second < $count_section; $second++)
		{	
			$set[$this->order[$primary]] = $set_primary;
			$count_lines = count ($set[$this->order[$primary]]);
			for ($index = 0; $index < $count_lines; $index ++)
			{
                        	if ( isset($set[$this->order[$second]][$index]) )
                        	{
                        		if ($set[$this->order[$primary]][$index] != $set[$this->order[$second]][$index])
                        		{
                        			$this->_findStr($set, array($primary, $second), $index);
                        			$set[$this->order[$primary]][$index];
                        			$this->_findStr($set, array($second, $primary), $index);
                        			$set[$this->order[$second]][$index];
                        		}
                        	}
                        	elseif (isset($set[$this->order[$primary]][$index]))
                        	{
					$this->_member( $this->order[$primary], 
							$index,
							$this->order[$second],
							null );


				}
				
			}			
		}
		return $this->result;
	}
	function _prepare_set (&$set)
	{
		if (!is_array ($set)) $this->error = 7;
		elseif ( count ($set) < 2 ) $this->error = 8;
	}
	//This function initialization
	function _init (&$set, $type)
	{
		reset ($set);
		foreach ($set as $index=>$element)
		{
			if (!is_string ($element))
			{
				$this->error = 9;
				break;
			}
			if (!empty ($type))
				$set[$index] = split ($type, $element);
			else
				$set[$index] = $this->str_split ($element);
			$this->_count ($index, count ($set[$index]));
		}
		$this->set = $set;
		$this->_order ();
	}
	//This function prepare order an set by count lines desc
	function _count ($index, $count)
	{
		if (!isset($this->order [$count]))
			$this->order [$count] = $index;
		else $this->_count ($index, $count-1);
	}
	//This function set pointers sort from max count to min count
	function _order ()
	{
		krsort ($this->order);
		$this->order = array_values ($this->order);
	}

	function _findStr (&$set, $section, $index)
	{
		$find = null;
		reset ($set[$this->order[$section[1]]]);
		foreach ($set[$this->order[$section[1]]] as $k=>$element)
		{
			if ($set[$this->order[$section[0]]][$index] == $element
			&& (!isset ($set[$this->order[$section[0]]][$k]) 
			|| $set[$this->order[$section[0]]][$k] != $element))
			{
				$this->_member( $this->order[$section[0]], 
						$index,
						$this->order[$section[1]],
						$k );
				$this->_member( $this->order[$section[1]], 
						$k,
						$this->order[$section[0]],
						$index );
				$find = $k;
				break;
			}
		}
		if (!$find)
			$this->_member( $this->order[$section[0]], 
						$index,
						$this->order[$section[1]],
						null );
		return $find;
	}
	function _member ($primary, $p_line, $second, $s_line)
	{
		$this->result [$primary][$second][$p_line] = $s_line;
	}
	//analogue str_split of PHP5
	function str_split ($str)
	{
		$result = array ();
		$count = strlen ($str);
		for ($i = 0; $i < $count; $i++)
		{
			$result [] = $str[$i];
		}
		return $result;
	}
}
?>
Return current item: Synchronization Analysis