edgaralexanderfr / php-types
库旨在扩展PHP的数据类型和有用的数据结构,以及在目标项目代码中的强类型一致性。
1.6.0
2024-08-22 00:40 UTC
This package is not auto-updated.
Last update: 2024-10-03 01:07:17 UTC
README
PHP Types 是一个小型库/框架,旨在提高并鼓励在项目的代码库中实施强类型,包括基本数组类型、有用的数据结构等,这些类型本身并非语言原生支持。
目录 📖
要求
- PHP 8.3.0 或更高版本
- Composer (可选)
- 拥有初始化的Composer项目 (可选)
安装
使用 Composer 安装 PHP Types
composer require edgaralexanderfr/php-types
或
您可以始终下载库作为 .zip 文件,解压缩它,将其存储在您的目标项目中,并包含库项目根目录中的 autoload.php 文件
curl -L -o php-types-master.zip https://github.com/edgaralexanderfr/php-types/archive/refs/heads/master.zip \ && unzip php-types-master.zip \ && rm php-types-master.zip
或
您可以下载库的打包 .phar 版本
mkdir lib \
&& curl -L -o lib/php-types.phar https://github.com/edgaralexanderfr/php-types/raw/master/lib/php-types.phar
<?php declare(strict_types=1); include 'lib/php-types.phar'; use function PHPTypes\Primitive\string_array; $message = string_array('Hello', 'world', '!'); print_r($message);
使用
基本类型
ObjectType
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\object_t; use function PHPTypes\Primitive\object; function display(object_t $object): void { echo "{$object->name}:" . PHP_EOL; echo $object->json() . PHP_EOL; } $object = object([ 'name' => 'Ford Mustang GT', 'brand' => 'Ford', 'category' => 'Muscle Car', 'gas' => 0.8, 'engine' => object([ 'type' => 'V8', 'rpm' => 750, ]), 'transmission' => object([ 'type' => 'manual', 'status' => 1, 'gears' => ['R', 'N', '1', '2', '3', '4', '5', '6'], ]), ]); display($object);
php examples/object.php Ford Mustang GT: {"name":"Ford Mustang GT","brand":"Ford","category":"Muscle Car","gas":0.8,"engine":{"type":"V8","rpm":750},"transmission":{"type":"manual","status":1,"gears":["R","N","1","2","3","4","5","6"]}}
BoolArray
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\bool_array_t; use function PHPTypes\Primitive\bool_array; function display(bool_array_t $array): void { foreach ($array as $value) { echo $value . PHP_EOL; } } $array = bool_array(false, true, false); display($array);
php examples/bool_array.php 1
IntArray
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\int_array_t; use function PHPTypes\Primitive\int_array; function display(int_array_t $array): void { foreach ($array as $value) { echo $value . PHP_EOL; } } $array = int_array(1, 2, 3); display($array);
php examples/int_array.php 1 2 3
FloatArray
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\float_array_t; use function PHPTypes\Primitive\float_array; function display(float_array_t $array): void { foreach ($array as $value) { echo $value . PHP_EOL; } } $array = float_array(0.1, 2.3, 4.5); display($array);
php examples/float_array.php 0.1 2.3 4.5
StringArray
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\string_array_t; use function PHPTypes\Primitive\string_array; function display(string_array_t $array): void { foreach ($array as $value) { echo $value . PHP_EOL; } } $array = string_array('🍎', '🍊', '🍌'); display($array);
php examples/string_array.php 🍎 🍊 🍌
ObjectArray
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Primitive\object_array_t; use function PHPTypes\Primitive\object; use function PHPTypes\Primitive\object_array; function display(object_array_t $array): void { foreach ($array as $value) { print_r($value); } } $array = object_array( object([ 'id' => 1, 'name' => 'Charles Babbage', ]), object([ 'id' => 2, 'name' => 'Alan Turing', ]), object([ 'id' => 3, 'name' => 'Edsger Dijkstra', ]), ); display($array);
php examples/object_array.php PHPTypes\Primitive\object_t Object ( [id] => 1 [name] => Charles Babbage ) PHPTypes\Primitive\object_t Object ( [id] => 2 [name] => Alan Turing ) PHPTypes\Primitive\object_t Object ( [id] => 3 [name] => Edsger Dijkstra )
数据结构
HashSet
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Set\HashSet; $set = new HashSet(); $set->add(1); $set->add('two'); $set->add(3); $set->add(3); $set->add('two'); $set->add('one'); foreach ($set as $value) { echo $value . PHP_EOL; }
php examples/hash_set.php 1 two 3 one
IntHashSet
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Set\IntHashSet; $set = new IntHashSet(); $set->add(1); $set->add(2); $set->add(3); $set->add(3); $set->add(2); $set->add(1); $set->add(0); foreach ($set as $value) { echo $value . PHP_EOL; }
php examples/int_hash_set.php 1 2 3 0
StringHashSet
<?php declare(strict_types=1); include 'vendor/autoload.php'; use PHPTypes\Set\StringHashSet; $set = new StringHashSet(); $set->add('🍎'); $set->add('🍊'); $set->add('🍌'); $set->add('🍌'); $set->add('🥭'); foreach ($set as $value) { echo $value . PHP_EOL; }
php examples/string_hash_set.php 🍎 🍊 🍌 🥭
类型错误
如果您尝试分配与数组类型不同的值,则会抛出 TypeError
<?php declare(strict_types=1); include 'vendor/autoload.php'; use function PHPTypes\Primitive\int_array; $array = int_array(1, 2, 3); try { $array[2] = '🥭'; } catch (TypeError $e) { echo $e->getMessage() . PHP_EOL; } try { $array[] = '🥭'; } catch (TypeError $e) { echo $e->getMessage() . PHP_EOL; } $array[] = 4; print_r($array);
php examples/type_error.php Element must be of type integer, string given, called Element must be of type integer, string given, called PHPTypes\Primitive\int_array_t Object ( [type:protected] => integer [storage:ArrayIterator:private] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) )
hasAny()
和 isEmpty()
方法
数组包含有用的方法,可以帮助确定数组是否为空或至少包含一个元素
<?php declare(strict_types=1); include 'vendor/autoload.php'; use function PHPTypes\Primitive\object_array; use function PHPTypes\Primitive\string_array; $fruits = string_array('🥭'); if ($fruits->hasAny()) { echo '`$fruits` contains at least 1 element.' . PHP_EOL; } $objects = object_array(); if ($objects->isEmpty()) { echo '`$objects` is an empty array.' . PHP_EOL; }
php examples/has_any_is_empty.php `$fruits` contains at least 1 element. `$objects` is an empty array.