Passed
Push — develop ( 391f8f...187ad6 )
by Paul
13:53
created

ReviewDefaults::normalize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.6111
cc 5
nc 4
nop 1
crap 5
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Defaults;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
7
class ReviewDefaults extends DefaultsAbstract
8
{
9
    /**
10
     * The values that should be cast before sanitization is run.
11
     * This is done before $sanitize and $enums.
12
     */
13
    public array $casts = [
14
        'author_id' => 'int',
15
        'ID' => 'int',
16
        'is_approved' => 'bool',
17
        'is_modified' => 'bool',
18
        'is_pinned' => 'bool',
19
        'is_verified' => 'bool',
20
        'rating' => 'int',
21
        'rating_id' => 'int',
22
        'terms' => 'bool',
23
    ];
24
25
    /**
26
     * The keys that should be mapped to other keys.
27
     * Keys are mapped before the values are normalized and sanitized.
28
     * Note: Mapped keys should not be included in the defaults!
29
     */
30
    public array $mapped = [
31
        'name' => 'author',
32
        'post_ids' => 'assigned_posts',
33
        'term_ids' => 'assigned_terms',
34
        'user_ids' => 'assigned_users',
35
    ];
36
37
    /**
38
     * The values that should be sanitized.
39
     * Sanitization is done in the following order: cast, sanitize, enums
40
     */
41
    public array $sanitize = [
42
        'assigned_posts' => 'array-int',
43
        'assigned_terms' => 'array-int',
44
        'assigned_users' => 'array-int',
45
        'author' => 'text',
46
        'avatar' => 'url',
47
        'content' => 'text-multiline',
48
        'date' => 'date',
49
        'date_gmt' => 'date',
50
        'email' => 'email',
51
        'ip_address' => 'ip-address',
52
        'response' => 'text-html',
53
        'score' => 'min:0',
54
        'status' => 'text',
55
        'title' => 'text',
56
        'type' => 'slug',
57
        'url' => 'url',
58
    ];
59
60 26
    protected function defaults(): array
61
    {
62 26
        return [
63 26
            'assigned_posts' => '',
64 26
            'assigned_terms' => '',
65 26
            'assigned_users' => '',
66 26
            'author' => '',
67 26
            'author_id' => '',
68 26
            'avatar' => '',
69 26
            'content' => '',
70 26
            'custom' => '',
71 26
            'date' => '',
72 26
            'date_gmt' => '',
73 26
            'email' => '',
74 26
            'ID' => '',
75 26
            'ip_address' => '',
76 26
            'is_approved' => false,
77 26
            'is_modified' => false,
78 26
            'is_pinned' => false,
79 26
            'is_verified' => false,
80 26
            'rating' => '',
81 26
            'rating_id' => '',
82 26
            'response' => '',
83 26
            'score' => '',
84 26
            'status' => '',
85 26
            'terms' => true,
86 26
            'title' => '',
87 26
            'type' => '',
88 26
            'url' => '',
89 26
        ];
90
    }
91
92
    /**
93
     * Normalize provided values, this always runs first (after keys are mapped).
94
     */
95 26
    protected function normalize(array $values = []): array
96
    {
97 26
        $date = Arr::getAs('string', $values, 'date');
98 26
        if (!empty($date) && '0000-00-00 00:00:00' === Arr::get($values, 'date_gmt')) {
99 1
            $values['date_gmt'] = get_gmt_from_date($date);
100
        }
101 26
        if (!empty($values['ID']) && !empty($values['review_id'])) {
102 25
            $values['rating_id'] = $values['ID'];
103 25
            $values['ID'] = $values['review_id'];
104
        }
105 26
        return $values;
106
    }
107
}
108