Passed
Push — master ( ff614d...6c840c )
by Anton
05:03 queued 02:24
created

InputScope   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 7 2
A withPrefix() 0 6 1
A __construct() 0 3 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Filter;
11
12
use Spiral\Filters\Exception\InputException;
13
use Spiral\Filters\InputInterface;
14
use Spiral\Http\Request\InputManager;
15
16
/**
17
 * Provides ability to use http request scope as filters input.
18
 */
19
final class InputScope implements InputInterface
20
{
21
    /** @var InputManager */
22
    private $input;
23
24
    /**
25
     * @param InputManager $input
26
     */
27
    public function __construct(InputManager $input)
28
    {
29
        $this->input = $input;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function withPrefix(string $prefix, bool $add = true): InputInterface
36
    {
37
        $input = clone $this;
38
        $input->input = $this->input->withPrefix($prefix, $add);
39
40
        return $input;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getValue(string $source, string $name = null)
47
    {
48
        if (!method_exists($this->input, $source)) {
49
            throw new InputException("Undefined input source '{$source}'");
50
        }
51
52
        return call_user_func([$this->input, $source], $name);
53
    }
54
}
55