<?php
/**
 * Persian Text Normalizer
 * Converts Arabic characters to their Persian equivalents
 */

class PersianNormalizer {
    
    // Mapping of Arabic characters to Persian characters
    private static $arabicToPersianMap = [
        'ي' => 'ی',  // Arabic Yeh to Persian Yeh
        'ك' => 'ک',  // Arabic Kaf to Persian Kaf
        'ە' => 'ه',  // Arabic Heh to Persian Heh
        '٠' => '۰',  // Arabic-Indic Digit Zero to Extended Arabic-Indic Digit Zero
        '١' => '۱',  // Arabic-Indic Digit One to Extended Arabic-Indic Digit One
        '٢' => '۲',  // Arabic-Indic Digit Two to Extended Arabic-Indic Digit Two
        '٣' => '۳',  // Arabic-Indic Digit Three to Extended Arabic-Indic Digit Three
        '٤' => '۴',  // Arabic-Indic Digit Four to Extended Arabic-Indic Digit Four
        '٥' => '۵',  // Arabic-Indic Digit Five to Extended Arabic-Indic Digit Five
        '٦' => '۶',  // Arabic-Indic Digit Six to Extended Arabic-Indic Digit Six
        '٧' => '۷',  // Arabic-Indic Digit Seven to Extended Arabic-Indic Digit Seven
        '٨' => '۸',  // Arabic-Indic Digit Eight to Extended Arabic-Indic Digit Eight
        '٩' => '۹',  // Arabic-Indic Digit Nine to Extended Arabic-Indic Digit Nine
    ];
    
    /**
     * Normalize Arabic text to Persian
     * @param string $text Text to normalize
     * @return string Normalized text with Persian characters
     */
    public static function normalizeToPersian($text) {
        if (!$text || !is_string($text)) {
            return $text;
        }
        
        return str_replace(
            array_keys(self::$arabicToPersianMap),
            array_values(self::$arabicToPersianMap),
            $text
        );
    }
    
    /**
     * Normalize array values recursively
     * Useful for normalizing file lists, API responses, etc.
     * @param mixed $data Data to normalize
     * @return mixed Data with normalized string values
     */
    public static function normalizeArrayValues($data) {
        if (is_string($data)) {
            return self::normalizeToPersian($data);
        }
        
        if (is_array($data)) {
            $normalized = [];
            foreach ($data as $key => $value) {
                $normalized[$key] = self::normalizeArrayValues($value);
            }
            return $normalized;
        }
        
        if (is_object($data)) {
            $normalized = clone $data;
            foreach ($normalized as $key => $value) {
                $normalized->$key = self::normalizeArrayValues($value);
            }
            return $normalized;
        }
        
        return $data;
    }
    
    /**
     * Get the Arabic to Persian character mapping
     * @return array Character mapping
     */
    public static function getCharacterMap() {
        return self::$arabicToPersianMap;
    }
}
