Преглед на файлове

hotfix: tolérance sur les titres tmdb

Sangfroid преди 1 ден
родител
ревизия
b95788f92a
променени са 1 файла, в които са добавени 26 реда и са изтрити 2 реда
  1. 26 2
      src/Repository/FilmRepository.php

+ 26 - 2
src/Repository/FilmRepository.php

@@ -210,6 +210,11 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
+    /**
+     * Films déjà en base correspondant au film TMDb (mêmes réalisateurs et titre proche).
+     * Le titre est comparé de façon tolérante : TMDb renomme parfois ses films
+     * (ponctuation, sous-titre) et l'égalité stricte raterait alors le doublon.
+     */
     public function findFilmInImdb(Film $film): array
     {
         $reals = [];
@@ -217,14 +222,33 @@ class FilmRepository extends ServiceEntityRepository
             $reals[] = $real->getNomComplet();
         }
         $qb = $this->createQueryBuilder("f");
-        $qb->andWhere("f.titre = :titre")
-            ->setParameter("titre", $film->getTitre())
+        $qb->andWhere("f.titre LIKE :titrePattern")
+            ->setParameter("titrePattern", $this->titrePattern($film->getTitre()))
             ->innerJoin("f.realisateurs", "rea")
             ->andWhere($qb->expr()->in("rea.nomComplet", ":reals"))
             ->setParameter("reals", $reals);
         return $qb->getQuery()->getResult();
     }
 
+    /**
+     * Transforme un titre en motif LIKE : minuscules, accents réduits à l'ASCII
+     * et toute suite de non-alphanumériques remplacée par %.
+     * "Kaamelott - Premier volet" -> "%kaamelott%premier%volet%"
+     */
+    private function titrePattern(?string $titre): string
+    {
+        $normalise = mb_strtolower(trim((string) $titre));
+        $ascii = @iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $normalise);
+        if (is_string($ascii) && $ascii !== "") {
+            $normalise = $ascii;
+        }
+        $motif = (string) preg_replace("/[^a-z0-9]+/", "%", $normalise);
+        if (trim($motif, "%") === "") {
+            return (string) $titre;
+        }
+        return "%" . trim($motif, "%") . "%";
+    }
+
     public function findRandom(int $nb = 5): array
     {
         $films = $this->createQueryBuilder("f")