View source
<?php
namespace Drupal\hijri;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use ArUtil\I18N\Date;
class HijriFormatter extends DateFormatter {
protected $timezones;
protected $dateFormatStorage;
protected $languageManager;
protected $configFactory;
protected $requestStack;
protected $country = NULL;
protected $dateFormats = [];
protected $units = [
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1,
];
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
$this->dateFormatStorage = $entity_type_manager
->getStorage('date_format');
$this->languageManager = $language_manager;
$this->stringTranslation = $translation;
$this->configFactory = $config_factory;
$this->requestStack = $request_stack;
}
public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
$correction = 0;
if (!isset($timezone)) {
$timezone = date_default_timezone_get();
$correction = $this->configFactory
->get('hijri.config')
->get('correction_value');
}
if (!isset($this->timezones[$timezone])) {
$this->timezones[$timezone] = timezone_open($timezone);
}
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
if ($type !== 'custom') {
if ($date_format = $this
->dateFormat($type, $langcode)) {
$format = $date_format
->getPattern();
}
}
if (empty($format)) {
$format = $this
->dateFormat('fallback', $langcode)
->getPattern();
}
return $this
->hijri($format, $timestamp, $correction);
$create_settings = [
'langcode' => $langcode,
'country' => $this
->country(),
];
$settings = [
'langcode' => $langcode,
];
return $date
->format($format, $settings);
}
public function hijri($format, $timestamp = NULL, $correction = 0) {
$timestamp = $timestamp == NULL ? time() : $timestamp;
$patterns = [];
$replacements = [];
array_push($patterns, 'Y');
array_push($replacements, 'x1');
array_push($patterns, 'y');
array_push($replacements, 'x2');
array_push($patterns, 'M');
array_push($replacements, 'x3');
array_push($patterns, 'F');
array_push($replacements, 'x3');
array_push($patterns, 'n');
array_push($replacements, 'x4');
array_push($patterns, 'm');
array_push($replacements, 'x5');
array_push($patterns, 'j');
array_push($replacements, 'x6');
array_push($patterns, 'd');
array_push($replacements, 'x7');
array_push($patterns, 'S');
array_push($replacements, '');
$format = str_replace($patterns, $replacements, $format);
$date_result = \Drupal::service('date.formatter')
->format($timestamp, 'custom', $format);
if ($correction != 0) {
$timestamp = $timestamp + 60 * 60 * 24 * $correction;
}
list($y, $m, $d) = explode(' ', \Drupal::service('date.formatter')
->format($timestamp, 'custom', 'Y m d'));
$calc_hijri_date = (new Date())
->hjConvert($y, $m, $d);
$hijri_month_name = $this
->hijriMonthNames();
$patterns = [];
$replacements = [];
$patterns[] = 'x1';
$replacements[] = $calc_hijri_date[0];
$patterns[] = 'x2';
$replacements[] = substr($calc_hijri_date[0], -2);
$patterns[] = 'x3';
$replacements[] = $hijri_month_name[$calc_hijri_date[1]];
$patterns[] = 'x4';
$replacements[] = $calc_hijri_date[1];
$patterns[] = 'x5';
$replacements[] = sprintf('%02d', $calc_hijri_date[1]);
$patterns[] = 'x6';
$replacements[] = $calc_hijri_date[2];
$patterns[] = 'x7';
$replacements[] = sprintf('%02d', $calc_hijri_date[2]);
return str_replace($patterns, $replacements, $date_result);
}
public function hijriMonthNames() {
return [
'1' => (string) $this
->t('Muharram'),
'2' => (string) $this
->t('Safar'),
'3' => (string) $this
->t('Rabia al-Awwal'),
'4' => (string) $this
->t('Rabia ath-Thani'),
'5' => (string) $this
->t('Jumada al-Ula'),
'6' => (string) $this
->t('Jumada ath-Thaniya'),
'7' => (string) $this
->t('Rajab'),
'8' => (string) $this
->t('Shaban'),
'9' => (string) $this
->t('Ramadan'),
'10' => (string) $this
->t('Shawwal'),
'11' => (string) $this
->t('Dhu al-Qada'),
'12' => (string) $this
->t('Dhu al-Hijja'),
];
}
}