How to decline a full name into genitive?

Is there an algorithm for declining the last name? in PHP into genitive case?

Found algorithms for declension of full name, but not in the genitive or not for PHP. Ideally, 2 different solutions are better (simpler and more difficult).

Answer 1, authority 100%

I’m also interested in this topic. copied the first code, added from the bottom:

$r = new NCLNameCaseRu();
echo $r->getFormatted(2);

similarly: Cherepanshina Rafael Michelangelovich.

Answer 2, authority 100%

The encoding with the class file must be in ANSI.
The class is called like this:

include ("NCLNameCaseRu.php");
$r = new NCLNameCaseRu();
$r->setFirstName(iconv('cp1251', 'utf-8', ''));
$r->setLastName(iconv('cp1251', 'utf-8', ''));
$r->setFatherName(iconv('cp1251', 'utf-8', ''));
echo iconv('utf-8', 'cp1251', $r->getFormatted(2));

Provided that the document where the class is called is cp1251 encoded. Checked, everything works!