PHP code example of oasis / utils

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

    

oasis / utils example snippets




use Oasis\Mlib\Utils\ArrayDataProvider;
use Oasis\Mlib\Utils\DataProviderInterface;

$data = [
    "int-key" => 10,
    "string-key" => "name",
    "switch" => "true",
    "wrong-type" => "hello",
];

$dp = new ArrayDataProvider($data);

// $i = 10
$i = $dp->getMandatory('int-key', DataProviderInterface::INT_TYPE);
// throws Oasis\Mlib\Utils\Exceptions\MandatoryValueMissingException
$i = $dp->getMandatory('int-key2', DataProviderInterface::INT_TYPE);
// $i = 5
$i = $dp->getOptional('int-key2', DataProviderInterface::INT_TYPE, 5);

// $s = 'name';
$s = $dp->getMandatory('string-key', DataProviderInterface::STRING_TYPE);

// $onoff = true;
$onoff = $dp->getOptional('switch', DataProviderInterface::BOOL_TYPE, false);

// throws Oasis\Mlib\Utils\Exceptions\InvalidDataTypeException
$wrongType = $dp->getMandatory('wrong-type', DataProviderInterface::BOOL_TYPE);




use Oasis\Mlib\Utils\DataPacker;

$obj = new stdClass();

$tmpfile = tempnam(sys_get_temp_dir(), '');

$packer = new DataPacker();
$fh     = fopen($tmpfile, 'w');
$packer->attachStream($fh);
$packer->packToStream($obj);
$packer->packToStream($obj);
$packer->packToStream($obj);
fclose($fh);

$fh = fopen($tmpfile, 'r');
$packer->attachStream($fh);
while ($obj = $packer->unpackFromStream()) {
    // we should have 3 $obj unpacked from stream
}



use Oasis\Mlib\Utils\CaesarCipher;

$cipher = new CaesarCipher();

// encrypt and decrypt integer
$enc = $cipher->encrypt(1234);
$dec = $cipher->decrypt($enc); // $dec = 1234

// encrypt and decrypt string
$enc = $cipher->encrypt("abcdefg");
$dec = $cipher->decrypt($enc); // $dec = "abcdefg"

// using stronger cipher
$cipher = new CaesarCipher(
    32, // bits to use in partition, default to 32, must be divisable by partition size
    8,  // partition size, even positive number, default to 8
    12  // strength, positive number, default to 5
);




use Oasis\Mlib\Utils\Rc4;

$encrypted = Rc4::rc4('random-key', 'abc');

// to decrypt, call the encrypt function with same key
$decrypted = Rc4::rc4('random-key', $encrypted); // $decrypted = 'abc'




use Oasis\Mlib\Utils\StringUtils;

$str = 'abcdefg';

var_dump(StringUtils::stringStartsWith($str, 'a')); // true
var_dump(StringUtils::stringStartsWith($str, 'b')); // false
var_dump(StringUtils::stringEndsWith($str, 'g')); // true
var_dump(StringUtils::stringEndsWith($str, 'f')); // false

var_dump(StringUtils::stringChopdown($str, 4)); // 'abcd'




use Oasis\Mlib\Utils\CommonUtils;

// check the current memory usage and increase if needed
CommonUtils::monitorMemoryUsage();

// if the script declares tick, this will monitor memory usage every time tick is triggered
CommonUtils::registerMemoryMonitorForTick();