定制 arnapou/ensure 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arnapou/ensure

最新稳定版本:v2.8.1

Composer 安装命令:

composer require arnapou/ensure

包简介

Library - Simple library to make safer php code for SA tools.

README 文档

README

pipeline coverage

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

DateRef8.58.48.38.2
25/10/20252.8.x, main×××
24/11/20242.7.x××
25/11/20232.0 - 2.6×
17/05/20241.4.x××
23/08/20231.x×

统计信息

  • 总下载量: 1.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-24