<commit_before><?php

namespace Evocatio\Bundle\CoreBundle\Lib;

class Slugifier {

    public function slugifiyRequest($requestT, $fields) {
        $requestT = array_pop($requestT);

        $this->changeSlugs($requestT, $fields);
        return $requestT;
    }

    /**
     * recursive look into the ArrayFiles to find all ["uploads"], and transform them to ["files"] and merge them with arrayRequest.
     * It changes the uploadedFiles to array's and moves the files to a new location configured in %secure_upload_dir%
     * @param type $arrayFile
     * @param type $arrayRequest
     */
    private function changeSlugs(&$arrayRequest, &$fields) {
        if (array_key_exists("slug", $arrayRequest)) {
            $preSlugArray = null;
            foreach ($fields as $key => $field) {
                $preSlugArray[] .= trim($arrayRequest[$field]);
            }

            $slug = (substr($slug = $this->slugifyString(implode("-", $preSlugArray)), -1) == "-") ? substr($slug, 0, -1) : $slug;
            $arrayRequest["slug"] = ($arrayRequest["slug"] == "") ? $slug : $this->slugifyString($arrayRequest["slug"]);
        }
        foreach ($arrayRequest as $key => &$child) {
            if (is_array($child)) {
                $this->changeSlugs($child, $fields);
//                unset($child);
            }
        }
    }

    public function slugifyString($string, $separator = "-") {
        $string = $this->normalizeUtf8String($string);

        return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', $separator, preg_replace('/([a-z\d])([A-Z])/', '\1_\2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', preg_replace('/::/', '/', $string)))));
    }

    private function normalizeUtf8String($s) {
        $original_string = $s;

        // Normalizer-class missing!
        if (!class_exists("Normalizer", $autoload = false))
            return $original_string;


        // maps German (umlauts) and other European characters onto two characters before just removing diacritics
        $s = preg_replace('@\x{00c4}@u', "AE", $s);    // umlaut Ä => AE
        $s = preg_replace('@\x{00d6}@u', "OE", $s);    // umlaut Ö => OE
        $s = preg_replace('@\x{00dc}@u', "UE", $s);    // umlaut Ü => UE
        $s = preg_replace('@\x{00e4}@u', "ae", $s);    // umlaut ä => ae
        $s = preg_replace('@\x{00f6}@u', "oe", $s);    // umlaut ö => oe
        $s = preg_replace('@\x{00fc}@u', "ue", $s);    // umlaut ü => ue
        $s = preg_replace('@\x{00f1}@u', "ny", $s);    // ñ => ny
        $s = preg_replace('@\x{00ff}@u', "yu", $s);    // ÿ => yu
        // maps special characters (characters with diacritics) on their base-character followed by the diacritical mark
        // exmaple:  Ú => U´,  á => a`
        $s = \Normalizer::normalize($s, \Normalizer::FORM_D);


        $s = preg_replace('@\pM@u', "", $s);    // removes diacritics<commit_msg>Ajustement<commit_after><?php

namespace Evocatio\Bundle\CoreBundle\Lib;

class Slugifier {

    public function slugifiyRequest($requestT, $fields) {
        $requestT = array_pop($requestT);

        $sluggify = function (&$array, $fields) use (&$sluggify) {
                    if (array_key_exists("slug", $array)) {
                        $preSlugArray = null;
                        foreach ($fields as $field)
                            $preSlugArray[] .= trim($array[$field]);

                        $slug = (substr($slug = $this->slugifyString(implode("-", $preSlugArray)), -1) == "-") ? substr($slug, 0, -1) : $slug;
                        $array["slug"] = ($array["slug"] == "") ? $slug : $this->slugifyString($array["slug"]);
                    }

                    foreach ($array as &$child) {
                        if (is_array($child))
                            $sluggify($child, $fields);
                    }
                };

        $sluggify($requestT, $fields);

        return $requestT;
    }

    /**
     * recursive look into the ArrayFiles to find all ["uploads"], and transform them to ["files"] and merge them with arrayRequest.
     * It changes the uploadedFiles to array's and moves the files to a new location configured in %secure_upload_dir%
     * @param type $arrayFile
     * @param type $arrayRequest
     */
//    private function changeSlugs($arrayRequest, &$fields) {
//        if (array_key_exists("slug", $arrayRequest)) {
//            $preSlugArray = null;
//            foreach ($fields as $key => $field) {
//                $preSlugArray[] .= trim($arrayRequest[$field]);
//            }
//
//            $slug = (substr($slug = $this->slugifyString(implode("-", $preSlugArray)), -1) == "-") ? substr($slug, 0, -1) : $slug;
//            $arrayRequest["slug"] = ($arrayRequest["slug"] == "") ? $slug : $this->slugifyString($arrayRequest["slug"]);
//        }
//        foreach ($arrayRequest as $key => &$child) {
//            if (is_array($child)) {
//                $this->changeSlugs($child, $fields);
////                unset($child);
//            }
//        }
//    }

    public function slugifyString($string, $separator = "-") {
        $string = $this->normalizeUtf8String($string);

        return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', $separator, preg_replace('/([a-z\d])([A-Z])/', '\1_\2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', preg_replace('/::/', '/', $string)))));
    }

    private function normalizeUtf8String($s) {
        $original_string = $s;

        // Normalizer-class missing!
        if (!class_exists("Normalizer", $autoload = false))
            return $original_string;


        // maps German (umlauts) and other European characters onto two characters before just removing diacritics
        $s = preg_replace('@\x{00c4}@u', "AE", $s);    // umlaut Ä => AE
        $s = preg_replace('@\x{00d6}@u', "OE", $s);    // umlaut Ö => OE
        $s = preg_replace('@\x{00dc}@u', "UE", $s);    // umlaut Ü => UE
        $s = preg_replace('@\x{00e4}@u', "ae", $s);    // umlaut ä => ae
        $s = preg_replace('@\x{00f6}@u', "oe", $s);    // umlaut ö => oe
        $s = preg_replace('@\x{00fc}@u', "ue", $s);    // umlaut ü => ue
        $s = preg_replace('@\x{00f1}@u', "ny", $s);    // ñ => ny
        $s = preg_replace('@\x{00ff}@u', "yu", $s);    // ÿ => yu
        // maps special characters (characters with diacritics) on their base-character followed by the diacritical mark
        // exmaple:  Ú => U´,  á => a`
        $s = \Normalizer::normalize($s, \Normalizer::FORM_D);


        $s = preg_replace('@\pM@u', "", $s);    // removes diacritics
{'fraction_non_alphanumeric': 0.19288875324278956, 'fraction_numerical': 0.010224324736761789, 'mean_word_length': 2.23334977799704, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '818780', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2168, 'n_words': 475}