Last active
July 11, 2022 01:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks