定制 jstewmc/interval 二次开发

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

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

jstewmc/interval

Composer 安装命令:

composer require jstewmc/interval

包简介

A set of numbers between two endpoints

README 文档

README

A set of numbers between two endpoints.

use Jstewmc\Interval;

// create an interval between 2 (exclusive) and 4 (inclusive)
$interval = new Interval('(2, 4]');

// compare values
$interval->compare(1);  // returns -1
$interval->compare(2);  // returns -1
$interval->compare(3);  // returns 0
$interval->compare(4);  // returns 0
$interval->compare(5);  // returns 1

// echo the interval
echo $interval;  // returns "(2, 4]"

Syntax

This library supports standard US interval syntax requiring (in order):

  • an open-bracket ([) or open-parentheses (();
  • a number, positive infinity (INF), or negative infinity (-INF);
  • a comma-space (, );
  • a number, positive infinity (INF), or negative infinity (-INF); and,
  • a close-bracket (]) or close-parentheses ()).

Positive or negative floats, integers, and infinity are accepted.

For example:

  • (2, 4] represents 2 < x <= 4
  • (-10.5, 10.5) represents -10.5 < x < 10.5
  • [0, INF) represents 0 <= x < ∞
  • (-INF, INF) represents -∞ < x < ∞

Keep in mind, this library does not support reverse-bracket syntax (e.g., ]2, 4]) or semi-colon separated syntax (e.g., [2; 4]).

Usage

You can instantiate an interval from a string or create it manually using the set methods:

use Jstewmc\Interval;

$a = new Interval('(2, 4]');

$b = (new Interval())
    ->setLowerExclusive()
    ->setLower(2)
    ->setUpper(4)
    ->setUpperInclusive();

$a == $b;  // returns true

Keep in mind, when instantiating an interval from a string, an InvalidArgumentException will be thrown if the interval's syntax is invalid:

use Jstewmc\Interval;

new Interval('[0; 0]');      // throws exception (semicolon syntax not supported)
new Interval(']0, 2]');      // throws exception (reverse brackets not supported)
new Interval('[foo, bar]');  // throws exception (use numbers or INF)
new Interval('[0, 0)');      // throws exception (same endpoint, different boundary)
new Interval('[1, -1]');     // throws exception (upper- is less than lower-bound)

Infinity is supported as the string 'INF' or the INF predefined constant:

use Jstewmc\Interval;

$a = new Interval('(-INF, 0]');

$b = (new Interval())
    ->setLowerExclusive()
    ->setLower(-INF)
    ->setUpper(0)
    ->setUpperInclusive(true);

$a == $b;  // returns true

You can compare a value to the interval using the compare() method. The compare() method will return -1, 0, or 1 if the value is below, inside, or above the interval, respectively:

use Jstewmc\Interval;

$interval = new Interval('(2, 4]');

$interval->compare(1);  // returns -1
$interval->compare(2);  // returns -1
$interval->compare(3);  // returns 0
$interval->compare(4);  // returns 0
$interval->compare(5);  // returns 1

You can get any of the interval's settings with the get methods:

use Jstewmc\Interval;

$interval = new Interval('(2, 4]');

$interval->getIsLowerInclusive();  // returns false
$interval->getLower();             // returns 2
$interval->getUpper();             // returns 4
$interval->getIsUpperInclusive();  // returns true

There are a few convenience methods to make getting and setting the boundaries eaiser:

use Jstewmc\Interval;

$interval = new Interval('(2, 4]');

$interval->isLowerInclusive();  // returns false
$interval->isLowerExclusive();  // returns true
$interval->isUpperInclusive();  // returns true
$interval->isUpperExclusive();  // returns false

echo $interval->setLowerInclusive();  // prints "[2, 4]"
echo $interval->setLowerExclusive();  // prints "(2, 4]"
echo $interval->setUpperInclusive();  // prints "(2, 4]"
echo $interval->setUpperExclusive();  // prints "(2, 4)"

That's about it!

License

MIT

Author

Jack Clayton

Version

1.0.0, August 13, 2016

  • Major release
  • Update composer.json

0.2.0, August 7, 2016

  • Add support for infinity (e.g., '(-INF, 0]').
  • Update error messages to be a little more informative.
  • Update README examples.

0.1.0, August 6, 2016

  • Initial release

jstewmc/interval 适用场景与选型建议

jstewmc/interval 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「interval」 「jstewmc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jstewmc/interval 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jstewmc/interval 我们能提供哪些服务?
定制开发 / 二次开发

基于 jstewmc/interval 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 58
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-07