Utility functions for common web development tasks including string/array transformations and secure cookie handling.
Overview
The Helper class provides static utility methods for converting between different naming conventions and transforming data structures. It's particularly useful for API integrations and data processing where you need to convert between camelCase and snake_case formats.
Class Reference
Static Methods
camelToSnake(string $input): string
Converts camelCase strings to snake_case.
Parameters:
$input (string) - The camelCase string to convert
Returns: The converted snake_case string
Example:
use Stilmark\Base\Helper;$result=Helper::camelToSnake('firstName');// Returns: 'first_name'$result=Helper::camelToSnake('userAccountId');// Returns: 'user_account_id'$result=Helper::camelToSnake('XMLHttpRequest');// Returns: 'x_m_l_http_request'
snakeToCamel(string $input): string
Converts snake_case strings to CamelCase (PascalCase).
Parameters:
$input (string) - The snake_case string to convert
Returns: The converted CamelCase string
Example:
arrayKeysCamelToSnake(array $array): array
Recursively converts all array keys from camelCase to snake_case.
Parameters:
$array (array) - The array with camelCase keys to convert
Returns: Array with snake_case keys
Example:
Cookie Handling
Secure methods for managing HTTP cookies with security best practices.
// After successful login
$token = Jwt::generate(['user_id' => 123]);
Helper::setJwtCookie($token, [
'expires' => 86400, // 1 day
'sameSite' => 'Strict'
]);
// In subsequent requests
if ($jwt = Helper::getCookie('jwt')) {
try {
$user = Jwt::validate($jwt);
// User is authenticated
} catch (Exception $e) {
// Handle invalid token
Helper::deleteCookie('jwt');
}
}
use Stilmark\Base\Helper;
class UserController extends Controller
{
public function store()
{
$userData = $this->request->json();
// Convert camelCase keys to snake_case for database
$dbData = Helper::arrayKeysCamelToSnake($userData);
// Save to database with snake_case column names
$user = User::create($dbData);
return $this->json(['success' => true, 'user' => $user]);
}
}
class UserService
{
public function getUserProfile(int $userId): array
{
// Get user data from database (snake_case columns)
$userData = DB::table('users')
->select('first_name', 'last_name', 'email_address', 'created_at')
->where('user_id', $userId)
->first();
// Convert to camelCase for API response
return $this->snakeToCamelKeys($userData);
}
private function snakeToCamelKeys(array $data): array
{
$converted = [];
foreach ($data as $key => $value) {
// Convert to camelCase (lowercase first letter)
$camelKey = lcfirst(Helper::snakeToCamel($key));
$converted[$camelKey] = $value;
}
return $converted;
}
}
class ContactController extends Controller
{
public function submitForm()
{
$formData = $this->request->post();
// Normalize all keys to snake_case
$normalizedData = Helper::arrayKeysCamelToSnake($formData);
// Validate and process
$validator = new FormValidator($normalizedData);
if ($validator->isValid()) {
$this->saveContactForm($normalizedData);
return $this->json(['status' => 'success']);
}
return $this->json(['status' => 'error', 'errors' => $validator->getErrors()], 400);
}
}
class ConfigMapper
{
public static function mapToExternalApi(array $config): array
{
// Convert internal snake_case config to external camelCase API
$mapped = [];
foreach ($config as $key => $value) {
$camelKey = lcfirst(Helper::snakeToCamel($key));
$mapped[$camelKey] = $value;
}
return $mapped;
}
public static function mapFromExternalApi(array $apiData): array
{
// Convert external camelCase to internal snake_case
return Helper::arrayKeysCamelToSnake($apiData);
}
}
class DataTransformer
{
private array $pipeline = [];
public function addStep(callable $step): self
{
$this->pipeline[] = $step;
return $this;
}
public function transform(array $data): array
{
foreach ($this->pipeline as $step) {
$data = $step($data);
}
return $data;
}
public static function create(): self
{
return new self();
}
}
// Usage
$transformer = DataTransformer::create()
->addStep([Helper::class, 'arrayKeysCamelToSnake'])
->addStep(function($data) {
// Additional custom transformation
return array_filter($data, fn($value) => $value !== null);
});
$result = $transformer->transform($inputData);