137 |
jpm |
1 |
<?php
|
|
|
2 |
class Surligneur {
|
|
|
3 |
private $texte = '';
|
202 |
delphine |
4 |
private $texteSansAccent = '';
|
137 |
jpm |
5 |
private $tags = array();
|
|
|
6 |
private $nbreSurlignageMaxParMot = 1;
|
|
|
7 |
private $nbreSurlignageCourant = 0;
|
|
|
8 |
|
202 |
delphine |
9 |
public function __construct($texte = null, $surlignageMaxParMot = null, ChaineManipulateur $manipulateurDeChaine = null) {
|
137 |
jpm |
10 |
if (is_null($texte) == false) {
|
|
|
11 |
$this->setTexte($texte);
|
|
|
12 |
}
|
|
|
13 |
if (is_null($surlignageMaxParMot) == false) {
|
|
|
14 |
$this->setNbreMaxSurlignageParMot($surlignageMaxParMot);
|
|
|
15 |
}
|
202 |
delphine |
16 |
|
|
|
17 |
$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
|
137 |
jpm |
18 |
}
|
|
|
19 |
|
|
|
20 |
public function setTexte($txt) {
|
|
|
21 |
$this->texte = $txt;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public function setNbreMaxSurlignageParMot($nbre) {
|
|
|
25 |
$this->nbreSurlignageMaxParMot = $nbre;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
public function surlignerMots($mots) {
|
|
|
29 |
$this->verifierTableauDeMots($mots);
|
|
|
30 |
$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
|
202 |
delphine |
31 |
$this->texteSansAccent = $this->manipulateurDeChaine->supprimerAccents($this->texte);
|
137 |
jpm |
32 |
foreach ($mots as $mot) {
|
|
|
33 |
$this->initialiserNbreSurlignageCourant();
|
|
|
34 |
$this->texte = $this->surlignerMot($mot);
|
|
|
35 |
}
|
|
|
36 |
$this->texte = preg_replace_callback('`<([0-9]+)>`', array($this, 'restaurerTags'), $this->texte);
|
|
|
37 |
return $this->texte;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private function verifierTableauDeMots($mots) {
|
|
|
41 |
if (is_array($mots) === false) {
|
|
|
42 |
$message = "Surligneur::surlignerMots() n'accepte que les tableaux de mots en argument";
|
|
|
43 |
throw new InvalidArgumentException($message);
|
|
|
44 |
} else {
|
|
|
45 |
if (count($mots) == 0) {
|
|
|
46 |
$message = "Surligneur::surlignerMots() n'accepte que des tableaux contenant au moins un mot";
|
|
|
47 |
throw new LengthException($message);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function sauverTags($match) {
|
|
|
53 |
$i = count($this->tags);
|
|
|
54 |
$this->tags[$i] = $match[0];
|
|
|
55 |
return '<'.$i.'>';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private function initialiserNbreSurlignageCourant() {
|
|
|
59 |
$this->nbreSurlignageCourant = 0;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
private function surlignerMot($mot) {
|
202 |
delphine |
63 |
$positionDebutMot = stripos($this->texteSansAccent, $mot);
|
137 |
jpm |
64 |
$longueurMot = strlen($mot);
|
|
|
65 |
$surlignage = $this->texte;
|
|
|
66 |
if ($positionDebutMot !== false) {
|
|
|
67 |
$this->nbreSurlignageCourant++;
|
|
|
68 |
if ($this->nbreSurlignageCourant <= $this->nbreSurlignageMaxParMot) {
|
|
|
69 |
$debut = substr($this->texte, 0, $positionDebutMot);
|
|
|
70 |
$milieu = substr($this->texte, $positionDebutMot, $longueurMot);
|
|
|
71 |
$this->texte = substr($this->texte, $positionDebutMot + $longueurMot);
|
|
|
72 |
$fin = $this->surlignerMot($mot);
|
|
|
73 |
$surlignage = $debut.$this->sauverTagSurlignage($milieu).$fin;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
return $surlignage;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function sauverTagSurlignage($motTrouve) {
|
|
|
80 |
$i = count($this->tags);
|
|
|
81 |
$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
|
|
|
82 |
return '<'.$i.'>';
|
|
|
83 |
}
|
202 |
delphine |
84 |
|
137 |
jpm |
85 |
private function restaurerTags($match) {
|
|
|
86 |
return $this->tags[$match[1]];
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
?>
|