Skip to content

Commit 6e7c62e

Browse files
FEATURE: Use \hyphenations case-insensitive (like \patterns)
1 parent d47fe93 commit 6e7c62e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Syllable.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,30 @@ private function parseWord($word)
754754
return [$word];
755755
}
756756

757+
$wordLowerCased = mb_strtolower($word);
758+
757759
// Is it a pre-hyphenated word?
758-
if (isset($this->hyphenation[$word])) {
759-
return mb_split('-', $this->hyphenation[$word]);
760+
if (isset($this->hyphenation[$wordLowerCased])) {
761+
$hyphenation = $this->hyphenation[$wordLowerCased];
762+
$hyphenationLength = mb_strlen($hyphenation);
763+
$parts = [];
764+
$part = '';
765+
for ($i = 0, $j = 0; $i < $hyphenationLength; $i++) {
766+
if (mb_substr($hyphenation, $i, 1) !== '-') {
767+
$part .= mb_substr($word, $j++, 1);
768+
} else {
769+
$parts[] = $part;
770+
$part = '';
771+
}
772+
}
773+
if (!empty($part)) {
774+
$parts[] = $part;
775+
}
776+
return $parts;
760777
}
761778

762779
// Convenience array
763-
$text = '.'.mb_strtolower($word).'.';
780+
$text = '.'.$wordLowerCased.'.';
764781
$textLength = $wordLength + 2;
765782
$patternLength = $this->maxPattern < $textLength
766783
? $this->maxPattern

tests/src/SyllableTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,13 @@ public function testCaseInsensitivity()
638638
{
639639
$this->object->setHyphen('-');
640640

641+
// Patterns
641642
$this->assertEquals(['IN', 'EX', 'PLIC', 'A', 'BLE'], $this->object->splitText('INEXPLICABLE'));
642643
$this->assertEquals(['in', 'ex', 'plic', 'a', 'ble'], $this->object->splitText('inexplicable'));
644+
645+
// Hyphenations
646+
$this->assertEquals(['as', 'so', 'ciate'], $this->object->splitText('associate'));
647+
$this->assertEquals(['AS', 'SO', 'CIATE'], $this->object->splitText('ASSOCIATE'));
643648
}
644649

645650
/**

0 commit comments

Comments
 (0)