Issues (3)

src/TransDate.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Alkoumi\CarbonDateTranslator;
4
5
    use Carbon\Carbon;
6
    use Illuminate\Support\Str;
0 ignored issues
show
The type Illuminate\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
    /*
9
     * this class translates carbon date difference to arabic
10
     */
11
    class TransDate
12
    {
13
        public static function inArabic($theDate)
14
        {
15
            // Define The Zaman after or before
16
            $theDate < Carbon::now() ? $zaman = 'قبل ' : $zaman = 'بعد ';
17
            $theDate = $theDate->diffForHumans();
18
            $dateArray = explode(' ', $theDate); //return $dateArray; // ["1","day","ago"]
19
            //$period is number, $dateArray[1] is the time $dateArray[0] , $dateArray[2] is the word ago
20
            $time = $dateArray[1];
21
            $period = $dateArray[0];
22
            //$dateArray[0] >= 3 ?  $period = (new self)->TransNumbers($dateArray[0]) : $period = $dateArray[0];
23
            if ((new self())->isSecond($time)) {
24
                return (new self())->handelSeconds($period, $zaman);
25
            }
26
            // handl minutes
27
            if ((new self())->isMinute($time)) {
28
                return (new self())->handelMinutes($period, $zaman);
29
            }
30
            // handl hours
31
            if ((new self())->isHour($time)) {
32
                return (new self())->handelHours($period, $zaman);
33
            }
34
            // handl days
35
            if ((new self())->isDay($time)) {
36
                return (new self())->handelDays($period, $zaman);
37
            }
38
            // handl weeks
39
            if ((new self())->isWeek($time)) {
40
                return (new self())->handelWeeks($period, $zaman);
41
            }
42
            // handl months
43
            if ((new self())->isMonth($time)) {
44
                return (new self())->handelMonths($period, $zaman);
45
            }
46
            // handl years
47
            if ((new self())->isYear($time)) {
48
                return (new self())->handelYears($period, $zaman);
49
            }
50
        }
51
52
        public function isSecond($time)
53
        {
54
            Str::startsWith($time, 'second') ? $isSecond = true : $isSecond = false;
55
56
            return $isSecond;
57
        }
58
59
        public function isMinute($time)
60
        {
61
            Str::startsWith($time, 'minute') ? $isMinute = true : $isMinute = false;
62
63
            return $isMinute;
64
        }
65
66
        public function isHour($time)
67
        {
68
            Str::startsWith($time, 'hour') ? $isHour = true : $isHour = false;
69
70
            return $isHour;
71
        }
72
73
        public function isDay($time)
74
        {
75
            Str::startsWith($time, 'day') ? $isDay = true : $isDay = false;
76
77
            return $isDay;
78
        }
79
80
        public function isWeek($time)
81
        {
82
            Str::startsWith($time, 'week') ? $isWeek = true : $isWeek = false;
83
84
            return $isWeek;
85
        }
86
87
        public function isMonth($time)
88
        {
89
            Str::startsWith($time, 'month') ? $isMonth = true : $isMonth = false;
90
91
            return $isMonth;
92
        }
93
94
        public function isYear($time)
95
        {
96
            Str::startsWith($time, 'year') ? $isYear = true : $isYear = false;
97
98
            return $isYear;
99
        }
100
101
        public function handelSeconds($period, $zaman)
102
        {
103
            if ($period == 1) {
104
                return $zaman.'ثانية واحدة';
105
            } elseif ($period == 2) {
106
                return $zaman.'ثانيتين';
107
            } elseif ($period >= 3 && $period <= 10) {
108
                return $zaman.$this->TransNumbers($period).' ثواني';
109
            } else {
110
                return $zaman.$this->TransNumbers($period).' ثانية';
111
            }
112
        }
113
114
        public function handelMinutes($period, $zaman)
115
        {
116
            if ($period == 1) {
117
                return $zaman.'دقيقة واحدة';
118
            } elseif ($period == 2) {
119
                return $zaman.'دقيقتين';
120
            } elseif ($period >= 3 && $period <= 10) {
121
                return $zaman.$this->TransNumbers($period).' دقائق';
122
            } else {
123
                return $zaman.$this->TransNumbers($period).' دقيقة';
124
            }
125
        }
126
127
        public function handelHours($period, $zaman)
128
        {
129
            if ($period == 1) {
130
                return $zaman.'ساعة';
131
            } elseif ($period == 2) {
132
                return $zaman.'ساعتين';
133
            } elseif ($period >= 3 && $period <= 10) {
134
                return $zaman.$this->TransNumbers($period).' ساعات';
135
            } else {
136
                return $zaman.$this->TransNumbers($period).' ساعة';
137
            }
138
        }
139
140
        public function handelDays($period, $zaman)
141
        {
142
            if ($period == 1) {
143
                return $zaman.' يوم';
144
            } elseif ($period == 2) {
145
                return $zaman.'يومين';
146
            } elseif ($period >= 3 && $period <= 10) {
147
                return $zaman.$this->TransNumbers($period).' أيام';
148
            } else {
149
                return $zaman.$this->TransNumbers($period).' يوم';
150
            }
151
        }
152
153
        public function handelWeeks($period, $zaman)
154
        {
155
            if ($period == 1) {
156
                return $zaman.' أسبوع';
157
            } elseif ($period == 2) {
158
                return $zaman.' أسبوعين';
159
            } elseif ($period >= 3 && $period <= 10) {
160
                return $zaman.$this->TransNumbers($period).' أسابيع';
161
            } else {
162
                return $zaman.$this->TransNumbers($period).' أسبوع';
163
            }
164
        }
165
166
        public function handelMonths($period, $zaman)
167
        {
168
            if ($period == 1) {
169
                return $zaman.'شهر';
170
            } elseif ($period == 2) {
171
                return $zaman.' شهرين';
172
            } elseif ($period >= 3 && $period <= 10) {
173
                return $zaman.$this->TransNumbers($period).' شهور';
174
            } else {
175
                return $zaman.$this->TransNumbers($period).' شهر';
176
            }
177
        }
178
179
        public function handelYears($period, $zaman)
180
        {
181
            if ($period == 1) {
182
                return $zaman.'سنة';
183
            } elseif ($period == 2) {
184
                return $zaman.' سنتين';
185
            } elseif ($period >= 3 && $period <= 10) {
186
                return $zaman.$this->TransNumbers($period).' سنوات';
187
            } else {
188
                return $zaman.$this->TransNumbers($period).' سنة';
189
            }
190
        }
191
192
        protected function TransNumbers($value)
193
        {
194
            if (is_string($value)) {
195
                $arabic_eastern = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
196
                $arabic_western = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
197
198
                return str_replace($arabic_western, $arabic_eastern, $value);
199
            }
200
201
            return $value;
202
        }
203
    }
204