Skip to content

Commit 47629e1

Browse files
committed
fixed PHP 8.4 syntax compatibility
1 parent 6259080 commit 47629e1

File tree

4 files changed

+134
-88
lines changed

4 files changed

+134
-88
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# ZAMAN - Laravel 5+ Jalali datetime component:
2-
This component based on PHP International (php-intl) extension so php-intl extension must be installed on your web server.
1+
# ZAMAN - Jalali datetime component for Laravel:
2+
This component is based on native PHP International (php-intl) extension,
3+
So php-intl extension must be installed on your server.
34

45
known as:
56

@@ -10,6 +11,8 @@ known as:
1011
- تقویم خورشیدی
1112
- تاریخ شمسی
1213

14+
Supprts Laravel 5+ and PHP 8.1+
15+
1316
## php-intl extension installation
1417

1518
- On windows servers, open your php.ini (which should be in Program Files/PHP), and simply uncomment the extension.
@@ -24,7 +27,7 @@ sudo apt-get install php-intl
2427

2528
- Redhat based Linux (Redhat/Centos/ ...)
2629
```
27-
sudo yum -y install php-intl
30+
sudo dnf -y install php-intl
2831
```
2932
Restart your webserver - done.
3033

@@ -36,7 +39,7 @@ composer require php-monsters/laravel-jalali-date
3639

3740
### Integration with Laravel 5.*
3841

39-
Add Zaman to app aliases in config/app.php file
42+
Add package alias to your app aliases (only for Laravel < 5.5):
4043

4144
```php
4245
// aliases
@@ -47,7 +50,7 @@ Add Zaman to app aliases in config/app.php file
4750

4851
```php
4952

50-
// Jalali to Gregraian samples
53+
// Jalali to Gregorian samples
5154
echo Zaman::jTog('next week');
5255
echo Zaman::jTog('now');
5356
echo Zaman::jTog('1396-06-30 05:30:10');
@@ -58,12 +61,23 @@ echo Zaman::gToj('2 days ago');
5861
echo Zaman::gToj('2010-10-24 22:50:14');
5962
echo Zaman::gToj('2014-09-21 07:12:54', 'EEEE yyyy/MMMM/dd H:m:s');
6063

61-
6264
// Moment samples
63-
echo Zaman::moment(strtotime('3 hours ago')); // "3 ساعت قبل"
64-
echo Zaman::moment(strtotime('2017-01-02 00:10:20')); // "2 هفته قبل"
65-
echo Zaman::momentEn(1494328806); // "May 2017"
66-
echo Zaman::momentEn(1494334506); // "last month"
65+
// JALALI moment
66+
echo Zaman::moment(strtotime('3 hours ago'), Zaman::CAL_JALALI); // "3 ساعت قبل"
67+
echo Zaman::momentJalali(strtotime('3 hours ago')); // "3 ساعت قبل"
68+
echo Zaman::moment(strtotime('2017-01-02 00:10:20'), Zaman::CAL_JALALI); // "2 هفته قبل"
69+
echo Zaman::momentJalali(strtotime('2017-01-02 00:10:20')); // "2 هفته قبل"
70+
71+
// GREGORIAN moment
72+
echo Zaman::moment('now', Zaman::CAL_GREGORIAN); // "May 2017"
73+
echo Zaman::momentGregorian(1494328806); // "May 2017"
74+
echo Zaman::moment('1 month ago', Zaman::CAL_GREGORIAN); // "last month"
75+
echo Zaman::momentGregorian(1494334506); // "last month"
76+
77+
// HIJRI moment
78+
// ISN'T implemented yet!!!
79+
// echo Zaman::moment('now', Zaman::CAL_HIJRI);
80+
// echo Zaman::momentHijri(1494328806);
6781

6882
// Blade usage example
6983
{{ Zaman::gToj('2011-11-20 19:12:19') }}

src/Zaman/Facades/Zaman.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
*/
1212
class Zaman extends Facade
1313
{
14+
const CAL_GREGORIAN = 0;
15+
const CAL_JALALI = 1;
16+
const CAL_HIJRI = 2;
1417
/**
1518
* Get the registered name of the component.
1619
*
1720
* @return string
1821
*/
1922
protected static function getFacadeAccessor ()
2023
{
21-
return 'PhpMonsters\Zaman\Helpers\PersianDateHelper';
24+
return 'PhpMonsters\Zaman\Helpers\ZamanDateHelper';
2225
}
2326
}

src/Zaman/Helpers/PersianDateHelper.php renamed to src/Zaman/Helpers/ZamanDateHelper.php

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,35 @@
33
namespace PhpMonsters\Zaman\Helpers;
44

55
use Exception;
6+
use PhpMonsters\Zaman\Facades\Zaman;
67
use PhpMonsters\Zaman\IntlDatetime;
8+
use RuntimeException;
79

810
/**
911
* Class PersianDateHelper
1012
* @package PhpMonsters\Zaman
1113
* @author Aboozar Ghaffari <aboozar.ghf@gmail.com>
1214
*/
13-
class PersianDateHelper
15+
class ZamanDateHelper
1416
{
1517
/**
1618
* Gregorian to Jalali
1719
*
18-
* @param string $date
19-
* @param string $format
20-
* @param string $locale
21-
* @param string|null $timezone
20+
* @param string $date
21+
* @param string $format
22+
* @param string $locale
23+
* @param string|null $timezone
2224
* @return string
2325
* @throws Exception
2426
* @link http://userguide.icu-project.org/formatparse/datetime
2527
*/
2628
public function gToj(
27-
mixed $date,
28-
string $format = 'yyyy/MM/dd H:m:s',
29-
string $locale = 'fa',
30-
string $timezone = null
31-
): string {
29+
mixed $date,
30+
string $format = 'yyyy/MM/dd H:m:s',
31+
string $locale = 'fa',
32+
?string $timezone = null
33+
): string
34+
{
3235
$date = new IntlDatetime($date, $timezone, 'gregorian');
3336
$date->setCalendar('persian');
3437
$date->setLocale($locale);
@@ -39,11 +42,11 @@ public function gToj(
3942
/**
4043
* Jalali to Gregorian
4144
*
42-
* @param mixed $date
43-
* @param string $format
44-
* @param string $inputLocale
45-
* @param string $locale
46-
* @param string $timezone
45+
* @param mixed $date
46+
* @param string $format
47+
* @param string $inputLocale
48+
* @param string $locale
49+
* @param string $timezone
4750
* @return string
4851
* @throws Exception
4952
* @link http://userguide.icu-project.org/formatparse/datetime
@@ -54,7 +57,8 @@ public function jTog(
5457
string $inputLocale = 'fa',
5558
string $locale = 'en',
5659
string $timezone = 'Asia/Tehran'
57-
): string {
60+
): string
61+
{
5862
$date = new IntlDatetime($date, $timezone, 'persian', $inputLocale);
5963

6064
$date->setCalendar('Gregorian');
@@ -64,11 +68,25 @@ public function jTog(
6468
}
6569

6670
/**
67-
* @param string $timestamp timestamp
71+
* @param string $timestamp timestamp
72+
* @param int $calendar
73+
* @return string
74+
*/
75+
public function moment(string $timestamp, int $calendar): string
76+
{
77+
return match ($calendar) {
78+
Zaman::CAL_JALALI => $this->momentJalali($timestamp),
79+
Zaman::CAL_HIJRI => $this->momentHijri($timestamp),
80+
default => $this->momentGregorian($timestamp),
81+
};
82+
}
83+
84+
/**
85+
* @param string $timestamp timestamp
6886
*
6987
* @return string
7088
*/
71-
public function moment(string $timestamp): string
89+
public function momentJalali(string $timestamp): string
7290
{
7391
if (!ctype_digit($timestamp)) {
7492
$timestamp = strtotime($timestamp);
@@ -78,7 +96,7 @@ public function moment(string $timestamp): string
7896
return 'اکنون';
7997
}
8098

81-
$dayDiff = (int) floor($diff / 86400);
99+
$dayDiff = (int)floor($diff / 86400);
82100

83101
if ($diff > 0) {
84102
if ($dayDiff === 0) {
@@ -89,23 +107,23 @@ public function moment(string $timestamp): string
89107
return 'یک دقیقه قبل';
90108
}
91109
if ($diff < 3600) {
92-
return floor($diff / 60).' دقیقه قبل';
110+
return floor($diff / 60) . ' دقیقه قبل';
93111
}
94112
if ($diff < 7200) {
95113
return 'یک ساعت پیش';
96114
}
97115
if ($diff < 86400) {
98-
return floor($diff / 3600).' ساعت قبل';
116+
return floor($diff / 3600) . ' ساعت قبل';
99117
}
100118
}
101119
if ($dayDiff === 1) {
102120
return 'دیروز';
103121
}
104122
if ($dayDiff < 7) {
105-
return $dayDiff.' روز قبل';
123+
return $dayDiff . ' روز قبل';
106124
}
107125
if ($dayDiff < 31) {
108-
return ceil($dayDiff / 7).' هفته قبل';
126+
return ceil($dayDiff / 7) . ' هفته قبل';
109127
}
110128
if ($dayDiff < 60) {
111129
return 'ماه گذشته';
@@ -120,13 +138,13 @@ public function moment(string $timestamp): string
120138
return 'یک دقیقه پیش';
121139
}
122140
if ($diff < 3600) {
123-
return floor($diff / 60).' دقیقه پیش';
141+
return floor($diff / 60) . ' دقیقه پیش';
124142
}
125143
if ($diff < 7200) {
126144
return 'یک ساعت پیش';
127145
}
128146
if ($diff < 86400) {
129-
return floor($diff / 3600).' ساعت پیش';
147+
return floor($diff / 3600) . ' ساعت پیش';
130148
}
131149
}
132150
if ($dayDiff === 1) {
@@ -139,21 +157,21 @@ public function moment(string $timestamp): string
139157
return 'هفته بعد';
140158
}
141159
if (ceil($dayDiff / 7) < 4) {
142-
return 'در '.ceil($dayDiff / 7).' هفته';
160+
return 'در ' . ceil($dayDiff / 7) . ' هفته';
143161
}
144-
if ((int) date('n', $timestamp) === (int) date('n') + 1) {
162+
if ((int)date('n', $timestamp) === (int)date('n') + 1) {
145163
return 'ماه بعد';
146164
}
147165

148166
return date('F Y', $timestamp);
149167
}
150168

151169
/**
152-
* @param string $timestamp timestamp
170+
* @param string $timestamp timestamp
153171
*
154-
* @return false|string
172+
* @return string
155173
*/
156-
public function momentEn(string $timestamp): bool|string
174+
public function momentGregorian(string $timestamp): string
157175
{
158176
if (!ctype_digit($timestamp)) {
159177
$timestamp = strtotime($timestamp);
@@ -163,7 +181,7 @@ public function momentEn(string $timestamp): bool|string
163181
return 'now';
164182
}
165183

166-
$dayDiff = (int) floor($diff / 86400);
184+
$dayDiff = (int)floor($diff / 86400);
167185

168186
if ($diff > 0) {
169187
if ($dayDiff === 0) {
@@ -174,23 +192,23 @@ public function momentEn(string $timestamp): bool|string
174192
return '1 minute ago';
175193
}
176194
if ($diff < 3600) {
177-
return floor($diff / 60).' minutes ago';
195+
return floor($diff / 60) . ' minutes ago';
178196
}
179197
if ($diff < 7200) {
180198
return '1 hour ago';
181199
}
182200
if ($diff < 86400) {
183-
return floor($diff / 3600).' hours ago';
201+
return floor($diff / 3600) . ' hours ago';
184202
}
185203
}
186204
if ($dayDiff === 1) {
187205
return 'Yesterday';
188206
}
189207
if ($dayDiff < 7) {
190-
return $dayDiff.' days ago';
208+
return $dayDiff . ' days ago';
191209
}
192210
if ($dayDiff < 31) {
193-
return ceil($dayDiff / 7).' weeks ago';
211+
return ceil($dayDiff / 7) . ' weeks ago';
194212
}
195213
if ($dayDiff < 60) {
196214
return 'last month';
@@ -205,13 +223,13 @@ public function momentEn(string $timestamp): bool|string
205223
return 'in a minute';
206224
}
207225
if ($diff < 3600) {
208-
return 'in '.floor($diff / 60).' minutes';
226+
return 'in ' . floor($diff / 60) . ' minutes';
209227
}
210228
if ($diff < 7200) {
211229
return 'in an hour';
212230
}
213231
if ($diff < 86400) {
214-
return 'in '.floor($diff / 3600).' hours';
232+
return 'in ' . floor($diff / 3600) . ' hours';
215233
}
216234
}
217235
if ($dayDiff === 1) {
@@ -224,12 +242,22 @@ public function momentEn(string $timestamp): bool|string
224242
return 'next week';
225243
}
226244
if (ceil($dayDiff / 7) < 4) {
227-
return 'in '.ceil($dayDiff / 7).' weeks';
245+
return 'in ' . ceil($dayDiff / 7) . ' weeks';
228246
}
229-
if ((int) date('n', $timestamp) === (int) date('n') + 1) {
247+
if ((int)date('n', $timestamp) === (int)date('n') + 1) {
230248
return 'next month';
231249
}
232250

233251
return date('F Y', $timestamp);
234252
}
253+
254+
/**
255+
* @param string $timestamp timestamp
256+
*
257+
* @return string
258+
*/
259+
private function momentHijri(string $timestamp): string
260+
{
261+
throw new RuntimeException('Hijri moment has not implemented yet!');
262+
}
235263
}

0 commit comments

Comments
 (0)