Lithe SwissHelper is a comprehensive PHP utility library designed to simplify common programming tasks. It provides a collection of helper functions for string manipulation, array handling, data validation, and more, all designed with a focus on developer experience and code readability.
Install the package via Composer:
composer require lithephp/swisshelperThe now() function provides a simple interface for datetime manipulation.
// Get current DateTime object
$datetime = now();
// Get formatted current date
$formatted = now('Y-m-d H:i:s');
$date = now('Y-m-d');
$time = now('H:i:s');The string helper provides various methods for string manipulation.
$slug = str('Hello World!')->slug();
// Output: "hello-world"
$slug = str('Café com leite')->slug();
// Output: "cafe-com-leite"$text = str('Café à la crème')->removeAccents();
// Output: "Cafe a la creme"$numbers = str('Phone: (123) 456-7890')->onlyNumbers();
// Output: "1234567890"
$numbers = str('Order #123-456')->onlyNumbers();
// Output: "123456"// CPF (Brazilian ID)
$masked = str('12345678901')->mask('###.###.###-##');
// Output: "123.456.789-01"
// Phone number
$masked = str('1234567890')->mask('(##) ####-####');
// Output: "(12) 3456-7890"
// Custom mask
$masked = str('ABC123')->mask('???-###');
// Output: "ABC-123"The array helper provides methods for array manipulation and access.
$array = [
'user' => [
'profile' => [
'name' => 'John Doe',
'settings' => [
'theme' => 'dark'
]
]
]
];
// Access with dot notation
$name = arr($array)->get('user.profile.name');
// Output: "John Doe"
// With default value
$color = arr($array)->get('user.profile.color', 'blue');
// Output: "blue"$array = ['name' => 'John', 'age' => 30, 'email' => 'john@example.com'];
// Get only specific keys
$only = arr($array)->only(['name', 'email']);
// Output: ['name' => 'John', 'email' => 'john@example.com']
// Get everything except specified keys
$except = arr($array)->except(['age']);
// Output: ['name' => 'John', 'email' => 'john@example.com']The validation helper provides methods for validating different types of data.
validate('email@example.com')->email(); // true
validate('invalid-email')->email(); // falsevalidate('https://example.com')->url(); // true
validate('invalid-url')->url(); // falsevalidate('192.168.1.1')->ip(); // true
validate('256.256.256.256')->ip(); // falsevalidate('2024-01-03')->date(); // true
validate('2024-13-45')->date(); // false
// Custom format
validate('03/01/2024')->date('d/m/Y'); // truevalidate('John Doe')->name(); // true
validate('John123')->name(); // falsevalidate('4532015112830366')->creditCard(); // true
validate('1234567890123456')->creditCard(); // false// Validate password strength
validate('P@ssw0rd')->password(); // true
validate('weak')->password(); // false
// Custom rules
validate('1234Abcd!')->password(8, false, true, true, true); // true// Check if age is within a range
validate('2000-01-01')->age(18, 60); // true
validate('2010-01-01')->age(18); // false// Check if string or numeric value is between limits
validate('Hello')->between(3, 10); // true
validate(100)->between(50, 150); // true// Check if string contains a specific substring
validate('Hello World')->contains('World'); // true
// Check if string starts or ends with specific text
validate('example.com')->startsWith('example'); // true
validate('example.com')->endsWith('.com'); // trueThe random() function generates random strings of various types.
// Generate a random alphanumeric string of 16 characters
$alnum = random(16);
// Generate a random alphabetic string of 10 characters
$alpha = random(10, 'alpha');
// Generate a random numeric string of 8 characters
$numeric = random(8, 'numeric');
// Generate a random non-zero numeric string of 6 characters
$nozero = random(6, 'nozero');The URL helper provides methods for handling URLs.
// Get the current URL
$current = url()->current();
// Get the base URL
$base = url()->base();
// Generate a URL to a specific path
$path = url()->to('path/to/resource');
// Get the previous URL (referer)
$previous = url()->previous();The session helper provides methods for managing session data.
// Set a session variable
session()->put('key', 'value');
// Get a session variable
$value = session()->get('key');
// Check if a session variable exists
$exists = session()->has('key');
// Get all session variables
$all = session()->all();
// Unset a session variable
session()->forget('key');
// Destroy the session
session()->destroy();$token = csrf_token();CSRF Hidden Field
$field = csrf_field();
// Output: <input type="hidden" name="_token" value="your_csrf_token_here">Contributions are welcome and will be fully credited. Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
The MIT License (MIT). Please see License File for more information.
Create an issue in the GitHub repository.