Passed
Push — master ( 6df11d...8e8817 )
by Julien
05:21
created

Builder::setContextKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Support\Exposer;
13
14
class Builder implements BuilderInterface
15
{
16
    private $value;
17
    
18
    private $parent;
19
    
20
    private ?array $columns = null;
21
    
22
    private ?string $field = null;
23
    
24
    private ?string $key = null;
25
    
26
    private ?string $contextKey = null;
27
    
28
    private bool $expose = true;
29
    
30
    private bool $protected = false;
31
    
32
    
33 6
    public function getValue()
34
    {
35 6
        return $this->value;
36
    }
37
    
38 6
    public function setValue($value = null): void
39
    {
40 6
        $this->value = $value;
41
    }
42
    
43 2
    public function getParent()
44
    {
45 2
        return $this->parent;
46
    }
47
    
48 6
    public function setParent($parent = null): void
49
    {
50 6
        $this->parent = $parent;
51
    }
52
    
53 6
    public function getKey(): ?string
54
    {
55 6
        return $this->key;
56
    }
57
    
58 6
    public function setKey(?string $key = null): void
59
    {
60 6
        $this->key = self::processKey($key);
61
    }
62
    
63 6
    public function getContextKey(): ?string
64
    {
65 6
        return $this->contextKey;
66
    }
67
    
68 6
    public function setContextKey(?string $contextKey = null): void
69
    {
70 6
        $this->contextKey = self::processKey($contextKey);
71
    }
72
    
73
    public function getField(): string
74
    {
75
        return $this->field;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->field could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
76
    }
77
    
78
    public function setField(?string $field = null): void
79
    {
80
        $this->field = $field;
81
    }
82
    
83 6
    public function getColumns(): ?array
84
    {
85 6
        return $this->columns;
86
    }
87
    
88 6
    public function setColumns(?array $columns = null): void
89
    {
90 6
        $this->columns = $columns;
91
    }
92
    
93 6
    public function getExpose(): bool
94
    {
95 6
        return $this->expose;
96
    }
97
    
98 6
    public function setExpose(bool $expose): void
99
    {
100 6
        $this->expose = $expose;
101
    }
102
    
103 6
    public function getProtected(): bool
104
    {
105 6
        return $this->protected;
106
    }
107
    
108 6
    public function setProtected(bool $protected): void
109
    {
110 6
        $this->protected = $protected;
111
    }
112
    
113 6
    public function getFullKey(): ?string
114
    {
115 6
        $key = $this->getKey();
116 6
        $keyContext = $this->getContextKey();
117 6
        return $keyContext . (empty($key) ? null : (empty($keyContext) ? $key : '.' . $key));
118
    }
119
    
120 6
    public static function processKey(?string $key = null): ?string
121
    {
122 6
        return empty($key) || filter_var($key, FILTER_VALIDATE_INT) ? '' : mb_strtolower(
123 6
            trim(
124 6
                preg_replace(
125 6
                    '/\.+/',
126 6
                    '.',
127 6
                    preg_replace(
128 6
                        '/\s+/',
129 6
                        '.',
130 6
                        $key
131 6
                    )
132 6
                ),
133 6
                '.'
134 6
            )
135 6
        );
136
    }
137
}
138