midorikocak / arraytools
方便的数组工具,用于从数组创建和更新对象,将对象转换为数组以及验证它们。
Requires
- php: ~7.4
- ext-json: *
- ext-pdo: *
- midorikocak/querymaker: ^1.2.3
Requires (Dev)
- ext-pdo: *
- opsway/psr12-strict-coding-standard: ^0.3.0
- phpunit/phpunit: >=8.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-08 06:59:20 UTC
README
方便的数组工具,用于从数组创建和更新对象,将对象转换为数组以及验证它们。
需求
严格要求PHP 7.4。
安装
通过Composer
$ composer require midorikocak/arraytools
用法
对象和数组转换
假设你有一个简单的数据对象,如下所示
<?php declare(strict_types=1); namespace midorikocak\arraytools; class User implements ArrayConvertableInterface, ArrayUpdateableInterface { use ArrayConvertableTrait; use ArrayUpdateableTrait; private ?string $id; private string $username; private string $email; private string $password; public function __construct(?string $id, string $username, string $email, string $password) { $this->id = $id; $this->username = $username; $this->password = $password; $this->email = $email; } public function getId(): ?string { return $this->id; } public function setId(string $id) { $this->id = $id; } // Getters and setters
为了将此对象转换为数组,您应该实现如toArray
和fromArray
这样的方法。
public function toArray(): array { $toReturn = [ 'username' => $this->getUsername(), 'email' => $this->getEmail(), 'password' => $this->getPassword(), ]; if ($this->getId()) { $toReturn['id'] = $this->getId(); } return $toReturn; } public static function fromArray($array): User { $id = $array['id'] ?? null; $username = $array['username']; $email = $array['email']; $password = $array['password']; return new User($id, $username, $email, $password); }
这可能会在更改和责任方面引起许多问题。相反,您可以在数据对象实现中使用ArrayConvertableTrait
。
<?php declare(strict_types=1); namespace midorikocak\arraytools; class User implements ArrayConvertableInterface { use ArrayConvertableTrait; private ?string $id; private string $username; private string $email; private string $password; // rest
只需从您的对象中调用toArray()
方法即可返回一个包含构造函数参数及其名称作为数组键的数组。
注意: 特性期望实现的对象具有getter。
$userData = [ 'id' => '1', 'username' => 'midorikocak', 'password' => '24738956349lhksbvlhf', 'email' => 'mtkocak@gmail.com', ]; $user = new User( $userData['id'] ?? null, $userData['username'], $userData['email'], $userData['password'] ); $userArray = $user->toArray(); print_r($userArray);
输出结果为
Array
(
[id] => 1
[username] => midorikocak
[password] => 24738956349lhksbvlhf
[email] => mtkocak@gmail.com
)
fromArray
ArrayConvertableTrait
还为您的对象添加了fromArray
功能。它期望数组具有与构造函数参数相同的键。
$userData = [ 'username' => 'midorikocak', 'password' => '24738956349lhksbvlhf', 'email' => 'mtkocak@gmail.com', ]; $user = User::fromArray($userData);
使用数组数据更新对象
如果您使用ArrayUpdateableTrait
,则可以在对象中使用setFromArray()
方法。它将使用数组数据更新对象实例。
注意: 它期望数组具有与setter相同的键。
$userData = [ 'id' => '2', 'username' => 'update', 'password' => 'updatedpw', 'email' => 'updated@email.com', ]; $user->setFromArray($userData);
数组验证
您可以使用ArrayValidator
类根据您定义的规则验证您的数组。
use midorikocak\arraytools\ArrayValidator; $arrayValidator = new ArrayValidator();
数组应有一个键
use midorikocak\arraytools\ArrayValidator; $toValidate = [ 'id'=>'1', 'username'=>'uname', 'password'=>'' ]; $arrayValidator = new ArrayValidator(); $arrayValidator->hasKey('id'); echo $arrayValidator->validate($toValidate); // true $arrayValidator->hasKey('hasan'); echo $arrayValidator->validate($toValidate); // false
数组应包含键
$arrayValidator->hasKeys('id', 'username'); echo $arrayValidator->validate($toValidate); // true
数组应精确地具有键
$arrayValidator->keys('id', 'username', 'password'); echo $arrayValidator->validate($toValidate); // true
数组中定义的键不应为空
$arrayValidator->notEmpty('password'); echo $arrayValidator->validate($toValidate); // false
规则是可链接的
echo $arrayValidator ->keys('id', 'username', 'password') ->notEmpty('password') ->hasKeys('id', 'username') ->validate($toValidate); // false
数组可以符合定义的架构
可以使用简单的架构结构来检查数组值。架构值可以是boolean
、domain
、int
、email
、mac
、float
、regexp
和string
之一。
$schema = [ 'username' => 'string', 'password' => 'string', 'email' => 'email', ]; $arrayValidator->schema($schema); // true
数组可以符合用户提供的函数
可以将接受数组并返回布尔值的函数附加到最后的验证。
echo $arrayValidator->validate($toValidate, function($array){ return array_key_exists('key', $array); }); // false
自定义验证器
要创建自定义验证器,您可以扩展AbstractValidator
类。请参阅源代码以获取详细信息。
动机和警告
主要是教育目的。请自行承担风险。
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
贡献
有关详细信息,请参阅CONTRIBUTING和CODE_OF_CONDUCT。
安全
如果您发现任何与安全相关的问题,请通过电子邮件mtkocak@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。