Просмотр исходного кода

apply string normalization to address too, improve the normalization

Olivier Massot 1 год назад
Родитель
Сommit
fec9c2591c

+ 17 - 8
sql/schema-extensions/003-view_organization_identification.sql

@@ -1,11 +1,20 @@
 CREATE OR REPLACE VIEW view_organization_identification AS
-    SELECT o.id, o.name, REGEXP_REPLACE(LOWER(o.name), '[^a-z0-9]+', '+') AS normalizedName,
-       o.identifier, o.siretNumber, o.waldecNumber,
-       CONCAT(a.streetAddress, ' ', a.streetAddressSecond, ' ', a.streetAddressThird) AS address,
-       a.addressCity AS city, a.postalCode, c.email, c.telphone
+    SELECT o.id, o.name, o.identifier, o.siretNumber, o.waldecNumber,
+           a.streetAddress, a.streetAddressSecond, a.streetAddressThird, a.addressCity, a.postalCode,
+           c.email, c.telphone,
+           REGEXP_REPLACE(
+                   REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(LOWER(trim(o.name)), '[éèê]', 'e'), '[à]', 'a'), '[ç]', 'c'),
+                   '[^a-z0-9]+',
+                   '+'
+           ) as normalizedName,
+           REGEXP_REPLACE(
+                   LOWER(REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(trim(concat(a.streetAddress, ' ', a.streetAddressSecond, ' ', a.streetAddressThird)), '[éèê]', 'e'), '[à]', 'a'), '[ç]', 'c')),
+                   '[^a-z0-9]+',
+                   '+'
+           ) as normalizedAddress
     FROM opentalent.Organization o
-        INNER JOIN opentalent.OrganizationAddressPostal oa ON oa.organization_id = o.id
-        INNER JOIN opentalent.AddressPostal a ON a.id = oa.addressPostal_id
-        INNER JOIN opentalent.organization_contactpoint oc ON oc.organization_id = o.id
-        INNER JOIN opentalent.ContactPoint c ON oc.contactPoint_id = c.id
+             INNER JOIN opentalent.OrganizationAddressPostal oa ON oa.organization_id = o.id
+             INNER JOIN opentalent.AddressPostal a ON a.id = oa.addressPostal_id
+             INNER JOIN opentalent.organization_contactpoint oc ON oc.organization_id = o.id
+             INNER JOIN opentalent.ContactPoint c ON oc.contactPoint_id = c.id
     WHERE oa.type='ADDRESS_HEAD_OFFICE' AND c.contactType='PRINCIPAL';

+ 14 - 5
src/Entity/Organization/OrganizationIdentification.php

@@ -46,7 +46,16 @@ class OrganizationIdentification
     private ?string $waldecNumber;
 
     #[ORM\Column]
-    private ?string $address;
+    private ?string $normalizedAddress;
+
+    #[ORM\Column]
+    private ?string $streetAddress;
+
+    #[ORM\Column]
+    private ?string $streetAddressSecond;
+
+    #[ORM\Column]
+    private ?string $streetAddressThird;
 
     #[ORM\Column]
     private ?string $city;
@@ -126,14 +135,14 @@ class OrganizationIdentification
         return $this;
     }
 
-    public function getAddress(): ?string
+    public function getNormalizedAddress(): ?string
     {
-        return $this->address;
+        return $this->normalizedAddress;
     }
 
-    public function setAddress(?string $address): self
+    public function setNormalizedAddress(?string $normalizedAddress): self
     {
-        $this->address = $address;
+        $this->normalizedAddress = $normalizedAddress;
         return $this;
     }
 

+ 19 - 7
src/Service/Organization/OrganizationFactory.php

@@ -202,11 +202,7 @@ class OrganizationFactory
             );
         }
 
-        $normalizedName = preg_replace(
-            "/[^a-z0-9]+/",
-            "+",
-            strtolower($organizationCreationRequest->getName())
-        );
+        $normalizedName = $this->normalizeIdentificationField($organizationCreationRequest->getName());
 
         if (
             $this->organizationIdentificationRepository->findOneBy(
@@ -219,11 +215,11 @@ class OrganizationFactory
             );
         }
 
-        $address = implode(' ', [
+        $address = $this->normalizeIdentificationField(implode(' ', [
             $organizationCreationRequest->getStreetAddress1(),
             $organizationCreationRequest->getStreetAddress2(),
             $organizationCreationRequest->getStreetAddress3()
-        ]);
+        ]));
 
         if (
             $this->organizationIdentificationRepository->findOneBy(
@@ -705,4 +701,20 @@ class OrganizationFactory
 
         return null;
     }
+
+    /**
+     * Normalise la chaine comme sont normalisées les champs de l'entité OrganizationIdentification
+     *
+     * @øee sql/schema-extensions/003-view_organization_identification.sql
+     *
+     * @param string $value
+     * @return array|string|string[]|null
+     */
+    protected function normalizeIdentificationField(string $value) {
+        $value = strtolower(trim($value));
+        $value = preg_replace('/[éèê]/', 'e', $value);
+        $value = preg_replace('/[à]/', 'a', $value);
+        $value = preg_replace('/[ç]/', 'c', $value);
+        return preg_replace('/[^a-z0-9]+/', '+', $value);
+    }
 }