33namespace PhpMonsters \Zaman \Helpers ;
44
55use Exception ;
6+ use PhpMonsters \Zaman \Facades \Zaman ;
67use PhpMonsters \Zaman \IntlDatetime ;
8+ use RuntimeException ;
79
810/**
911 * Class PersianDateHelper
1012 * @package PhpMonsters\Zaman
1113 * @author Aboozar Ghaffari <[email protected] > 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