Roomify\Bat\Constraint\ConstraintManager::normalizeMinMaxDaysConstraints PHP Метод

normalizeMinMaxDaysConstraints() защищенный Метод

protected normalizeMinMaxDaysConstraints ( ) : array
Результат array
    protected function normalizeMinMaxDaysConstraints()
    {
        $new_constraints = array();
        $constraints = array_map(function ($object) {
            return clone $object;
        }, $this->constraints['Roomify\\Bat\\Constraint\\MinMaxDaysConstraint']);
        foreach (array_reverse($constraints) as $constraint) {
            $start_date = $constraint->getStartDate();
            $end_date = $constraint->getEndDate();
            $split_constraint = NULL;
            if (!empty($new_constraints)) {
                foreach ($new_constraints as $new_constraint) {
                    $new_start_date = $new_constraint->getStartDate();
                    $new_end_date = $new_constraint->getEndDate();
                    if ($constraint->getMinDays() && $new_constraint->getMinDays() || $constraint->getMaxDays() && $new_constraint->getMaxDays()) {
                        if ($start_date >= $new_start_date && $start_date <= $new_end_date) {
                            $new_end_date_clone = clone $new_end_date;
                            $constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
                        } elseif ($end_date >= $new_start_date && $end_date <= $new_end_date) {
                            $new_start_date_clone = clone $new_start_date;
                            $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
                        } elseif ($start_date < $new_start_date && $end_date > $new_end_date) {
                            if ($constraint->getEndDate() > $new_start_date) {
                                $new_start_date_clone = clone $new_start_date;
                                $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
                            }
                            if ($split_constraint == NULL) {
                                $split_start_date = clone $new_end_date;
                                $split_start_date->add(new \DateInterval('P1D'));
                                $split_end_date = $end_date;
                                $split_constraint = new MinMaxDaysConstraint($constraint->getUnits(), $constraint->getMinDays(), $constraint->getMaxDays(), $split_start_date, $split_end_date, $constraint->getCheckinDay());
                            } else {
                                $split_start_date = $split_constraint->getStartDate();
                                $split_end_date = $split_constraint->getEndDate();
                                if ($split_start_date < $new_end_date) {
                                    $new_end_date_clone = clone $new_end_date;
                                    $split_constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
                                }
                                if ($split_end_date < $new_start_date) {
                                    $new_start_date_clone = clone $new_start_date;
                                    $split_constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
                                }
                            }
                        }
                    }
                }
                if ($split_constraint != NULL) {
                    $new_constraints[] = $split_constraint;
                }
            }
            $new_constraints[] = $constraint;
        }
        foreach ($new_constraints as $i => $constraint) {
            if ($constraint->getStartDate() > $constraint->getEndDate()) {
                unset($new_constraints[$i]);
            }
        }
        return $new_constraints;
    }