Completed
Push — master ( e2643e...ad7c73 )
by Klaus
55:03
created

InputHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 87
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 4 1
A remove() 0 4 1
A setRootType() 0 4 1
A bind() 0 12 3
A getData() 0 12 3
A isValid() 0 4 1
A getErrors() 0 4 1
A getErrorsAsString() 0 4 1
define() 0 1 ?
1
<?php
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 setRootType(string $type)
49
    {
50
        $this->root = $this->typeHandler->getType($type);
51
    }
52
53
    public function bind(array $input)
54
    {
55
        $this->define();
56
57
        try {
58
            $this->output = $this->root->getValue('root', $this->root->walk($input));
59
        } catch (RequiredFieldException $exception) {
60
            $this->errors[] = 'Missing required field: ' . $exception->getField();
61
        } catch (\RuntimeException $exception) {
62
            $this->errors[] = $exception->getMessage();
63
        }
64
    }
65
66
    public function getData($index = null)
67
    {
68
        if (!$this->isValid()) {
69
            throw new \RuntimeException($this->getErrorsAsString());
70
        }
71
72
        if ($index) {
73
            return $this->output[$index];
74
        }
75
76
        return $this->output;
77
    }
78
79
    public function isValid(): bool
80
    {
81
        return empty($this->errors);
82
    }
83
84
    public function getErrors(): array
85
    {
86
        return $this->errors;
87
    }
88
89
    public function getErrorsAsString(): string
90
    {
91
        return implode(', ', $this->errors);
92
    }
93
94
    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...
95
}
96