kirouane/interval
Composer 安装命令:
composer require kirouane/interval
包简介
Library to handel intervals
README 文档
README
Interval
This library provides some tools to handle intervals. For instance, you can compute the union or intersection of two intervals.
Use cases
- Availabilities calculation.
- Scheduling/calendar/planning.
- Mathematics interval computation with open/closed boundaries
- etc
Features
- It computes some operations between two intervals: union, intersection and exclusion.
- It computes some operations between two sets of intervals: exclusion for now.
- It handles several types of boundaries : float, \DateTime and integer.
- It handles infinity type as boundary.
- Ability to combine infinity with \DateTime and other types.
- filter, sort, map.
- Immutability.
- Chain operations.
Quality
- Code coverage
- Mutation test : Code coverage more than 90%
- Takes care of performance and memory usage
- PSR1/PSR2, Code Smell
Install
composer require kirouane/interval
Basic usage
Let's assume an interval [20, 40]. We instantiate a new Interval object .
$interval = new Interval(20, 40);// [20, 40];
or
$interval = Interval::create('[20,40]');// [20, 40];
We can do some operations like :
- Intersection :
echo $interval->intersect(new Interval(30, 60)); // [30, 40];
- Union :
echo $interval->union(new Interval(30, 60)); // {[20, 60]};
or
echo $interval->union(new Interval(60, 100)); // {[20, 40], [60, 100]};
- Exclusion :
echo $interval->exclude(new Interval(30, 60)); // {[20, 30[};
or
echo $interval->exclude(new Interval(30, 35)); // {[20, 30[, ]35, 40]};
We can compare two intervals as well:
- Overlapping test :
echo $interval->overlaps(new Interval(30, 60)); // true;
- Inclusion test :
echo $interval->includes(new Interval(30, 60)); // false;
Use DateTimeInterface as boundary
$interval = new Interval(new \DateTime('2016-01-01'), new \DateTime('2016-01-10')); // [2016-01-01T00:00:00+01:00, 2016-01-10T00:00:00+01:00];
- Union :
echo $interval->union(Interval::create('[2016-01-10, 2016-01-15]')); // {[2016-01-01T00:00:00+01:00, 2016-01-15T00:00:00+01:00]};
Use Infinity as boundary
$interval = new Interval(-INF, INF);// ]-∞, +∞[;
- Exclusion :
echo $interval->exclude(Interval::create('[2016-01-10, 2016-01-15]')); // {]-∞, 2016-01-10T00:00:00+01:00[, ]2016-01-15T00:00:00+01:00, +∞[};
Operations on sets (arrays) of intervals
$intervals = Intervals::create(['[0,5]', '[8,12]']);// {[0, 5], [8, 12]};
- Exclusion :
echo $intervals->exclude(Intervals::create(['[3,10]'])); // {[0, 3[, ]10, 12]};
Chaining
$result = Interval ::create('[10, 20]') ->intersect(new Interval(11, 30)) ->union(new Interval(15, INF)) ->exclude(Intervals::create(['[18, 20]', '[25, 30]', '[32, 35]', '[12, 13]'])) ->sort(function (Interval $first, Interval $second) { return $first->getStart()->getValue() <=> $second->getStart()->getValue(); }) ->map(function (Interval $interval) { return new Interval( $interval->getStart()->getValue() ** 2, $interval->getEnd()->getValue() ** 2 ); }) ->filter(function (Interval $interval) { return $interval->getEnd()->getValue() > 170; }); // {[169, 324], [400, 625], [900, 1024], [1225, +∞[}; echo $result;
Advanced usage
You can create intervals with open boundaries :
$result = Intervals ::create([']10, +INF[']) ->exclude(Intervals::create([']18, 20]', ']25, 30[', '[32, 35]', ']12, 13]'])); // {]10, 12], ]13, 18], ]20, 25], [30, 32[, ]35, +∞[}
Contributing
You are very welcomed to contribute to this Library!
-
Clone
git clone https://github.com/Kirouane/interval.git -
Install
composer installormake install(with docker and docker-compose) -
Test
vendor/bin/phpunit -
Build
vendor/bin/grumphp run
kirouane/interval 适用场景与选型建议
kirouane/interval 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.53k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2017 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「time」 「period」 「booking」 「interval」 「infinity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kirouane/interval 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kirouane/interval 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kirouane/interval 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library to handel intervals
Tools to convert between .NET and PHP date formats
Period handling
Adds the EDTF data type to Wikibase
Date and time library
Date component is a set of methods to help with the manipulation of dates.
统计信息
- 总下载量: 78.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-21