Test Failed
Push — master ( 6fa527...85e920 )
by Davide
51:34 queued 26:11
created

LaravelEventsCalendar   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Test Coverage

Coverage 91.91%

Importance

Changes 17
Bugs 6 Features 7
Metric Value
wmc 36
eloc 133
c 17
b 6
f 7
dl 0
loc 317
ccs 125
cts 136
cp 0.9191
rs 9.52
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar;
4
5
class LaravelEventsCalendar
6
{
7
    /***************************************************************************/
8
9
    /**
10
     * Format a date from datepicker (d/m/Y) to a format ready to be stored on DB (Y-m-d).
11
     * If the date picker date is null return today's date.
12
     * the PARAM is a date in the d/m/Y format - the RETURN is a date in the Y-m-d format.
13
     * If $todaysDateIfNull = 1, when the date is null return the date of today.
14
     *
15
     * @param  string  $DatePickerDate
16
     * @param  bool  $todaysDateIfNull
17
     * @return string  $ret
18
     */
19 1
    public static function formatDatePickerDateForMysql($DatePickerDate, bool $todaysDateIfNull = null)
20
    {
21 1
        if ($DatePickerDate) {
22 1
            [$tid, $tim, $tiy] = explode('/', $DatePickerDate);
23 1
            $ret = "$tiy-$tim-$tid";
24 1
        } elseif ($todaysDateIfNull) {
25 1
            date_default_timezone_set('Europe/Rome');
26 1
            $ret = date('Y-m-d', time());
27
        } else {
28 1
            $ret = '';
29
        }
30
31 1
        return $ret;
32
    }
33
34
    /***************************************************************************/
35
36
    /**
37
     * It returns a string that is composed by the array values separated by a comma.
38
     *
39
     * @param  iterable  $items
40
     * @return string  $ret
41
     */
42 4
    public function getStringFromArraySeparatedByComma(iterable $items)
43
    {
44 4
        $ret = '';
45 4
        $i = 0;
46 4
        $len = count($items); // to put "," to all items except the last
47
48 4
        foreach ($items as $key => $item) {
49 4
            $ret .= $item;
50 4
            if ($i != $len - 1) {  // not last
51 2
                $ret .= ', ';
52
            }
53 4
            $i++;
54
        }
55
56 4
        return $ret;
57
    }
58
59
    /***************************************************************************/
60
61
    /**
62
     * Check the date and return true if the weekday is the one specified in $dayOfTheWeek. eg. if $dayOfTheWeek = 3, is true if the date is a Wednesday
63
     * $dayOfTheWeek: 1|2|3|4|5|6|7 (MONDAY-SUNDAY)
64
     * https://stackoverflow.com/questions/2045736/getting-all-dates-for-mondays-and-tuesdays-for-the-next-year.
65
     *
66
     * @param  string $date
67
     * @param  int $dayOfTheWeek
68
     * @return bool
69
     */
70 4
    public function isWeekDay(string $date, int $dayOfTheWeek)
71
    {
72
        // Fix the bug that was avoiding to save Sunday. Date 'w' identify sunday as 0 and not 7.
73 4
        if ($dayOfTheWeek == 7) {
74 1
            $dayOfTheWeek = 0;
75
        }
76
77 4
        return date('w', strtotime($date)) == $dayOfTheWeek;
78
    }
79
80
    /***************************************************************************/
81
82
    /**
83
     * GET number of the specified weekday in this month (1 for the first).
84
     * $dateTimestamp - unix timestramp of the date specified
85
     * $dayOfWeekValue -  1 (for Monday) through 7 (for Sunday)
86
     * Return the number of the week in the month of the weekday specified.
87
     * @param  string $dateTimestamp
88
     * @param  string $dayOfWeekValue
89
     * @return int
90
     */
91 2
    public function weekdayNumberOfMonth(string $dateTimestamp, string $dayOfWeekValue)
92
    {
93 2
        $cut = substr($dateTimestamp, 0, 8);
94 2
        $daylen = 86400;
95 2
        $timestamp = strtotime($dateTimestamp);
96 2
        $first = strtotime($cut.'01');
97 2
        $elapsed = (($timestamp - $first) / $daylen) + 1;
98 2
        $i = 1;
99 2
        $weeks = 0;
100 2
        for ($i == 1; $i <= $elapsed; $i++) {
101 2
            $dayfind = $cut.(strlen($i) < 2 ? '0'.$i : $i);
102 2
            $daytimestamp = strtotime($dayfind);
103 2
            $day = strtolower(date('N', $daytimestamp));
104 2
            if ($day == strtolower($dayOfWeekValue)) {
105 1
                $weeks++;
106
            }
107
        }
108 2
        if ($weeks == 0) {
109 1
            $weeks++;
110
        }
111
112 2
        return $weeks;
113
    }
114
115
    /***************************************************************************/
116
117
    /**
118
     * GET number of week from the end of the month - https://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month
119
     * Week of the month = Week of the year - Week of the year of first day of month + 1.
120
     * Return the number of the week in the month of the day specified
121
     * $when - unix timestramp of the date specified.
122
     *
123
     * @param  int $when
124
     * @return int
125
     */
126 2
    public function weekOfMonthFromTheEnd(int $when)
127
    {
128 2
        $numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31
129 2
        $lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date
130 2
        $dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth;
131
132
        switch (true) {
133 2
            case $dayDifference < 7:
134
                $weekFromTheEnd = 1;
135
                break;
136
137 2
            case $dayDifference < 14:
138
                $weekFromTheEnd = 2;
139
                break;
140
141 2
            case $dayDifference < 21:
142 1
                $weekFromTheEnd = 3;
143 1
                break;
144
145 1
            case $dayDifference < 28:
146 1
                $weekFromTheEnd = 4;
147 1
                break;
148
149
            default:
150
                $weekFromTheEnd = 5;
151
                break;
152
        }
153
154 2
        return $weekFromTheEnd;
155
    }
156
157
    /***************************************************************************/
158
159
    /**
160
     * GET number of day from the end of the month.
161
     * $when - unix timestramp of the date specified
162
     * Return the number of day of the month from end.
163
     *
164
     * @param  int $when
165
     * @return int
166
     */
167 2
    public function dayOfMonthFromTheEnd(int $when)
168
    {
169 2
        $numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31
170 2
        $lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date
171 2
        $dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth;
172
173 2
        return $dayDifference;
174
    }
175
176
    /***************************************************************************/
177
178
    /**
179
     * GET the ordinal indicator - for the day of the month.
180
     * Return the ordinal indicator (st, nd, rd, th).
181
     * @param  int $number
182
     * @return string
183
     */
184 1
    public function getOrdinalIndicator($number)
185
    {
186
        switch ($number) {
187 1
            case  $number == 1 || $number == 21 || $number == 31:
188 1
                $ret = 'st';
189 1
                break;
190 1
            case  $number == 2 || $number == 22:
191
                $ret = 'nd';
192
                break;
193 1
            case  $number == 3 || $number == 23:
194
                $ret = 'rd';
195
                break;
196
            default:
197 1
                $ret = 'th';
198 1
                break;
199
        }
200
201 1
        return $ret;
202
    }
203
204
    /***************************************************************************/
205
206
    /**
207
     * Decode the event repeat_weekly_on field - used in event.show.
208
     * Return a string like "Monday".
209
     *
210
     * @param  string $repeatWeeklyOn
211
     * @return string
212
     */
213 2
    public function decodeRepeatWeeklyOn(string $repeatWeeklyOn)
214
    {
215
        $weekdayArray = [
216 2
            '',
217 2
            __('laravel-events-calendar::general.monday'),
218 2
            __('laravel-events-calendar::general.tuesday'),
219 2
            __('laravel-events-calendar::general.wednesday'),
220 2
            __('laravel-events-calendar::general.thursday'),
221 2
            __('laravel-events-calendar::general.friday'),
222 2
            __('laravel-events-calendar::general.saturday'),
223 2
            __('laravel-events-calendar::general.sunday'),
224
        ];
225 2
        $ret = $weekdayArray[$repeatWeeklyOn];
226
227 2
        return $ret;
228
    }
229
230
    /***************************************************************************/
231
232
    /**
233
     * Decode the event on_monthly_kind field - used in event.show.
234
     * Return a string like "the 4th to last Thursday of the month".
235
     *
236
     * @param  string $onMonthlyKindCode
237
     * @return string
238
     */
239 2
    public function decodeOnMonthlyKind(string $onMonthlyKindCode)
240
    {
241 2
        $onMonthlyKindCodeArray = explode('|', $onMonthlyKindCode);
242
        $weekDays = [
243 2
            '',
244 2
            __('laravel-events-calendar::general.monday'),
245 2
            __('laravel-events-calendar::general.tuesday'),
246 2
            __('laravel-events-calendar::general.wednesday'),
247 2
            __('laravel-events-calendar::general.thursday'),
248 2
            __('laravel-events-calendar::general.friday'),
249 2
            __('laravel-events-calendar::general.saturday'),
250 2
            __('laravel-events-calendar::general.sunday'),
251
        ];
252
253
        //dd($onMonthlyKindCodeArray);
254 2
        switch ($onMonthlyKindCodeArray[0]) {
255 2
            case '0':  // 0|7 eg. the 7th day of the month
256 2
                $dayNumber = $onMonthlyKindCodeArray[1];
257 2
                $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month');
258 2
                $ret = sprintf($format, __('laravel-events-calendar::general.day'));
259 2
                break;
260 1
            case '1':  // 1|2|4 eg. the 2nd Thursday of the month
261 1
                $dayNumber = $onMonthlyKindCodeArray[1];
262 1
                $weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday
263 1
                $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month');
264 1
                $ret = sprintf($format, $weekDay);
265 1
                break;
266 1
            case '2': // 2|20 eg. the 21st to last day of the month
267 1
                $dayNumber = $onMonthlyKindCodeArray[1] + 1;
268 1
                $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month');
269 1
                $ret = sprintf($format, __('laravel-events-calendar::general.day'));
270 1
                break;
271 1
            case '3': // 3|3|4 eg. the 4th to last Thursday of the month
272 1
                $dayNumber = $onMonthlyKindCodeArray[1] + 1;
273 1
                $weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday
274 1
                $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month');
275 1
                $ret = sprintf($format, $weekDay);
276 1
                break;
277
        }
278
279 2
        return $ret;
280
    }
281
282
    /***************************************************************************/
283
284
    /**
285
     * Return the GPS coordinates of the venue
286
     * https://developer.mapquest.com/.
287
     *
288
     * @param  string $address
289
     * @return array $ret
290
     */
291 4
    public static function getVenueGpsCoordinates(string $address)
292
    {
293 4
        $key = 'Ad5KVnAISxX6aHyj6fAnHcKeh30n4W60';
294 4
        $response = @file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key='.$key.'&location='.$address);
295 4
        $response = json_decode($response, true);
296
297 4
        $ret = [];
298 4
        $ret['lat'] = $response['results'][0]['locations'][0]['latLng']['lat'];
299 4
        $ret['lng'] = $response['results'][0]['locations'][0]['latLng']['lng'];
300
301 4
        return $ret;
302
    }
303
304
    /***************************************************************************/
305
306
    /**
307
     * Return a string with the list of the collection id separated by comma, 
308
     * without any space. eg. "354,320,310"
309
     *
310
     * @param  iterable $items
311
     * @return string $ret
312
     */
313 1
    public static function getCollectionIdsSeparatedByComma(iterable $items)
314
    {
315 1
        $itemsIds = [];
316 1
        foreach ($items as $item) {
317
            array_push($itemsIds, $item->id);
318
        }
319 1
        $ret = implode(',', $itemsIds);
320
321 1
        return $ret;
322
    }
323
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL, expecting T_FUNCTION or T_CONST on line 323 at column 0
Loading history...
324
    
325
    /***************************************************************************/
326
327
    /**
328
     * Return a string that describe the report misuse reason
329
     *
330
     * @param  int $reason
331
     * @return string $ret
332
     */
333
    public static function getReportMisuseReasonDescription(int $reason)
334
    {
335
        switch ($reason) {
336
            case '1':
337
                $ret = 'Not about Contact Improvisation';
338
                break;
339
            case '2':
340
                $ret = 'Contains wrong informations';
341
                break;
342
            case '3':
343
                $ret = 'It is not translated in english';
344
                break;
345
            case '4':
346
                $ret = 'Other (specify in the message)';
347
                break;
348
        }
349
        return $ret;
350
    }
351
    
352
=======
353
>>>>>>> 6fa527b7882a04bd7c0189ed6083cbcabaf74a16
354
}
355