Skip to content

Instantly share code, notes, and snippets.

@adadgio
Created January 3, 2023 13:10
Show Gist options
  • Save adadgio/9ad12ef81a3a4c6b79d5ddb98aa82ece to your computer and use it in GitHub Desktop.
Save adadgio/9ad12ef81a3a4c6b79d5ddb98aa82ece to your computer and use it in GitHub Desktop.
<?php
namespace Medical\CoreBundle\Component\Tool;
class CreatinineCalculator
{
const MDRD = 'MDRD';
const COCKROFT = 'COCKROFT';
/**
* @return float
*/
public static function compute($type, array $input)
{
if ($type === self::MDRD) {
return self::computeMdrd($input);
} else if ($type === self::COCKROFT) {
return self::computeCockroft($input);
} else {
throw new \Exception('Cannot compute: allowed values are MDRD or COCKROFT');
}
}
/**
* @return float
*/
public static function computeCockroft(array $input)
{
$k = ($input['sex'] === 'H') ? 1.23 : 1.04;
// quick check if age is empty, it must be computed
// from birthyear. Otherwise we onnly need the age so go on
$input['age'] = self::validateAgeInput($input);
// base creatinimie value must be in umol/L (id normalize mg/L input(s))
$creat = self::toUmolCockroft($input);
$cockroft = ( ((140 - $input['age']) / $creat) * floatval($input['weight']) * floatval($k) );
$cockroft = round($cockroft, 2);
return $cockroft;
}
/**
* @return float
*/
public static function computeMdrd(array $input)
{
// two different multipliers must be applied to the base result
// depending on sex (male, female) or afro origin.
// If not, assign a neutral multiplier to m1 and m2 (ie. 1)
$m1 = ($input['sex'] === 'F') ? 0.742 : 1;
$m2 = ($input['originAfro'] === true) ? 1.212 : 1;
// base creatinimie value must be in umol/L (id normalize mg/L input(s))
$creat = self::toUmolMdrd($input);
$mdrd = 186 * pow( ($creat * 0.0113), -1.154) * pow($input['age'], -0.203) * $m1 * $m2;
// @TODO A vérifier absolument !
if ($input['creatinimieType'] === 'mg/L') {
$mdrd = $mdrd * pow(88.5,-1.154);
}
return round($mdrd, 2);
}
public static function birthyearToAge($year)
{
$year = (int) $year;
$now = new \DateTime();
$fulldate = $year.'-01-01 00:00:01';
$birthday = new \DateTime($fulldate);
$interval = $now->diff($birthday);
$age = $interval->format('%y');
return $age;
}
public static function validateAgeInput(array $input)
{
if (empty($input['age']) && empty($input['year'])) {
throw new \Exception('Only one of these fields can be empty: year or age');
}
return empty($input['age']) ? self::birthyearToAge($input['year']) : $input['age'];
}
public static function toUmolCockroft(array $input)
{
// base creatinimie value must be in umol/L (id normalize mg/L input(s))
if ($input['creatinimieType'] === 'mg/L') {
$creat = ($input['creatinimie'] / 10) * 88.4;
} else {
// value already in umol/L. Nice !
$creat = $input['creatinimie'];
}
return $creat;
}
public static function toUmolMdrd(array $input)
{
// base creatinimie value must be in umol/L (id normalize mg/L input(s))
if ($input['creatinimieType'] === 'mg/L') {
$creat = $input['creatinimie'] / 10;
} else {
// value already in umol/L. Nice !
$creat = $input['creatinimie'];
}
return $creat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment