olifanton/utils

此包已被弃用且不再维护。作者建议使用 olifanton/interop 包。

Olifanton 工具库

0.4.0 2022-12-22 14:35 UTC

This package is auto-updated.

Last update: 2023-04-08 05:44:20 UTC


README

Olifanton PHP 工具库

Code Coverage Badge Tests Latest Stable Version Total Downloads

PHP 版本的 tonweb-utils JS 库

安装

composer require olifanton/utils

文档

入门

通过 Composer 安装 olifanton/utils 包并包含自动加载脚本

<?php declare(strict_types=1);

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

use Olifanton\Utils\Address;
use Olifanton\Utils\Bytes;
use Olifanton\Utils\Crypto;
use Olifanton\Utils\Units;

// Now you can use Olifanton utils classes

库类

Olifanton\Utils\Address

Address 是一个类,允许您在 TON 网络中处理智能合约地址。在官方 文档 中了解更多关于地址的信息。

Address 构造函数
/**
 * @param string | \Olifanton\Utils\Address $anyForm
 */
public function __construct(string | Address $anyForm)

参数

  • $anyForm — 支持的格式中的地址。支持的值包括
    • 友好的格式(base64 编码,URL 安全或不安全):EQBvI0aFLnw2QbZgjMPCLRdtRHxhUyinQudg6sdiohIwg5jL;
    • 原始格式:-1:fcb91a3a3816d0f7b8c2c76108b8a9bc5a6b7a55bd79f8ab101c52db29232260;
    • 其他 Address 实例,在这种情况下,新实例将是其他地址的不可变副本。

根据传递的值,Address 实例将存储有关输入地址标志的信息。

如果输入值不是有效的地址,则将抛出 \InvalidArgumentException

Address 静态方法
isValid(string | \Olifanton\Utils\Address $anyForm): bool

检查传递的值是否为任何形式的有效地址。

Address 方法
toString(): string
/**
 * @param bool|null $isUserFriendly User-friendly flag
 * @param bool|null $isUrlSafe URL safe encoded flag
 * @param bool|null $isBounceable Bounceable address flag
 * @param bool|null $isTestOnly Testnet Only flag
 */
public function toString(?bool $isUserFriendly = null,
                         ?bool $isUrlSafe = null,
                         ?bool $isBounceable = null,
                         ?bool $isTestOnly = null): string

返回地址的字符串表示形式。

如果所有参数都保留为默认值,则地址将使用在构造函数中识别的相同标志进行格式化。

getWorkchain(): int

返回 Workchain ID。对于主链返回 -1,对于基本工作链返回 0

getHashPart(): Uint8Array

返回地址 Account ID。

isTestOnly(): bool

如果地址具有 isTestnetOnly 标志,则返回 true。

isBounceable(): bool

如果地址具有 isBounceable 标志,则返回 true。

isUserFriendly(): bool

如果地址用户友好,则返回 true。

isUrlSafe(): bool

如果地址仅使用 URL 安全字符编码,则返回 true。

Olifanton\Utils\Bytes

这是一个用于处理 Uint8Array 的辅助类。您不太可能直接使用此类,因为它旨在处理 Cells 的内部表示。然而,本文档描述了您可能发现有用的方法。

Bytes 静态方法
readNBytesUIntFromArray(): int

从 Uint8Array 中返回 $n 个字节。

compareBytes(): bool

如果 Uint8Array 的 $a 和 $b 相等,则返回 true。

arraySlice(): Uint8Array

从给定的 Uint8Array 中返回一个不可变片段。

concatBytes(): bool

从给定的数组中返回一个新的 Uint8Array。

concatBytes(): Uint8Array

从给定的数组中返回一个新的 Uint8Array。

stringToBytes(): Uint8Array

从 PHP 字节字符串返回 Uint8Array。

示例

$a = \Olifanton\Utils\Bytes::stringToBytes('a');
$a[0] === 97; // True, because the ASCII code of `a` is 97 in decimal
hexStringToBytes(): Uint8Array

从十六进制字符串返回 Uint8Array。

示例

$a = \Olifanton\Utils\Bytes::hexStringToBytes('0a');
$a[0] === 10; // True
bytesToHexString(): string

返回 Uint8Array 的十六进制字符串表示。

示例

$s = \Olifanton\Utils\Bytes::bytesToHexString(new \ajf\TypedArrays\Uint8Array([10]));
$s === '0a'; // True

Olifanton\Utils\Crypto

Crypto 方法

@TODO

Olifanton\Utils\Units

Units 方法

@TODO

测试

composer run test

许可

MIT