Skip to content

Instantly share code, notes, and snippets.

@thehelvetian
Created July 5, 2018 09:54
Show Gist options
  • Select an option

  • Save thehelvetian/812edac83185e27080d13dbc5a045c9e to your computer and use it in GitHub Desktop.

Select an option

Save thehelvetian/812edac83185e27080d13dbc5a045c9e to your computer and use it in GitHub Desktop.
Calculates the user assets path based on the user ID
<?php
namespace TheHelvetian\Helpers;
use Carbon\Carbon;
/**
* Class UserFileHelper
* @package TheHelvetian\Helpers
*/
class UserFileHelper
{
/**
* Calculates the user assets path based on the user ID
*
* @param int $user_id The user ID
*
* @return string
*/
public static function getUserAssetsPath($user_id)
{
$pad_length = 10;
$user_assets_path = '';
$user_id_full = $user_id;
$user_id_length = strlen($user_id);
if ($user_id_length < $pad_length) {
// Fill path with leading zeroes
$user_id_full = number_format('1'.str_pad($user_id_full, $pad_length-1, '0', STR_PAD_LEFT));
$user_id_full[0] = '0';
} else {
$user_id_full = number_format($user_id_full);
}
$path_parts = explode(',', $user_id_full);
foreach ($path_parts as $part) {
$user_assets_path .= $part.'/';
}
$user_assets_path .= $user_id;
return $user_assets_path;
}
/**
* Calculates today's folder name within the user assets path based on the user ID
*
* @param $user_id
* @return string
*/
public static function getTodayAssetsPath($user_id)
{
$user_assets_path = self::getUserAssetsPath($user_id);
return $user_assets_path.'/'.Carbon::now()->toDateString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment