<commit_before><?php
class DRMCielCompare
{
	protected $xmlIn;
	protected $xmlOut;

	public function __construct($xmlIn, $xmlOut)
	{
		if(is_string($xmlIn)) {
			$this->xmlIn = simplexml_load_string($xmlIn);
		}else {
			$this->xmlIn = $xmlIn;
		}
		if(is_string($xmlOut)) {
			$this->xmlOut = simplexml_load_string($xmlOut);
		}else {
			$this->xmlOut = $xmlOut;
		}
	}

	public function getDiff()
	{
		$arrIn = $this->identifyKey($this->flattenArray($this->xmlToArray($this->xmlIn)));
		$arrOut = $this->identifyKey($this->flattenArray($this->xmlToArray($this->xmlOut)));

		$diff = array();
		foreach ($arrIn as $key => $value) {
			if (!isset($arrOut[$key]) && $value) {
				$diff[$key] = $value;
			}
			if (isset($arrOut[$key]) && $arrOut[$key] != $value) {
				$diff[$key] = $value;
			}
		}
		foreach ($arrOut as $key => $value) {
			if (!isset($arrIn[$key]) && $value) {
				$diff[$key] = null;
			}
		}
		return $diff;
	}

	public function hasDiff()
	{
		return (count($this->getDiff()) > 0)? true : false;
	}

	private function xmlToArray($xml)
	{
		return json_decode(json_encode((array)$xml), true);
	}

	private function flattenArray($array)
	{
		return acCouchdbToolsJson::json2FlatenArray($array, null, '_');
	}

	private function identifyKey($array)
	{
		$patternProduit = '/\/produit\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$patternCrd = '/\/compte-crd\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$patternCentilisation = '/\/centilisation\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$newKeyProduit = '';
		$newKeyCrd = '';
		$newKeyCentilisation = '';
		$result = array();
		foreach ($array as $key => $value) {
			if (preg_match('/\/produit\//i', $key) || preg_match('/\/compte-crd\//i', $key) || preg_match('/\/centilisation\//i', $key)) {
				if (preg_match('/\/observations/i', $key)) {
					continue;
				}
				if (preg_match($patternProduit, $key) && (preg_match('/code-inao/i', $key) || preg_match('/libelle-fiscal/i', $key))) {
					$newKeyProduit = $value;
					continue;
				}
				if (preg_match($patternProduit, $key) && preg_match('/libelle-personnalise/i', $key)) {
					$newKeyProduit .= '_'.KeyInflector::slugify($value);
					continue;
				}
				if (preg_match($patternCrd, $key) && preg_match('/categorie-fiscale-capsules/i', $key)) {
					$newKeyCrd = $value;
					continue;
				}
				if (preg_match($patternCrd, $key) && preg_match('/type-capsule/i', $key)) {
					$newKeyCrd .= '_'.$value;
					continue;
				}
				if (preg_match($patternCentilisation, $key) && preg_match('/@attributes/i', $key)) {
					$newKeyCentilisation = $value;
					continue;
				}
				$value = $this->cleanValue($value);
				if (preg_match($patternProduit, $key)) {
					$tmp = preg_replace($patternProduit, '/produit/{array}/'.$newKeyProduit.'/{array}/', $key);
					$result[$tmp] = $value;
				} elseif (preg_match($patternCrd, $key) || preg_match($patternCentilisation, $key)) {
					$tmp = preg_replace($patternCrd, '/compte-crd/{array}/'.$newKeyCrd.'/{array}/', $key);
					$tmp = preg_replace($patternCentilisation, '/centilisation/{array}/'.$newKeyCentilisation.'/{array}/', $tmp);
					$result[$tmp] = $value;
				} else {
					$result[$key] = $value;
				}
			}
		}
		return $result;
	}

	private function cleanValue($value)
	{
		if ($value == "false") {
			return 0;
		}
		if ($value == "true") {
			return 1;
		}
		if (is_numeric($value)) {
			return $value * 1;
		}
		return $value;
	}


	    public function getFormattedXMLComparaison() {
	      $str_arr = array();
	      foreach ($this->getDiff() as $key => $value) {
	        $keyArr = explode("/",$key);
	        if(strpos($key,"{array}/produit/{array}")){
	          $probleme = "[Problème de ".str_replace("-"," ",$keyArr[7])."]";
	          $categorie = "".str_replace("-"," ",$keyArr[1]);
	          $produit = "produit ".str_replace("_"," ",str_replace("-"," ",$keyArr[5]));
	          $mvt = "".str_replace("-"," ",$keyArr[9]);
	          $str_arr[$probleme." ".$categorie." ".$produit." ".$mvt] = (is_null($value))? "valeur nulle" : $value;
	        }elseif(strpos($key,"{array}/compte-crd/{array}")){
	          $probleme = "[Problème de ".str_replace("-"," ",$keyArr[1])."]";
	          $capsule = "capsule : ".str_replace("_"," ",str_replace("-"," ",$keyArr[3]));
	          $categorie = " ".str_replace("-"," ",$keyArr[7]);
	          $mvt = "".str_replace("-"," ",$keyArr[9]);
	          //$str_arr[$probleme." ".$capsule." ".$categorie." ".$mvt] = (is_null($value))? "valeur nulle" : $value;
	          $str_arr["Une différence a été détecté dans le compte capsule"] = "";
	        }else{
	          $str_arr[$key] = $value;

	        }
	      }
	      return $str_arr;
	    }
}<commit_msg>Tableau récapitulatif de la comparaison xml<commit_after><?php
class DRMCielCompare
{
	protected $xmlIn;
	protected $xmlOut;

	public function __construct($xmlIn, $xmlOut)
	{
		if(is_string($xmlIn)) {
			$this->xmlIn = simplexml_load_string($xmlIn);
		}else {
			$this->xmlIn = $xmlIn;
		}
		if(is_string($xmlOut)) {
			$this->xmlOut = simplexml_load_string($xmlOut);
		}else {
			$this->xmlOut = $xmlOut;
		}
	}

	public function getDiff()
	{
		$arrIn = $this->identifyKey($this->flattenArray($this->xmlToArray($this->xmlIn)));
		$arrOut = $this->identifyKey($this->flattenArray($this->xmlToArray($this->xmlOut)));

		$diff = array();
		foreach ($arrIn as $key => $value) {
			if (!isset($arrOut[$key]) && $value) {
				$diff[$key] = $value;
			}
			if (isset($arrOut[$key]) && $arrOut[$key] != $value) {
				$diff[$key] = $value;
			}
		}
		foreach ($arrOut as $key => $value) {
			if (!isset($arrIn[$key]) && $value) {
				$diff[$key] = null;
			}
		}
		return $diff;
	}

	public function hasDiff()
	{
		return (count($this->getDiff()) > 0)? true : false;
	}

	private function xmlToArray($xml)
	{
		return json_decode(json_encode((array)$xml), true);
	}

	private function flattenArray($array)
	{
		return acCouchdbToolsJson::json2FlatenArray($array, null, '_');
	}

	private function identifyKey($array)
	{
		$patternProduit = '/\/produit\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$patternCrd = '/\/compte-crd\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$patternCentilisation = '/\/centilisation\/\{array\}\/([0-9]+)\/\{array\}\//i';
		$newKeyProduit = '';
		$newKeyCrd = '';
		$newKeyCentilisation = '';
		$result = array();
		foreach ($array as $key => $value) {
			if (preg_match('/\/produit\//i', $key) || preg_match('/\/compte-crd\//i', $key) || preg_match('/\/centilisation\//i', $key)) {
				if (preg_match('/\/observations/i', $key)) {
					continue;
				}
				if (preg_match($patternProduit, $key) && (preg_match('/code-inao/i', $key) || preg_match('/libelle-fiscal/i', $key))) {
					$newKeyProduit = $value;
					continue;
				}
				if (preg_match($patternProduit, $key) && preg_match('/libelle-personnalise/i', $key)) {
					$newKeyProduit .= '_'.KeyInflector::slugify($value);
					continue;
				}
				if (preg_match($patternCrd, $key) && preg_match('/categorie-fiscale-capsules/i', $key)) {
					$newKeyCrd = $value;
					continue;
				}
				if (preg_match($patternCrd, $key) && preg_match('/type-capsule/i', $key)) {
					$newKeyCrd .= '_'.$value;
					continue;
				}
				if (preg_match($patternCentilisation, $key) && preg_match('/@attributes/i', $key)) {
					$newKeyCentilisation = $value;
					continue;
				}
				$value = $this->cleanValue($value);
				if (preg_match($patternProduit, $key)) {
					$tmp = preg_replace($patternProduit, '/produit/{array}/'.$newKeyProduit.'/{array}/', $key);
					$result[$tmp] = $value;
				} elseif (preg_match($patternCrd, $key) || preg_match($patternCentilisation, $key)) {
					$tmp = preg_replace($patternCrd, '/compte-crd/{array}/'.$newKeyCrd.'/{array}/', $key);
					$tmp = preg_replace($patternCentilisation, '/centilisation/{array}/'.$newKeyCentilisation.'/{array}/', $tmp);
					$result[$tmp] = $value;
				} else {
					$result[$key] = $value;
				}
			}
		}
		return $result;
	}

	private function cleanValue($value)
	{
		if ($value == "false") {
			return 0;
		}
		if ($value == "true") {
			return 1;
		}
		if (is_numeric($value)) {
			return $value * 1;
		}
		return $value;
	}


	    public function getFormattedXMLComparaison() {
	      $str_arr = array();
	      foreach ($this->getDiff() as $key => $value) {
	        $keyArr = explode("/",$key);
	        if(strpos($key,"{array}/produit/{array}")){
	          $probleme = "[Problème de ".str_replace("-"," ",$keyArr[7])." en ".str_replace("-"," ",$keyArr[1])."]";
	          $produit = "".str_replace("_"," ",str_replace("-"," ",$keyArr[5]));
	          $catMvt = "".str_replace(array("-periode"),array(""),$keyArr[9]);
						$mvt = (isset($keyArr[11]))? " ".str_replace("-"," ",$keyArr[11]) : '';
	          $str_arr[$probleme." ".$produit." ".$catMvt.$mvt] = (is_null($value))? "valeur nulle" : $value;
	        }elseif(strpos($key,"{array}/compte-crd/{array}")){
						$keyArr = explode("/",$key);
	          $probleme = "[Problème de CRD ".str_replace(array("T_PERSONNALISEES","M_PERSONNALISEES"),array("TRANQ","MOUSSEUX"),$keyArr[3])."]";
	          $origine = ucfirst($keyArr[5])." ".str_replace(array("-capsules"),array(""),$keyArr[7]);

	          $mvt = (isset($keyArr[9]))? " ".str_replace(array("-capsules"),array(""),$keyArr[9]) : '';
						$mvt .= (isset($keyArr[11]))? " ".$keyArr[11] : '';
	          $str_arr[$probleme." ".$origine.$mvt] = (is_null($value))? "valeur nulle" : $value;
	        }else{
	          $str_arr[$key] = $value;
	        }

	      }
	      return $str_arr;
	    }
}
{'fraction_non_alphanumeric': 0.23200757575757575, 'fraction_numerical': 0.004945286195286195, 'mean_word_length': 3.2853922452660056, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 47}, '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': '5525094', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3605, 'n_words': 553}