简体   繁体   English

CarbonPeriod 检查周期是否有限

[英]CarbonPeriod checks whether period is finite or not

I wrote a class that collects some information from a certain period by iterating and I need to check if this period is finite.我写了一个 class ,它通过迭代从某个时期收集一些信息,我需要检查这个时期是否是有限的。 I do somethink like that:我这样做是这样的:

foreach ($cabonPeriod as $date) {
    // #some action
}

By finite, I mean whether there is an end/beginning of this period and is other than the end/start of time, and whether the number of iterations is not infinite, but probably first of the conditions will satisfy the second, so that my loop won't last forever.有限的,我的意思是这个时期是否有结束/开始并且不是时间的结束/开始,以及迭代次数是否不是无限的,但可能第一个条件会满足第二个条件,所以我的循环不会永远持续下去。

I couldn't find a suitable method, so I wrote my own and it works for me, but it probably doesn't solve all cases of more advanced periods, which I don't have.我找不到合适的方法,所以我写了自己的方法,它对我有用,但它可能无法解决所有更晚期的情况,我没有。

Here is my method:这是我的方法:

private static function checkPeriod(CarbonPeriod $period)
{
    $start = $period->getStartDate();
    if ($start->isStartOfTime() || $start->isEndOfTime()) {
        throw new LengthException('Period has no defined beginning');
    }
    $end = $period->calculateEnd();
    if (!$end || $end->isStartOfTime() || $end->isEndOfTime()) {
        throw new LengthException('Period has no defined end');
    }
}

This seems to me quite a trivial case, so I'm wondering if there is any built-in method that allows you to achieve the same effect, or similar to mine, with less / more computational effort.在我看来,这似乎是一个微不足道的案例,所以我想知道是否有任何内置方法可以让您以更少/更多的计算工作实现相同或类似于我的效果。

It's enough to be sure your period is iterable and not loop indefinitely.足以确保您的期间是可迭代的并且不会无限期地循环。

But you can have finite periods that are not satisfying this.但是你可以有不满足这一点的有限时期。 If it has a number of recurrences, then it does not has an end, if you want to accept those:如果它有很多重复,那么它没有结束,如果你想接受那些:

$end = $period->calculateEnd();
if ($end && ($end->isStartOfTime() || $end->isEndOfTime())) {
    throw new LengthException('Period has no finite end');
}

if (!$end && ($period->getRecurrences() === null || $period->getRecurrences() === INF) {
    throw new LengthException('Period has no defined end');
}

Last you can have filters in a period, filters cannot transform a finite period into an infinite period so you're on the safe side, but then can make a period to be finite in practice while it has no end and no recurrences.最后,您可以在一个周期内使用过滤器,过滤器无法将有限周期转换为无限周期,因此您处于安全的一面,但随后可以使一个周期在实践中成为有限的,而它没有尽头也没有重复。

You might also want to limit period.您可能还想限制期限。 In practice if it loops for too long, it's probably causing troubles too (such as 1 second interval from 1970 to 2060).实际上,如果循环时间过长,也可能会造成麻烦(例如从 1970 年到 2060 年的间隔为 1 秒)。

For those case you can estimate the number of cycles and decide a maximum:对于这些情况,您可以估计周期数并确定最大值:

$end = $period->calculateEnd();
if ($end && ($end->isStartOfTime() || $end->isEndOfTime())) {
    throw new LengthException('Period has no finite end');
}

$recurrences = $period->getRecurrences();
if (!$end && ($recurrences === null || $recurrences === INF) {
    throw new LengthException('Period has no defined end');
}

$maxRecurrences = 2000;
if ($recurrences > $maxRecurrences) {
    throw new LengthException('Period is too long');
}

$seconds = $period->getDateInterval()->totalSeconds;
if ($end->diffInSeconds($start, true) / $seconds > $maxRecurrences) {
    throw new LengthException('Period is too long');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM