Test Failed
Push — develop ( 37d80d...39822a )
by Paul
08:38
created

SiteReviewsSummaryDefaults::finalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Defaults;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
7
class SiteReviewsSummaryDefaults 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
        'debug' => 'bool',
15
        'schema' => 'bool',
16
        'terms' => 'string',
17
    ];
18
19
    /**
20
     * The values that should be constrained after sanitization is run.
21
     * This is done after $casts and $sanitize.
22
     */
23
    public array $enums = [
24
        'terms' => ['0', 'false', '1', 'true'],
25
    ];
26
27
    /**
28
     * The values that should be guarded.
29
     *
30
     * @var string[]
31
     */
32
    public array $guarded = [
33
        'labels', 'text', 'title',
34
    ];
35
36
    /**
37
     * The keys that should be mapped to other keys.
38
     * Keys are mapped before the values are normalized and sanitized.
39
     * Note: Mapped keys should not be included in the defaults!
40
     */
41
    public array $mapped = [
42
        'assigned_to' => 'assigned_posts',
43
        'category' => 'assigned_terms',
44
        'user' => 'assigned_users',
45
    ];
46
47
    /**
48
     * The values that should be sanitized.
49
     * This is done after $casts and before $enums.
50
     */
51
    public array $sanitize = [
52
        'class' => 'attr-class',
53
        'hide' => 'array-string',
54
        'id' => 'id-hash',
55
        'labels' => 'text',
56
        'rating' => 'rating',
57
        'rating_field' => 'name',
58
        'text' => 'text-html:a',
59
        'title' => 'text',
60
        'type' => 'slug',
61
    ];
62
63
    protected function defaults(): array
64
    {
65
        return [
66
            'assigned_posts' => '',
67
            'assigned_terms' => '',
68
            'assigned_users' => '',
69
            'class' => '',
70
            'debug' => false,
71
            'hide' => '',
72
            'id' => '',
73
            'labels' => '',
74
            'rating' => 1,
75
            'rating_field' => 'rating', // used for custom rating fields
76
            'schema' => false,
77
            'terms' => '',
78
            'text' => '',
79
            'title' => '',
80
            'type' => 'local',
81
        ];
82
    }
83
84
    /**
85
     * Finalize provided values, this always runs last.
86
     */
87
    protected function finalize(array $values = []): array
88
    {
89
        $values['rating'] = max(1, $values['rating']);
90
        return $values;
91
    }
92
93
    /**
94
     * Normalize provided values, this always runs first.
95
     */
96
    protected function normalize(array $values = []): array
97
    {
98
        foreach ($this->mapped as $old => $new) {
99
            if ('custom' === Arr::get($values, $old)) {
100
                $values[$old] = Arr::get($values, $new);
101
            }
102
        }
103
        return parent::normalize($values);
104
    }
105
}
106