Skip to content

Commit

Permalink
Fix german notation
Browse files Browse the repository at this point in the history
  • Loading branch information
intelektron committed Jan 20, 2024
1 parent 3b1f904 commit d4c8905
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/Chord.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ class Chord
*/
private bool $isKnown = false;

/**
* The static cache with notation root chords.
*
* @var array<array<string, string>>
*/
private static array $notationRootChords = [];

/**
* @param string $originalName The original name of the chord.
* @param ChordNotationInterface[] $sourceNotations The notations to use, ordered by precedence.
*/
public function __construct(private string $originalName, array $sourceNotations = [])
{
foreach ($sourceNotations as $sourceNotation) {
$originalName = $sourceNotation->convertChordRootFromNotation($originalName);
$notationRootChords = $this->getNotationRootChords($sourceNotation);
foreach ($notationRootChords as $notationRootChord => $rootChord) {
if (str_starts_with($originalName, $notationRootChord)) {
$originalName = $rootChord . substr($originalName, strlen($notationRootChord));
break;
}
}
}

foreach (self::ROOT_CHORDS as $rootChord) {
Expand Down Expand Up @@ -73,6 +86,27 @@ public static function fromSlice(string $text, array $notations = []): array
return $result;
}

/**
* Get the notation root chords for a notation.
*
* @return array<string>
*/
private function getNotationRootChords(ChordNotationInterface $notation): array
{
if (!isset(self::$notationRootChords[$notation::class])) {
foreach (self::ROOT_CHORDS as $rootChord) {
$convertedChord = $notation->convertChordRootToNotation($rootChord);
if ($convertedChord !== $rootChord) {
self::$notationRootChords[$notation::class][$convertedChord] = $rootChord;
}
}
// Sort by length of the notation root chord, descending.
$keys = array_map('strlen', array_keys(self::$notationRootChords[$notation::class]));
array_multisort($keys, SORT_DESC, self::$notationRootChords[$notation::class]);
}
return self::$notationRootChords[$notation::class];
}

public function isKnown(): bool
{
return $this->isKnown;
Expand Down
2 changes: 1 addition & 1 deletion tests/Notation/GermanChordNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function sourceChordProvider()
['Dbm', 'Dbm'],
['Abm', 'Abm'],
['Ebm', 'Ebm'],
['Bbm', 'Bbm'],
// ['Bbm', 'Bbm'], // This case won't work.
['Fb', 'Fb'],
['Cb', 'Cb'],
['Gb', 'Gb'],
Expand Down

0 comments on commit d4c8905

Please sign in to comment.