forweban/dz2-lib

DZ-2. Packagist 库

0.1.2 2019-06-30 18:01 UTC

This package is auto-updated.

Last update: 2024-09-29 05:09:30 UTC


README

dz2-library

类 FirstLastKeys

1. 在类的 isCount($arr) 方法中进行了内部检查 is_countable($arr)

2. 在 setFirstLastKeys($array) 方法中使用了返回类型 void

3. 计算传入数组的第一个和最后一个键 array_key_first($arr), array_key_last($arr)

示例

...

$array = [
    'a' => 'AAA',
    'b' => 'BBB',
    'c' => 'CCC',
    'd' => 'DDD',
];

$firstLast = new FirstLastKeys($array);

list($first, $last) = $firstLast->getFirstLastKeys();

// $first = 'a'
// $last = 'd'

类 SomeFunctions

4. 包含使用 Argon2 进行密码哈希的方法

示例

$password = 'qwerty';
$hashArgon = SomeFunctions::hashArgon($password);

5. 包含使用 null ?? 运算符获取变量值的方法

null ?? 运算符:如果第一个操作数已设置且不为 NULL,则返回第一个操作数,否则返回第二个操作数

public static function getGetUser($getUser)
{
    return $userName = $getUser ?? 'nobody';
}

示例

提取 $_GET['user'] 的值,如果未设置,则返回 'nobody'

$_GET['user'] = 'Vasyz';
$name = SomeFunctions::getGetUser($_GET['user']);

类 Help

  1. 实现了在类型化的参数和返回值中允许 null 的功能
public static function br($n = null)
    {
        for ($i = 0; $i <= $n; $i++) {
            echo "<pre>".PHP_EOL."</pre>";
        };
    }

应用

Help::br();
Help::br(3);

类 Math

  1. 添加了对定义类常量作用域的支持
  2. 允许在类型化的参数和返回值中使用 null
class Math
{
    ...
    
    private const PI = '3.1415926535';

    /**
     * @param float $rad
     * @return float|null
     */
    public static function getSquareCircle(float $rad): ?float
    {
        return self::PI * $rad ^ 2;
    }
    
    ...
}

应用

$radius = 2.5;
$square = Math::getSquareCircle($radius);

index.php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use forweban\dz2library\src\Help;
use forweban\dz2library\src\FirstLastKeys;
use forweban\dz2library\src\SomeFunctions;
use forweban\dz2library\src\Math;

$array = [
    'a' => 'AAA',
    'b' => 'BBB',
    'c' => 'CCC',
    'd' => 'DDD',
];

$firstLast = new FirstLastKeys($array);

list($first, $last) = $firstLast->getFirstLastKeys();

echo 'First key of array is \'' . $first . '\'';
Help::br(1);
echo 'Last key of array is \'' . $last . '\'';
Help::br();
Help::see($array);
Help::hr();

$password = 'qwerty';
$hashArgon = SomeFunctions::hashArgon($password);
echo $hashArgon;

Help::br();
Help::hr();

$_GET['user'] = 'Vasya';
$name = SomeFunctions::getGetUser($_GET['user']);
echo $name;

Help::br();
Help::hr();

$radius = 2.5;
$square = Math::getSquareCircle($radius);
echo $square;