PreloadTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0
rs 10
ccs 10
cts 10
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 8 2
A filter() 0 6 2
preload() 0 1 ?
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the skeleton package.
7
 *
8
 * (c) Gennady Knyazkin <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Gennadyx\Skeleton\VarSource;
15
16
/**
17
 * Trait PreloadTrait
18
 *
19
 * @author Gennady Knyazkin <[email protected]>
20
 */
21
trait PreloadTrait
22
{
23
    protected $preloadVars;
24
25
    /**
26
     * Constructor.
27
     */
28 2
    public function __construct()
29
    {
30 2
        $this->preloadVars = $this->filter($this->preload());
31 2
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    final protected function find(string $name)
37
    {
38 2
        if (isset($this->preloadVars[$name])) {
39 2
            return $this->preloadVars[$name];
40
        }
41
42 2
        return null;
43
    }
44
45
    /**
46
     * @param array $vars
47
     *
48
     * @return array
49
     */
50
    final protected function filter(array $vars): array
51
    {
52 2
        return array_filter($vars, function ($var) {
53 2
            return is_string($var) && !empty($var);
54 2
        });
55
    }
56
57
    /**
58
     * Get all variables
59
     *
60
     * @return array
61
     */
62
    abstract protected function preload(): array;
63
}
64