nejcc/php-datatypes

PHP 数据类型

v1.0.4 2024-10-03 06:40 UTC

This package is auto-updated.

Last update: 2024-10-03 07:49:11 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Quality Gate Status Security Rating Maintainability Rating Vulnerabilities Bugs

Coverage

Duplicated Lines (%)

Lines of Code

我很高兴分享我的最新PHP包,PHP Datatypes。这个库通过一种灵活而严格的方式处理PHP中的基本数据类型,如整数、浮点数和字符串。它强调类型安全和精度,支持有符号和无符号整数(Int8,UInt8等)和各种浮点数格式(Float32,Float64等)。

使用PHP Datatypes,您可以对处理的数据有细粒度的控制,确保您的操作在有效范围内。这对于任何希望避免在PHP中常见的陷阱(如溢出、除以零和意外的类型转换)的人来说是完美的。

安装

您可以通过composer安装此包

composer require nejcc/php-datatypes

用法

以下是在您的项目中使用基本整数和浮点类的一些示例。

这种方法有几个关键好处

  • 类型安全:通过显式定义数据类型,如UInt8,您消除了无效值意外进入应用程序的风险。例如,强制执行无符号整数确保值始终在有效范围内,提供对意外数据输入的安全保障。

  • 精度:特别是在处理浮点数时,由于PHP如何原生地处理浮点数,精度在PHP中可能很棘手。通过提供精确的类型,如Float32或Float64,我们为开发者提供了他们所需的控制,以保持计算的一致性。

  • 范围保障:通过指定确切的范围,您可以防止像溢出或下溢这样的问题,这些问题在动态类型语言(如PHP)中通常不会被检查。

  • 可读性和维护性:显式数据类型提高了代码的可读性。当开发人员阅读您的代码时,他们可以立即知道期望的值类型及其约束。这增强了长期的可维护性。

Laravel示例

以下是它在不同类型中的实际应用,重点关注整数和浮点数的严格处理

namespace App\Http\Controllers;

use Illuminate\Http\Request;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;

class TestController
{
    public UInt8 $user_id;
    public Float32 $account_balance;

    public function __invoke(Request $request)
    {
        // Validating and assigning UInt8 (ensures non-negative user ID)
        $this->user_id = uint8($request->input('user_id'));
        
        // Validating and assigning Float32 (ensures correct precision)
        $this->account_balance = float32($request->input('account_balance'));
        
        // Now you can safely use the $user_id and $account_balance knowing they are in the right range
        dd([
            'user_id' => $this->user_id->getValue(),
            'account_balance' => $this->account_balance->getValue(),
        ]);
    }
}

在这里,我们不仅保护了用户ID,而且还处理了可能复杂的浮点数运算,其中精度至关重要。这特别适用于金融或分析等领域,在这些领域,数据完整性至关重要。

PHP示例

整数

use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;

$int8 = new Int8(-128); // Minimum value for Int8
echo $int8->getValue(); // -128

$uint8 = new UInt8(255); // Maximum value for UInt8
echo $uint8->getValue(); // 255

浮点数

use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;

$float32 = new Float32(3.14);
echo $float32->getValue(); // 3.14

$float64 = new Float64(1.7976931348623157e308); // Maximum value for Float64
echo $float64->getValue(); // 1.7976931348623157e308

算术运算

use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;

$int1 = new Int8(50);
$int2 = new Int8(30);

$result = $int1->add($int2); // Performs addition
echo $result->getValue(); // 80

路线图

Data Types
│
├── Scalar Types
│   ├── Integer Types
│   │   ├── Signed Integers
│   │   │   ├── ✓ Int8 
│   │   │   ├── ✓ Int16
│   │   │   ├── ✓ Int32
│   │   │   ├── Int64
│   │   │   └── Int128
│   │   └── Unsigned Integers
│   │       ├── ✓ UInt8
│   │       ├── ✓ UInt16
│   │       ├── ✓ UInt32
│   │       ├── UInt64
│   │       └── UInt128
│   ├── Floating Point Types
│   │   ├── ✓ Float32
│   │   ├── ✓ Float64
│   │   ├── Double
│   │   └── Double Floating Point
│   ├── Boolean
│   │   └── Boolean (true/false)
│   ├── Char
│   └── Byte
│
├── Composite Types
│   ├── Arrays
│   │   ├── StringArray
│   │   ├── IntArray
│   │   ├── FloatArray
│   │   └── Byte Slice
│   ├── Struct
│   │   └── struct { fields of different types }
│   ├── Union
│   │   └── union { shared memory for different types }
│   ├── List
│   └── Dictionary
│
├── Reference Types
│   ├── Reference Pointer
│   ├── Void (Nullable)
│   └── Channel (Concurrency)
│
├── Map Types
│   ├── Hashmap
│   └── Map
│
└── Specialized Types
    ├── String
    ├── Double
    ├── Slice
    ├── Map
    └── Channel

测试

composer test

更改日志

有关最近更改的更多信息,请参阅更改日志

贡献

有关详细信息,请参阅贡献

安全

如果您发现任何安全相关的问题,请通过电子邮件nejc.cotic@gmail.com联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

PHP包模板

此包是用PHP包模板生成的,由Beyond Code创建。