Skip to content

Instantly share code, notes, and snippets.

@azimidev
Last active July 11, 2022 01:51
Show Gist options
  • Save azimidev/97c00d715fee2ed8b60f876e48b2b27d to your computer and use it in GitHub Desktop.
Save azimidev/97c00d715fee2ed8b60f876e48b2b27d to your computer and use it in GitHub Desktop.
Calculates how much percentage of a profile is completed. It stripes off timestamps like created_at updated_at and primary keys like ids. Usually beneficial when using Laravel
<?php
/**
* Calculate how much a profile is completed
*
* @param $profile
* @return float|int
*/
function calculate_profile($profile)
{
if ( ! $profile) {
return 0;
}
$columns = preg_grep('/(.+ed_at)|(.*id)/', array_keys($profile->toArray()), PREG_GREP_INVERT);
$per_column = 100 / count($columns);
$total = 0;
foreach ($profile->toArray() as $key => $value) {
if ($value !== NULL && $value !== [] && in_array($key, $columns)) {
$total += $per_column;
}
}
return $total;
}
@kingRayhan
Copy link

Thanks

@alazar-tekle
Copy link

alazar-tekle commented Mar 8, 2022

Thanks. It works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment