| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Services/ |
Upload File : |
<?php
namespace App\Services;
use App\Models\User;
use App\Models\Company;
use App\Models\Report;
use App\Models\Role;
use App\Models\Module;
class PublicIdService
{
/**
* Map of model classes to handle public ID resolution
*/
private const MODEL_MAP = [
'user' => User::class,
'company' => Company::class,
'report' => Report::class,
'role' => Role::class,
'module' => Module::class,
];
/**
* Find a model instance by its public ID
*/
public static function findByPublicId(string $modelType, string $publicId)
{
if (!isset(self::MODEL_MAP[$modelType])) {
return null;
}
$modelClass = self::MODEL_MAP[$modelType];
return $modelClass::findByPublicId($publicId);
}
/**
* Get the internal ID from a public ID (for internal use only)
* This method should only be used internally and never expose the internal ID
*/
public static function getInternalId(string $modelType, string $publicId): ?int
{
$model = self::findByPublicId($modelType, $publicId);
if (!$model) {
return null;
}
return $model->getKey(); // Returns the primary key value
}
/**
* Generate a public ID for a given model instance
*/
public static function generatePublicId($model): string
{
return $model->public_id;
}
/**
* Validate if a public ID format is correct
*/
public static function isValidPublicIdFormat(string $publicId): bool
{
// Public IDs should be 32 character hex strings
return preg_match('/^[a-f0-9]{32}$/', $publicId) === 1;
}
/**
* Find user by email (alternative to public ID for certain operations)
*/
public static function findUserByEmail(string $email): ?User
{
return User::where('email', $email)->first();
}
/**
* Find company by domain (alternative identifier)
*/
public static function findCompanyByDomain(string $domain): ?Company
{
return Company::where('domain', $domain)->first();
}
}