Passed
Push — master ( dd007d...0d66f5 )
by George
03:32
created

AbstractStore::isoDateFromFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace JsonTable\Store;
3
4
use \JsonTable\Base;
5
/**
6
 * Abstract store class.
7
 * All store classes should extent this class.
8
 *
9
 * @package JSON table
10
 */
11
abstract class AbstractStore extends Base
12
{
13
    /**
14
     * @var array The values of the primary key of the last inserted data.
15
     */
16
    protected $insertedIds = [];
17
18
19
    /**
20
     * Get the primary keys of the records inserted in the last call to store.
21
     *
22
     * @return array The primary keys.
23
     */
24
    public function insertedRecords()
25
    {
26
        return $this->insertedIds;
27
    }
28
29
30
    /**
31
     * Convert a date from a specific format into ISO date format of YYYY-MM-DD.
32
     *
33
     * @static
34
     *
35
     * @param   string  $format           The format to convert from.
36
     * @param   string  $dateToConvert    The date to convert.
37
     *
38
     * @return  string  The formatted date.
39
     *
40
     * @throws  \Exception is the date couldn't be reformatted.
41
     */
42 9
    public static function isoDateFromFormat($format, $dateToConvert)
43
    {
44 9
        if (!$date = \DateTime::createFromFormat($format, $dateToConvert)) {
45 5
            throw new \Exception("Could not reformat date $dateToConvert from format $format");
46
        }
47
48 4
        return $date->format('Y-m-d');
49
    }
50
51
52
    /**
53
     * Convert a value from being something that passes the FILTER_VALIDATE_BOOLEAN filter to be an actual boolean.
54
     *
55
     * @static
56
     *
57
     * @param   mixed   $value  The value to convert to a boolean.
58
     *
59
     * @return  boolean|null The converted value.
60
     */
61 21
    public static function booleanFromFilterBooleans($value)
62
    {
63 21
        if (is_string($value)) {
64 15
            $value = strtolower($value);
65 15
        }
66
67 21
        $truths = ['1', 1, true, 'on', 'yes', 'true'];
68 21
        $false = ['0', 0, false, 'off', 'no', 'false'];
69
70 21
        if (in_array($value, $truths, true)) {
71 8
            return true;
72
        }
73
74 13
        if (in_array($value, $false, true)) {
75 8
            return false;
76
        }
77
78 5
        return null;
79
    }
80
}
81