Exemple #1
0
 private function replacePhrases($data, $phrases, Progress $progress, float $progressModifier)
 {
     $phrases = array_filter($phrases, function ($value) {
         return !empty($value);
     });
     usort($phrases, function ($a, $b) use($data) {
         $aUses = substr_count($data, $a) * strlen($a);
         $bUses = substr_count($data, $b) * strlen($b);
         return $bUses <=> $aUses;
     });
     $replacements = [];
     $i = 0;
     foreach ($phrases as $phrase) {
         $i++;
         $progress->notify($i / count($phrases) + $progressModifier);
         if (!strstr($data, $phrase)) {
             // This phrase isn't even in the data. Skip it
             continue;
         }
         $key = $this->findKey($data);
         if (strlen($phrase) <= strlen($key)) {
             // Do not bother if the key is as big as the word
             $this->freeKey($key);
             continue;
         }
         $usages = substr_count($data, $phrase);
         $usedSpace = $usages * strlen($phrase);
         $willUse = $usages * strlen($key) + strlen($key) + strlen($phrase) + 1;
         if ($willUse >= $usedSpace) {
             // Do not bother if the extra meta data means its overall larger with replacement
             $this->freeKey($key);
             continue;
         }
         $replacements[$phrase] = $key;
     }
     return $replacements;
 }