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

AbstractStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 7
c 9
b 0
f 0
lcom 0
cbo 1
dl 0
loc 70
ccs 15
cts 17
cp 0.8824
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A insertedRecords() 0 4 1
A isoDateFromFormat() 0 8 2
A booleanFromFilterBooleans() 0 19 4
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