PHP code example of websitesql / utilities

1. Go to this page and download the library: Download websitesql/utilities library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

websitesql / utilities example snippets


use WebsiteSQL\Utilities\Utilities;

// Access different factories
$uuid = Utilities::uuid();
$string = Utilities::string('Hello World');
$datetime = Utilities::datetime('2023-10-10');

// Generate a version 4 UUID
$uuid = Utilities::uuid()->generate('4')->toString();
// Output: e.g. "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// Generate a version 1 UUID
$uuid = Utilities::uuid()->generate('1')->toString();

// Create from an existing UUID string
$uuid = Utilities::uuid()->fromString('f47ac10b-58cc-4372-a567-0e02b2c3d479');

// Generate a random string
$randomString = Utilities::string()->random(12, 'alphanumeric')->toString();
// Output: e.g. "a1B2c3D4e5F6"

// Convert any string to a slug
$slug = Utilities::string('Hello World!')->slugify()->toString();
// Output: "hello-world"

// Encrypt a string
$secret = 'my-secret-key';
$encrypted = Utilities::string('sensitive data')->encrypt($secret)->toString();

// Decrypt a string
$decrypted = Utilities::string($encrypted)->decrypt($secret)->toString();
// Output: "sensitive data"

// Truncate a long string
$truncated = Utilities::string('This is a very long text that should be truncated')
    ->truncate(20, '...')
    ->toString();
// Output: "This is a very long..."

// Format a date
$date = Utilities::datetime('2023-10-15')->format('Y-m-d');
// Output: "2023-10-15"

// Get time ago
$timeAgo = Utilities::datetime('2023-01-15')->timeAgo();
// Output: e.g. "9 months ago"

// Chain operations
$nextMonth = Utilities::datetime()
    ->modify('+1 month')
    ->setTimezone('America/New_York')
    ->format('Y-m-d H:i:s');

// Sample data
$data = [
    ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane Smith', 'email' => '[email protected]'],
    // More items...
];

// Basic pagination
$result = Utilities::pagination($data)
    ->paginate(0, 10)
    ->getResult();

// Search and paginate
$result = Utilities::pagination($data)
    ->search('john', ['name', 'email'])
    ->paginate(0, 5)
    ->getResult();

// Parse cookies from a header
$cookies = Utilities::security()->parseCookies('name=value; session=abc123');

// Generate a cookie header
$cookieHeader = Utilities::security()->generateCookieHeader('session', 'abc123', [
    'domain' => 'example.com',
    'expires' => new DateTime('+1 day'),
    'secure' => true
]);

// Hash and verify passwords
$hash = Utilities::security()->hashPassword('my-secure-password');
$valid = Utilities::security()->verifyPassword('my-secure-password', $hash);

// Generate and validate CSRF tokens
$token = Utilities::security()->generateCSRFToken();
$isValid = Utilities::security()->validateCSRFToken($userToken, $storedToken);

// Read a file
$content = Utilities::file('/path/to/file.txt')->read();

// Write to a file
Utilities::file('/path/to/output.txt')
    ->open(null, 'w')
    ->write('Hello World!')
    ->close();

// Read a CSV file with headers
$data = Utilities::file('/path/to/data.csv')->readCsv(true);

// Write data to a CSV file
$data = [
    ['name' => 'John', 'email' => '[email protected]'],
    ['name' => 'Jane', 'email' => '[email protected]']
];
Utilities::file('/path/to/output.csv')->writeCsv($data);

$validator = Utilities::validation();

// Validate an email
$isValid = $validator->email('[email protected]');

// Validate form data
if (
    $validator->ors = $validator->getErrors();
    // Handle errors
}