Passed
Push — master ( df670c...4732d3 )
by Maxim
03:11
created

HandlersProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
/**
12
 * Class HandlersProcessor.
13
 * 
14
 * Processes Axessors handlers.
15
 * 
16
 * @package NoOne4rever\Axessors
17
 */
18
class HandlersProcessor
19
{
20
    private $inputHandlers;
21
    private $outputHandlers;
22
    private $namespace;
23
    
24
    public function __construct(string $in, string $out, string $namespace)
25
    {
26
        $this->inputHandlers = $in;
27
        $this->outputHandlers = $out;
28
        $this->namespace = $namespace;
29
    }
30
31
    /**
32
     * Creates list of handlers from a string of handlers definition.
33
     *
34
     * @param string $handlers handlers
35
     * @return string[] handlers
36
     */
37
    private function makeHandlersList(string $handlers): array
38
    {
39
        $result = preg_replace_callback(
40
            '{`([^`]|\\\\`)+((?<!\\\\)`)}',
41
            function (array $matches) {
42
                return addcslashes($matches[0], ',');
43
            },
44
            $handlers
45
        );
46
        $result = preg_split('{(?<!\\\\),\s*}', $result);
47
        foreach ($result as &$handler) {
48
            $injProcessor = new InjectedStringParser(stripcslashes($handler));
49
            $handler = $injProcessor->resolveClassNames($this->namespace);
50
        }
51
        return $result;
52
    }
53
54
    /**
55
     * Creates list of handlers for input data.
56
     *
57
     * @return string[] handlers
58
     */
59
    public function processInputHandlers(): array
60
    {
61
        return $this->inputHandlers === '' ? [] : $this->makeHandlersList($this->inputHandlers);
62
    }
63
64
    /**
65
     * Creates list of handlers for output data.
66
     *
67
     * @return string[] handlers
68
     */
69
    public function processOutputHandlers(): array
70
    {
71
        return $this->outputHandlers === '' ? [] : $this->makeHandlersList($this->outputHandlers);
72
    }
73
}