Helper

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:

Secure methods for managing HTTP cookies with security best practices.

setCookie(string $name, string $value, array $options = []): bool

Set a secure HTTP cookie with configurable options.

Parameters:

  • $name (string) - Cookie name

  • $value (string) - Cookie value

  • $options (array) - Optional settings:

    • expires (int) - Expiration time in seconds (default: 0 for session cookie)

    • path (string) - Cookie path (default: '/')

    • domain (string) - Cookie domain (default: current domain)

    • secure (bool) - Only send over HTTPS (default: true in production)

    • httpOnly (bool) - Make cookie inaccessible to JavaScript (default: true)

    • sameSite (string) - CSRF protection: 'Lax', 'Strict', or 'None' (default: 'Lax')

Example:

setJwtCookie(string $jwt, array $options = []): bool

Convenience method for setting JWT cookies with secure defaults.

Parameters:

  • $jwt (string) - The JWT token

  • $options (array) - Same as setCookie() plus:

    • name (string) - Cookie name (default: 'jwt')

Example:

getCookie(string $name, $default = null)

Get a cookie value by name.

Parameters:

  • $name (string) - Cookie name

  • $default (mixed) - Default value if cookie doesn't exist

Returns: Cookie value or default

Example:

deleteCookie(string $name, array $options = []): bool

Delete a cookie by setting its expiration to the past.

Parameters:

  • $name (string) - Cookie name

  • $options (array) - Must match options used when setting the cookie

Example:

Usage Patterns

API Response Transformation

Convert API responses from camelCase to snake_case for database storage:

Database to API Response

Convert database results to camelCase for API responses:

Form Data Processing

Process form submissions with mixed naming conventions:

Configuration Mapping

Map configuration keys between different systems:

Advanced Usage

Custom Conversion Pipeline

Create a data transformation pipeline:

Batch Processing

Process multiple arrays with consistent key conversion:

Performance Considerations

Caching Conversions

For frequently converted strings, consider caching:

Memory Optimization

For large arrays, consider processing in chunks:

Testing

Unit Tests

Security Considerations

  1. Always use secure cookies in production:

    • Set secure flag to true (default)

    • Always use httpOnly flag (default)

    • Set appropriate sameSite attribute (default: 'Lax')

    • Keep expiration times reasonable

  2. JWT in Cookies:

    • Always use httpOnly cookies for JWT storage

    • Set appropriate sameSite policy based on your needs

    • Consider using refresh tokens for better security

    • Implement proper token expiration and renewal

Best Practices

  1. Consistency: Choose one naming convention and stick with it throughout your application

  2. API Boundaries: Convert data at API boundaries rather than throughout your application

  3. Performance: Cache frequently converted strings for better performance

  4. Validation: Validate data after conversion to ensure integrity

  5. Documentation: Document your naming conventions and conversion patterns

Common Use Cases

  • API Integration: Converting between different API naming conventions

  • Database Mapping: Mapping between database column names and object properties

  • Form Processing: Normalizing form field names

  • Configuration Management: Converting configuration keys between systems

  • Data Import/Export: Transforming data formats during import/export operations

Last updated