<?php
declare(strict_types=1);
namespace App\Community;
final class ContentFingerprintService
{
    public function normalize(string $content): string
    {
        $value=html_entity_decode(trim($content),ENT_QUOTES|ENT_HTML5,'UTF-8');
        if(function_exists('mb_strtolower'))$value=mb_strtolower($value,'UTF-8');else$value=strtolower($value);
        $value=preg_replace('~https?://~iu','http ',$value)??$value;
        $value=preg_replace('/[\p{P}\p{S}]+/u',' ',$value)??$value;
        $value=preg_replace('/\s+/u',' ',trim($value))??trim($value);
        return $value;
    }
    public function fingerprint(string $content): string{return hash('sha256',$this->normalize($content));}
    public function equivalent(string $a,string $b): bool{return hash_equals($this->fingerprint($a),$this->fingerprint($b));}
}
