work_time-1.0.x-dev/src/WorkTimeHoliday.php

src/WorkTimeHoliday.php
<?php

namespace Drupal\work_time;

use Drupal\Core\Config\ConfigFactoryInterface;

/**
 * Class Work Time Monthly.
 *
 * @package Drupal\work_time\Services
 */
class WorkTimeHoliday {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new GroupAdminRouteSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getHolidays($holidays = '', $year = '') {
    $config = $this->configFactory->getEditable('work_time.settings');
    $generalHoliday = $config->get('holidays');
    if (!empty($generalHoliday)) {
      $generalHoliday = str_replace(PHP_EOL, ',', $generalHoliday);
    }
    if (!empty($holidays)) {
      $generalHoliday .= ',' . $holidays;
    }
    $rangeHolidays = [];
    $holidays = explode(',', preg_replace('/\s+/', '', $generalHoliday));
    if (!empty($year) && !empty($holidays)) {
      foreach ($holidays as &$holiday) {
        if (strlen($holiday < 6)) {
          $holiday = str_replace('/', '-', "$holiday-$year");
        }
        if (strlen($holiday > 10) && strstr($holiday, '-') !== FALSE) {
          [$startDate, $endDate] = explode('-', $holiday);
          $rangeHolidays = array_merge($rangeHolidays, $this->generateDateRange($startDate, $endDate));
          unset($holiday);
          continue;
        }
        $holiday = date('Y-m-d', strtotime($holiday));
      }
    }
    if (!empty($rangeHolidays)) {
      $holidays = array_merge($holidays, $rangeHolidays);
    }
    return $holidays;
  }

  /**
   * {@inheritdoc}
   */
  public function generateDateRange($startDate, $endDate) {
    $dates = [];
    $currentDate = strtotime($startDate);
    $lastDate = strtotime($endDate);

    while ($currentDate <= $lastDate) {
      $dates[] = date('Y-m-d', $currentDate);
      $currentDate = strtotime('+1 day', $currentDate);
    }

    return $dates;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc