PHP code example of corex / support

1. Go to this page and download the library: Download corex/support 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/ */

    

corex / support example snippets


// Generate key based on string + array.
$key = Cache::key('test', ['param1' => 'Something']);

// Set path for cache stores.
Cache::path('/path/cache/stores');

// Set lifetime for cache in seconds.
Cache::lifetime(600);

// Set lifetime for cache in minutes.
Cache::lifetime('60m');

// Set lifetime for cache in hours.
Cache::lifetime('1h');

// Get from cache from 'custom-store'.
$data = Cache::get('test', 'default.value', 'custom-store');

// Put data in cache to 'custom-store'.
Cache::put('test', 'data', 'custom-store');

// Flush cache 'custom-store'.
Cache::flush('custom-store');

// Writeln text.
Console::writeln('this is a test');

// Writeln texts.
Console::writeln(['this is a test', 'this is line 2']);

// Show header.
Console::header('this is a test');

// Ask question.
$answer = Console::ask('Enter name');

// Enter password.
$password = Console::secret('Enter password');

// Show table.
Console::table($items, ['Header 1', 'Header 2']);

// Throw error (exception).
Console::throwError('this is an error');

// Test if directory exists.
$exist = Directory::exist('/my/path');

// Check if directory is writeable.
$isWriteable = Directory::isWritable('/my/path');

// Make directory.
Directory::make('/my/path');

// Get entries of a directory.
$entries = Directory::entries('/my/path', '*', true, true, true);

// Check if file exists.
$exist = File::exist($filename);

// Get from file.
$content = File::get($filename);

// Load lines.
$lines = File::getLines($filename);

// Save content.
File::put($filename, $content);

// Save lines.
File::putLines($filename, $lines);

// Get stub.
$stub = File::getStub($filename, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Get template.
$template = File::getTemplate($filename, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Get json.
$array = File::getJson($filename);

// Put json.
File::putJson($filename, $array);

// Get temp filename.
$filename = File::getTempFilename();

// Delete file.
File::delete($filename);

// Get base url.
$baseUrl = Input::getBaseUrl();

// Get user agent.
$userAgent = Input::getUserAgent();

// Get remote address.
$remoteAddress = Input::getRemoteIp();

// Get headers.
$headers = Input::getHeaders();

// Get root of project.
$pathRoot = Path::root();

// Get config-path of project-root.
$pathConfig = Path::root(['config']);

// Get name of package.
$package = Path::packageName();

// Get name of vendor.
$package = Path::vendorName();

// Set session variable.
Session::set('actor', 'Roger Moore');

// Get session variable.
$actor = Session::get('actor');

// Check if session variable exists.
if (!Session::has('actor')) {
}

// Create csrf token.
$csrfToken = Token::create('csrf');

// Check csrf token.
if (!Token::isValid($csrfToken)) {
}

// Get firstname from array via dot notation.
$firstname = Arr::get($array, 'actor.firstname');

// Set firstname on array via dot notation.
Arr::set($array, 'actor.firstname', $firstname);

// Pluck firstnames from list of actors.
$firstnames = Arr::pluck($actors, 'firstname');

// Update each element in collection.
$collection = new Collection($actors);
$collection->each(function (&$actor) {
    $actor->firstname = 'Mr. ' . $actor->firstname;
});

// Get sum of value.
$collection = new Collection($values);
$sum = $collection->sum('amount');

// Loop through actors.
$collection = new Collection($actors);
foreach ($collection => $actor) {
    var_dump($actor->firstname);
};

// Get last element.
$collection = new Collection($actors);
$lastElement = $collection->last();

// Get json.
$bag = new Bag();
$bag->set('actor.firstname', 'Roger');

// Get firstname of actor using dot notation.
$firstname = $bag->get('actor.firstname');

class BaseProperties extends \CoRex\Support\Base\BaseProperties
{
    private $privateValue;
    protected $protectedValue;
    public $publicValue;
}
$properties = new BaseProperties([
    'privateValue' => 'something',
    'protectedValue' => 'something',
    'publicValue' => 'something'
]);

// Get first 4 characters of string.
$left = Str::left($string, 4);

// Check if string starts with 'Test'.
$startsWith = Str::startsWith($string, 'Test');

// Limit text to 20 characters with '...' at the end.
$text = Str::limit($text, 20, '...');

// Replace tokens.
$text = Str::replaceToken($text, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Create a unique string.
$identifier = Str::unique();

// Convert to pascal case.
$data = Convention::pascalCase($data);

// Convert to camel case.
$data = Convention::camelCase($data);

// Convert to snake case.
$data = Convention::snakeCase($data);

// Convert to kebab case.
$data = Convention::kebabCase($data);

// Add 'test' to string with separator '|'.
$string = StrList::add($string, 'test', '|');

// Remove 'test' from string.
$string = StrList::remove($string, 'test', '|');

// Check if 'test' exist in string.
$exist = StrList::exist($string, 'test');