oasis / utils

各种实用类和函数

v1.9.2 2020-01-09 03:51 UTC

README

此组件提供了一些PHP助手类,用于执行常见任务。

安装

使用以下命令安装最新版本

$ composer require oasis/utils

数据提供者

通常我们使用数据提供者来创建一个可验证的容器。 Oasis\Mlib\Utils\ArrayDataProvider 可能是使用最频繁的。

示例

<?php

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);

数据打包器

当你有一些应该写入或从流(文件、网络)读取的对象时, Oasis\Mlib\Utils\DataPacker 就是为你准备的。

以下是一个使用数据打包器的示例

<?php

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
}

凯撒密码

凯撒密码 是已知最早且最简单的密码之一。它是一种替换密码,其中明文中的每个字母在字母表中向下“移动”一定的位置。

oasis/utils 为凯撒密码提供了简单的加密类

<?php
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
);

RC4

RC4 是一个非常简单的流密码。 oasis/utils 提供了对 RC4 加密的快速访问。以下是一个示例

<?php

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'

字符串实用工具

Oasis\Mlib\Utils\StringUtils 类提供了一些简单的字符串相关函数

<?php

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'

内存使用监控

有时,PHP脚本会耗尽内存。对于长时间运行的脚本来说,监控内存使用尤为重要。 oasis/utils 提供了一些内存使用监控和即时管理的工具。

示例

<?php

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();

Tick 是PHP支持的一种声明指令,用于监控底层代码执行。