Completed
Push — master ( 0b864d...788c20 )
by Davide
10:44
created

getStringFromArraySeparatedByComma()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
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, $todaysDateIfNull = 0)
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 = null;
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  array  $array
40
     * @return string  $ret
41
     */
42 3
    public static function getStringFromArraySeparatedByComma($array)
43
    {
44 3
        $ret = "";
45 3
        $i = 0;
46 3
        $len = count($array); // to put "," to all items except the last
47
        
48 3
        foreach ($array as $key => $value) {
49 3
            $ret .= $value;
50 3
            if ($i != $len - 1) {  // not last
51 1
                $ret .= ', ';
52
            }
53 3
            $i++;
54
        }
55
56 3
        return $ret;
57
    }
58
59
}
60