Subversion Repositories eFlore/Applications.eflore-consultation

Compare Revisions

Ignore whitespace Rev 136 → Rev 137

/trunk/tests/bibliotheque/SurligneurTest.php
New file
0,0 → 1,53
<?php
require dirname(__FILE__).'/../../bibliotheque/Surligneur.php';
 
class SurligneurTest extends PHPUnit_Framework_TestCase {
 
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Surligneur::surlignerMots() n'accepte que les tableaux de mots en argument
*/
public function testerSurlignerMotsAvecChaineRetourneInvalidArgumentException() {
$surligneur = new Surligneur();
$surligneur->surlignerMots('test');
}
 
/**
* @expectedException LengthException
* @expectedExceptionMessage Surligneur::surlignerMots() n'accepte que des tableaux contenant au moins un mot
*/
public function testerSurlignerMotsAvecTableauVideRetourneLengthException() {
$surligneur = new Surligneur();
$surligneur->surlignerMots(array());
}
 
public function testerSurlignerMotsRetournePremiereOccurenceMotSurlignee() {
$surligneur = new Surligneur();
$surligneur->setTexte('Viola alba subsp. alba');
$txtSurligne = $surligneur->surlignerMots(array('viola', 'al'));
 
$txtSurligneAttendu = '<span class="surlignage">Viola</span> <span class="surlignage">al</span>ba subsp. alba';
$this->assertEquals($txtSurligneAttendu, $txtSurligne);
}
 
public function testerSurlignerMotsRetourneDeuxPremieresOccurencesMotSurlignees() {
$surligneur = new Surligneur();
$surligneur->setNbreMaxSurlignageParMot(2);
$surligneur->setTexte('Viola alba subsp. alba');
 
$txtSurligne = $surligneur->surlignerMots(array('viola', 'al'));
 
$txtSurligneAttendu = '<span class="surlignage">Viola</span> <span class="surlignage">al</span>ba subsp. <span class="surlignage">al</span>ba';
$this->assertEquals($txtSurligneAttendu, $txtSurligne);
}
 
public function testerSurlignerMotsParConstructeurRetourneDeuxPremieresOccurencesMotSurlignees() {
$surligneur = new Surligneur('Viola alba subsp. alba', 2);
 
$txtSurligne = $surligneur->surlignerMots(array('viola', 'al'));
 
$txtSurligneAttendu = '<span class="surlignage">Viola</span> <span class="surlignage">al</span>ba subsp. <span class="surlignage">al</span>ba';
$this->assertEquals($txtSurligneAttendu, $txtSurligne);
}
}
?>
/trunk/modules/resultat/Resultat.php
77,9 → 77,9
 
private function surlignerMotsMasqueRecherche($nom) {
$mots = explode(' ', $this->masqueRecherche);
foreach ($mots as $mot) {
$nom = $this->surlignerMotDansTxt($mot, $nom);
}
$surligneur = new Surligneur();
$surligneur->setTexte($nom);
$nom = $surligneur->surlignerMots($mots);
return $nom;
}
 
165,19 → 165,6
return $urls;
}
 
private function surlignerNomsPourDetermination($noms) {
foreach ($noms as $idNom => $nom) {
$nom['nom_sci_retenu'] = $this->surlignerMotsMasqueRecherche($nom['nom_sci_retenu']);
if (isset($nom['synonymes'])) {
foreach ($nom['synonymes'] as $idSyn => $synonyme) {
$nom['synonymes'][$idSyn]['nom_sci'] = $this->surlignerMotsMasqueRecherche($synonyme['nom_sci']);
}
}
$noms[$idNom] = $nom;
}
return $noms;
}
 
private function extraireNomsPourDetermination($noms) {
$nomsSansCorrespondance = array();
$nomsAvecCorrespondance = array();
229,6 → 216,19
return $noms;
}
 
private function surlignerNomsPourDetermination($noms) {
foreach ($noms as $idNom => $nom) {
$nom['nom_sci_retenu'] = $this->surlignerMotsMasqueRecherche($nom['nom_sci_retenu']);
if (isset($nom['synonymes'])) {
foreach ($nom['synonymes'] as $idSyn => $synonyme) {
$nom['synonymes'][$idSyn]['nom_sci'] = $this->surlignerMotsMasqueRecherche($synonyme['nom_sci']);
}
}
$noms[$idNom] = $nom;
}
return $noms;
}
 
private function genererListeDecompo($noms) {
return '<p>À réaliser</p>';
}
/trunk/bibliotheque/Surligneur.php
New file
0,0 → 1,85
<?php
class Surligneur {
private $texte = '';
private $tags = array();
private $nbreSurlignageMaxParMot = 1;
private $nbreSurlignageCourant = 0;
 
public function __construct($texte = null, $surlignageMaxParMot = null) {
if (is_null($texte) == false) {
$this->setTexte($texte);
}
if (is_null($surlignageMaxParMot) == false) {
$this->setNbreMaxSurlignageParMot($surlignageMaxParMot);
}
}
 
public function setTexte($txt) {
$this->texte = $txt;
}
 
public function setNbreMaxSurlignageParMot($nbre) {
$this->nbreSurlignageMaxParMot = $nbre;
}
 
public function surlignerMots($mots) {
$this->verifierTableauDeMots($mots);
$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
foreach ($mots as $mot) {
$this->initialiserNbreSurlignageCourant();
$this->texte = $this->surlignerMot($mot);
}
$this->texte = preg_replace_callback('`<([0-9]+)>`', array($this, 'restaurerTags'), $this->texte);
return $this->texte;
}
 
private function verifierTableauDeMots($mots) {
if (is_array($mots) === false) {
$message = "Surligneur::surlignerMots() n'accepte que les tableaux de mots en argument";
throw new InvalidArgumentException($message);
} else {
if (count($mots) == 0) {
$message = "Surligneur::surlignerMots() n'accepte que des tableaux contenant au moins un mot";
throw new LengthException($message);
}
}
}
 
private function sauverTags($match) {
$i = count($this->tags);
$this->tags[$i] = $match[0];
return '<'.$i.'>';
}
 
private function initialiserNbreSurlignageCourant() {
$this->nbreSurlignageCourant = 0;
}
 
private function surlignerMot($mot) {
$positionDebutMot = stripos($this->texte, $mot);
$longueurMot = strlen($mot);
$surlignage = $this->texte;
if ($positionDebutMot !== false) {
$this->nbreSurlignageCourant++;
if ($this->nbreSurlignageCourant <= $this->nbreSurlignageMaxParMot) {
$debut = substr($this->texte, 0, $positionDebutMot);
$milieu = substr($this->texte, $positionDebutMot, $longueurMot);
$this->texte = substr($this->texte, $positionDebutMot + $longueurMot);
$fin = $this->surlignerMot($mot);
$surlignage = $debut.$this->sauverTagSurlignage($milieu).$fin;
}
}
return $surlignage;
}
 
private function sauverTagSurlignage($motTrouve) {
$i = count($this->tags);
$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
return '<'.$i.'>';
}
 
private function restaurerTags($match) {
return $this->tags[$match[1]];
}
}
?>