arnapou/ensure
最新稳定版本:v2.8.1
Composer 安装命令:
composer require arnapou/ensure
包简介
Library - Simple library to make safer php code for SA tools.
README 文档
README
This library is a simple tool to have a safer php code.
We want to 'ensure' or 'enforce' the type check for quality reasons and/or for Static Analysis tools like PHPStan or Psalm.
Installation
composer require arnapou/ensure
packagist 👉️ arnapou/ensure
Ensure
In the example below, we guarantee the fact that a non-constrained int or string are constrained into the class.
use Arnapou\Ensure\Ensure;
use Arnapou\Ensure\Expected;
class MyObject
{
/** @var positive-int */
public int $id;
/** @var non-empty-string */
public string $name;
/**
* @throws Expected
*/
public function __construct(int $id, string $name)
{
$this->id = Ensure::positiveInt($id);
$this->name = Ensure::nonEmptyString($name);
}
}
Enforce
In the example below, which can be from a legacy code, we have a phpdoc type-hinting in the construct.
But it is not "hard" enough from a php point of view, and it may be too risky to change the mixed type-hinting.
You can guaranty the type hinting inside the class while staying lax on type-check by using Enforce.
It will cast the value when it is enough "safe" to do it :
- Examples of valid/auto-casted
int:"1234","1.2e3",3.0,true - Examples of invalid
int:"foo","1.234e2",3.14,[]
use Arnapou\Ensure\Enforce;
use Arnapou\Ensure\Expected;
class MyObject
{
/** @var positive-int */
public int $id;
/** @var non-empty-string */
public string $name;
/**
* @param int $id
* @param string $name
*
* @throws Expected
*/
public function __construct(mixed $id, mixed $name)
{
$this->id = Enforce::positiveInt($id);
$this->name = Enforce::nonEmptyString($name);
}
}
Message customization or translation
You can do that implementing your own MessageFactoryInterface
and then set it as default for Expected class.
class MyMessageFactory implements MessageFactoryInterface
{
public function createExpectedMessage(string $expected, int $code, mixed $value, string $propertyName, mixed ...$parameters): string
{
// Build the message here.
}
}
// Change the default message factory.
\Arnapou\Ensure\Expected::setMessageFactory(new MyMessageFactory());
ℹ️ The default MessageFactory implementation is final,
you cannot extend it, but you can reuse it with composition if necessary :
class MyMessageFactory implements MessageFactoryInterface
{
private \Arnapou\Ensure\MessageFactory\MessageFactory $internal;
public function __construct(){
$this->internal = new \Arnapou\Ensure\MessageFactory\MessageFactory();
}
public function createExpectedMessage(string $expected, int $code, mixed $value, string $propertyName, mixed ...$parameters): string
{
if (/* any logic you need */) {
// Your customization here
}
// Default on the default message factory
return $this->internal->createExpectedMessage($expected, $code, $value, $propertyName, ...$parameters);
}
}
// Change the default message factory.
\Arnapou\Ensure\Expected::setMessageFactory(new MyMessageFactory());
Php versions
| Date | Ref | 8.5 | 8.4 | 8.3 | 8.2 |
|---|---|---|---|---|---|
| 25/10/2025 | 2.8.x, main | × | × | × | |
| 24/11/2024 | 2.7.x | × | × | ||
| 25/11/2023 | 2.0 - 2.6 | × | |||
| 17/05/2024 | 1.4.x | × | × | ||
| 23/08/2023 | 1.x | × |
统计信息
- 总下载量: 1.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-24