simshaun/recurr
Composer 安装命令:
composer require simshaun/recurr
包简介
PHP library for working with recurrence rules
README 文档
README
Recurr is a PHP library for working with recurrence rules (RRULE) and converting them in to DateTime objects.
Recurr was developed as a precursor for a calendar with recurring events, and is heavily inspired by rrule.js.
Installing Recurr
The recommended way to install Recurr is through Composer.
composer require simshaun/recurr
Using Recurr
Creating RRULE rule objects
You can create a new Rule object by passing the (RRULE) string or an array with the rule parts, the start date, end date (optional) and timezone.
$timezone = 'America/New_York'; $startDate = new \DateTime('2013-06-12 20:00:00', new \DateTimeZone($timezone)); $endDate = new \DateTime('2013-06-14 20:00:00', new \DateTimeZone($timezone)); // Optional $rule = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, $endDate, $timezone);
You can also use chained methods to build your rule programmatically and get the resulting RRULE.
$rule = (new \Recurr\Rule) ->setStartDate($startDate) ->setTimezone($timezone) ->setFreq('DAILY') ->setByDay(['MO', 'TU']) ->setUntil(new \DateTime('2017-12-31')) ; echo $rule->getString(); //FREQ=DAILY;UNTIL=20171231T000000;BYDAY=MO,TU
RRULE to DateTime objects
$transformer = new \Recurr\Transformer\ArrayTransformer(); print_r($transformer->transform($rule));
$transformer->transform(...)returns aRecurrenceCollectionofRecurrenceobjects.- Each
RecurrencehasgetStart()andgetEnd()methods that return a\DateTimeobject. - If the transformed
Rulelacks an end date,getEnd()will return a\DateTimeobject equal to that ofgetStart().
Note: The transformer has a "virtual" limit (default 732) on the number of objects it generates. This prevents the script from crashing on an infinitely recurring rule. You can change the virtual limit with an
ArrayTransformerConfigobject that you pass toArrayTransformer.
Transformation Constraints
Constraints are used by the ArrayTransformer to allow or prevent certain dates from being added to a RecurrenceCollection. Recurr provides the following constraints:
AfterConstraint(\DateTime $after, $inc = false)BeforeConstraint(\DateTime $before, $inc = false)BetweenConstraint(\DateTime $after, \DateTime $before, $inc = false)
$inc defines what happens if $after or $before are themselves recurrences. If $inc = true, they will be included in the collection. For example,
$startDate = new \DateTime('2014-06-17 04:00:00'); $rule = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate); $transformer = new \Recurr\Transformer\ArrayTransformer(); $constraint = new \Recurr\Transformer\Constraint\BeforeConstraint(new \DateTime('2014-08-01 00:00:00')); print_r($transformer->transform($rule, $constraint));
Note: If building your own constraint, it is important to know that dates which do not meet the constraint's requirements do not count toward the transformer's virtual limit. If you manually set your constraint's
$stopsTransformerproperty tofalse, the transformer might crash via an infinite loop. See theBetweenConstraintfor an example on how to prevent that.
Post-Transformation RecurrenceCollection Filters
RecurrenceCollection provides the following chainable helper methods to filter out recurrences:
startsBetween(\DateTime $after, \DateTime $before, $inc = false)startsBefore(\DateTime $before, $inc = false)startsAfter(\DateTime $after, $inc = false)endsBetween(\DateTime $after, \DateTime $before, $inc = false)endsBefore(\DateTime $before, $inc = false)endsAfter(\DateTime $after, $inc = false)
$inc defines what happens if $after or $before are themselves recurrences. If $inc = true, they will be included in the filtered collection. For example,
pseudo...
2014-06-01 startsBetween(2014-06-01, 2014-06-20) // false
2014-06-01 startsBetween(2014-06-01, 2014-06-20, true) // true
Note:
RecurrenceCollectionextends the Doctrine project's ArrayCollection class.
RRULE to Text
Recurr supports transforming some recurrence rules into human readable text. This feature is still in beta and only supports yearly, monthly, weekly, and daily frequencies.
$rule = new Rule('FREQ=YEARLY;INTERVAL=2;COUNT=3;', new \DateTime()); $textTransformer = new TextTransformer(); echo $textTransformer->transform($rule);
If you need more than English you can pass in a translator with one of the supported locales (see translations folder).
$rule = new Rule('FREQ=YEARLY;INTERVAL=2;COUNT=3;', new \DateTime()); $textTransformer = new TextTransformer( new \Recurr\Transformer\Translator('de') ); echo $textTransformer->transform($rule);
Warnings
- Monthly recurring rules By default, if your start date is on the 29th, 30th, or 31st, Recurr will skip following months that don't have at least that many days. (e.g. Jan 31 + 1 month = March)
This behavior is configurable:
$timezone = 'America/New_York'; $startDate = new \DateTime('2013-01-31 20:00:00', new \DateTimeZone($timezone)); $rule = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, null, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $transformerConfig->enableLastDayOfMonthFix(); $transformer->setConfig($transformerConfig); print_r($transformer->transform($rule)); // 2013-01-31, 2013-02-28, 2013-03-31, 2013-04-30, 2013-05-31
Contribute
Feel free to comment or make pull requests. Please include tests with PRs.
License
Recurr is licensed under the MIT License. See the LICENSE file for details.
simshaun/recurr 适用场景与选型建议
simshaun/recurr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.29M 次下载、GitHub Stars 达 1.6k, 最近一次更新时间为 2013 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「recurring」 「rrule」 「recurrence」 「dates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simshaun/recurr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simshaun/recurr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simshaun/recurr 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple Laravel 5 service provider for including the Recurly PHP client.
Rich payment solutions for zend framework2. Paypal, payex, authorize.net, be2bill, omnipay, recurring paymens, instant notifications and many more
Библиотека для реализаций паттерна Pub/Sub
Laravel Eloquent RRULE tools
Wireless Cross-Component Communication
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
统计信息
- 总下载量: 17.29M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1612
- 点击次数: 30
- 依赖项目数: 50
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2013-06-13