spin = $spin; $this->debug(); if(empty($this->spin)) { throw new Exception('Spin vide.','1'); exit; } if($this->error) { throw new Exception('Spin buggué '.str_replace('\n','
',$this->buildDebug()),'2'); exit; } } public function __toString() { return $this->spin; } public function contentSpinning($text=''){ if($this->error) { return false; } if(empty($text)) { $text = $this->spin; } if(! preg_match('/{/si', $text)) { return $text; } preg_match_all('/\{([^{}]*)\}/si', $text, $matches); $occur = count($matches[1]); for ($i=0; $i<$occur; ++$i) { $replace = ''; $word_spinning = explode("|",$matches[1][$i]); foreach($word_spinning as $word) { if($this->minmax === 1) { $max=0; foreach($word_spinning as $key=>$word) { $strlen=strlen(trim($word)); if($strlen > $max) { $max = $strlen; $indent=$key; } } $replace = $word_spinning[$indent]; } else if($this->minmax === 0) { $min=99999; foreach($word_spinning as $key=>$word) { $strlen=strlen($word); if($strlen < $min) { $min = $strlen; $indent=$key; } } $replace = $word_spinning[$indent]; } else { shuffle($word_spinning); $replace = $word_spinning[0]; } } $prev_pos = strpos($text,$matches[0][$i]); $text = substr_replace($text,$replace,(int)strpos($text,$matches[0][$i],$prev_pos), strlen($matches[0][$i])); } return $this->contentSpinning($text); }//contentSpinning public function debug() { // Juste pour l'affichage de la "Localisation" $length = (strlen($this->spin)<20) ? intval(strlen($this->spin)/2) : 20; $tab = array(); // On parcours le texte for($j=0,$sz=strlen($this->spin);$j<$sz;++$j) { $elength = $length; if($j < $elength) { $elength = $j; } $start = $j-$elength; if($elength == 0) { $start = $elength; } // Si c'est un crochet ouvrant on cree un nouvel element de tableau // Si c'est un crochet fermant on complete le tableau de l'avant dernier crochet if($this->spin[$j] == '{') { $tab[count($tab)] = array('o'=>$j,'c'=>false); } else if ($this->spin[$j] == '}') { if(empty($tab)) { $this->error = true; $subStr = ($start>0) ? substr($this->spin,$subStart,$elength) : ''; $this->errors[] = array('type'=>'fermeture','pos'=>$j, 'loc'=>$subStr .'}'.substr($this->spin,$j+1,$elength)); } for($i=count($tab)-1;$i>=0;--$i) { // Si on trouve un crochet fermant pour un precedemment ouvert et a false if($tab[$i]['c'] === false) { $tab[$i]['c'] = $j; break; } else if ($i == 0) { $this->error = true; $subStr = ($start>0) ? substr($this->spin,$subStart,$elength) : ''; $this->errors[] = array('type'=>'fermeture','pos'=>$j, 'loc'=>$subStr .'}'.substr($this->spin,$j+1,$elength)); } } } } // On parcours à nouveau le tableau pour voir s'il n'y a pas un crochet ouvrant qui n'est pas ferme foreach($tab as $elem) { if(in_array(false,$elem)) { if($elem['c'] !== false) { continue; } $length = (strlen($this->spin)<20) ? intval($this->spin/2) : 20; $this->error = true; $subStr = ($elem['o']-$length>0) ? substr($this->spin,$elem['o']-$length,$length) : ''; $this->errors[] = array('type'=>'ouverture','pos'=>$elem['o'], 'loc'=>$subStr .'{'.substr($this->spin,$elem['o']+1,$length)); } } }//debug private function buildDebug() { $msg = ''; foreach($this->errors as $error) { $msg .= ($error['type'] == 'ouverture') ? 'Ouverture non fermée' : 'Fermeture en trop'; $msg .= ' (position : '.$error['pos'].')'; $msg .= ' : "'.$error['loc'].'"\n'; } return $msg; } } // Spin