Passed
Push — develop ( afff43...70b7ee )
by Paul
14:29
created

SiteReviewsDefaults   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 55
dl 0
loc 100
ccs 26
cts 27
cp 0.963
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A defaults() 0 22 1
A normalize() 0 8 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Defaults;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Modules\Rating;
7
8
class SiteReviewsDefaults extends DefaultsAbstract
9
{
10
    /**
11
     * The values that should be cast before sanitization is run.
12
     * This is done before $sanitize and $enums.
13
     */
14
    public array $casts = [
15
        'debug' => 'bool',
16
        'pagination' => 'string',
17
        'schema' => 'bool',
18
        'terms' => 'string',
19
        'verified' => 'string',
20
    ];
21
22
    /**
23
     * The values that should be constrained after sanitization is run.
24
     * This is done after $casts and $sanitize.
25
     */
26
    public array $enums = [
27
        'pagination' => ['ajax', 'loadmore', '1', 'true'],
28
        'terms' => ['0', 'false', '1', 'true'],
29
        'verified' => ['0', 'false', '1', 'true'],
30
    ];
31
32
    /**
33
     * The values that should be guarded.
34
     *
35
     * @var string[]
36
     */
37
    public array $guarded = [
38
        'fallback',
39
    ];
40
41
    /**
42
     * The keys that should be mapped to other keys.
43
     * Keys are mapped before the values are normalized and sanitized.
44
     * Note: Mapped keys should not be included in the defaults!
45
     */
46
    public array $mapped = [
47
        'assigned_to' => 'assigned_posts',
48
        'category' => 'assigned_terms',
49
        'className' => 'class',
50
        'per_page' => 'display',
51
        'user' => 'assigned_users',
52
    ];
53
54
    /**
55
     * The values that should be sanitized.
56
     * This is done after $casts and before $enums.
57
     */
58
    public array $sanitize = [
59
        'author' => 'user-id',
60
        'class' => 'attr-class',
61
        'display' => 'min:1',
62
        'fallback' => 'text-post',
63
        'hide' => 'array-string',
64
        'id' => 'id-unique',
65
        'offset' => 'min:0',
66
        'page' => 'min:1',
67
        'rating' => 'rating',
68
        'rating_field' => 'name',
69
        'type' => 'slug',
70
    ];
71
72 9
    protected function defaults(): array
73
    {
74 9
        return [
75 9
            'assigned_posts' => '',
76 9
            'assigned_terms' => '',
77 9
            'assigned_users' => '',
78 9
            'author' => 0,
79 9
            'class' => '',
80 9
            'display' => 5,
81 9
            'debug' => false,
82 9
            'fallback' => '',
83 9
            'hide' => [],
84 9
            'id' => '',
85 9
            'offset' => 0,
86 9
            'page' => 1,
87 9
            'pagination' => '',
88 9
            'rating' => Rating::min(),
89 9
            'rating_field' => 'rating', // used for custom rating fields
90 9
            'schema' => false,
91 9
            'terms' => '',
92 9
            'type' => '',
93 9
            'verified' => '',
94 9
        ];
95
    }
96
97
    /**
98
     * Normalize provided values, this always runs first.
99
     */
100 9
    protected function normalize(array $values = []): array
101
    {
102 9
        foreach ($this->mapped as $old => $new) {
103 9
            if ('custom' === Arr::get($values, $old)) { // @todo is this deprecated??
104
                $values[$old] = Arr::get($values, $new);
105
            }
106
        }
107 9
        return $values;
108
    }
109
}
110