Completed
Push — master ( 54a589...ba00a5 )
by Klaus
07:44
created

InputHandler::define()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
nc 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Linio\Component\Input;
5
6
use Linio\Component\Input\Exception\RequiredFieldException;
7
use Linio\Component\Input\Node\BaseNode;
8
9
abstract class InputHandler
0 ignored issues
show
Coding Style introduced by
InputHandler does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
10
{
11
    /**
12
     * @var BaseNode
13
     */
14
    protected $root;
15
16
    /**
17
     * @var TypeHandler
18
     */
19
    protected $typeHandler;
20
21
    /**
22
     * @var array
23
     */
24
    protected $output = [];
25
26
    /**
27
     * @var array
28
     */
29
    protected $errors = [];
30
31
    public function __construct(TypeHandler $typeHandler = null)
32
    {
33
        $this->root = new BaseNode();
34
        $this->typeHandler = $typeHandler ?? new TypeHandler();
35
        $this->root->setTypeHandler($this->typeHandler);
36
    }
37
38
    public function add(string $key, string $type, array $options = []): BaseNode
39
    {
40
        return $this->root->add($key, $type, $options);
41
    }
42
43
    public function remove(string $key)
44
    {
45
        $this->root->remove($key);
46
    }
47
48
    public function bind(array $input)
49
    {
50
        $this->define();
51
52
        try {
53
            $this->output = $this->root->walk($input);
54
        } catch (RequiredFieldException $exception) {
55
            $this->errors[] = 'Missing required field: ' . $exception->getField();
56
        } catch (\RuntimeException $exception) {
57
            $this->errors[] = $exception->getMessage();
58
        }
59
    }
60
61
    public function getData($index = null)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        if (!$this->isValid()) {
64
            throw new \RuntimeException($this->getErrorsAsString());
65
        }
66
67
        if ($index) {
68
            return $this->output[$index];
69
        }
70
71
        return $this->output;
72
    }
73
74
    public function isValid(): bool
75
    {
76
        return empty($this->errors);
77
    }
78
79
    public function getErrors(): array
80
    {
81
        return $this->errors;
82
    }
83
84
    public function getErrorsAsString(): string
85
    {
86
        return implode(', ', $this->errors);
87
    }
88
89
    abstract public function define();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
90
}
91