Completed
Push — master ( f6f8bd...61d974 )
by Kirill
02:08
created

ConfigResolver::match()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Compiler\Reader\Resolver\PragmaResolver;
11
12
/**
13
 * Class ConfigResolver
14
 */
15
final class ConfigResolver
16
{
17
    /**
18
     * @var string
19
     */
20
    private $prefix;
21
22
    /**
23
     * @var array|string[]
24
     */
25
    private $allowed;
26
27
    /**
28
     * ConfigResolver constructor.
29
     * @param string $prefix
30
     * @param array $allowed
31
     */
32
    public function __construct(string $prefix, array $allowed)
33
    {
34
        $this->prefix = \trim($prefix, '.') . '.';
35
        $this->allowed = $allowed;
36
    }
37
38
    /**
39
     * @param string $name
40
     * @return bool
41
     */
42
    public function match(string $name): bool
43
    {
44
        return \strpos($name, $this->prefix) === 0;
45
    }
46
47
    /**
48
     * @param string $name
49
     * @return null|string
50
     */
51
    public function resolve(string $name): ?string
52
    {
53
        $name = \substr($name, \strlen($this->prefix));
54
55
        return \in_array($name, $this->allowed, true) ? $name : null;
56
    }
57
}
58