Skip to content

Instantly share code, notes, and snippets.

@azimidev
Last active July 11, 2022 01:51
Show Gist options
  • Select an option

  • Save azimidev/97c00d715fee2ed8b60f876e48b2b27d to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Thanks

@alazar-tekle

alazar-tekle commented Mar 8, 2022

Copy link
Copy Markdown

Thanks. It works perfectly.

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