Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\SDL\Frontend\Interceptor;
11
12
use Railt\SDL\Frontend\Context\ContextInterface;
13
14
/**
15
 * Class Factory
16
 */
17
class Factory
18
{
19
    /**
20
     * @var array|InterceptorInterface[]
21
     */
22
    private $interceptors;
23
24
    /**
25
     * Factory constructor.
26
     * @param array|InterceptorInterface $interceptors
27
     */
28
    public function __construct(array $interceptors = [])
29
    {
30
        $this->interceptors = $interceptors;
31
    }
32
33
    /**
34
     * @param array|InterceptorInterface $interceptors
35
     * @return Factory
36
     */
37
    public static function create(array $interceptors = []): Factory
38
    {
39
        return new static($interceptors);
40
    }
41
42
    /**
43
     * @param InterceptorInterface ...$interceptors
44
     * @return Factory
45
     */
46
    public function add(InterceptorInterface ...$interceptors): Factory
47
    {
48
        foreach ($interceptors as $interceptor) {
49
            $this->interceptors[] = $interceptor;
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param ContextInterface $ctx
57
     * @param mixed $value
58
     * @return array
59
     */
60
    public function resolve(ContextInterface $ctx, $value): array
61
    {
62
        foreach ($this->interceptors as $interceptor) {
63
            if ($interceptor->match($value)) {
64
                return $interceptor->resolve($ctx, $value);
65
            }
66
        }
67
68
        return [$ctx, $value];
69
    }
70
}
71