DataView::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace Tuum\Form;
3
4
use Tuum\Form\Data\Data;
5
use Tuum\Form\Data\Errors;
6
use Tuum\Form\Data\Escape;
7
use Tuum\Form\Data\Inputs;
8
use Tuum\Form\Data\Message;
9
10
/**
11
 * Class Value
12
 *
13
 * @package Tuum\View\DataView
14
 *
15
 */
16
class DataView
17
{
18
    /**
19
     * @var Forms
20
     */
21
    public $forms;
22
23
    /**
24
     * @var Dates
25
     */
26
    public $dates;
27
28
    /**
29
     * @var Data
30
     */
31
    public $data;
32
33
    /**
34
     * @var Message
35
     */
36
    public $message;
37
38
    /**
39
     * @var Inputs
40
     */
41
    public $inputs;
42
43
    /**
44
     * @var Errors
45
     */
46
    public $errors;
47
48
    /**
49
     * @var callable|Escape
50
     */
51
    public $escape;
52
53
    /**
54
     * @param null|callable $escape
55
     */
56
    public function __construct($escape = null)
57
    {
58
        if (is_callable($escape)) {
59
            $this->escape = $escape;
60
        } else {
61
            $this->escape = new Escape();
62
        }
63
        $this->forms = new Forms();
64
        $this->dates = new Dates();
65
    }
66
67
    /**
68
     * @param array|Inputs $inputs
69
     * @return $this
70
     */
71
    public function setInputs($inputs)
72
    {
73
        if (is_array($inputs)) {
74
            $inputs = Inputs::forge($inputs, $this->escape);
75
        }
76
        $this->inputs = $inputs;
77
        if ($this->forms) {
78
            $this->forms = $this->forms->withInputs($inputs);
79
        }
80
        if ($this->dates) {
81
            $this->dates = $this->dates->setInputs($inputs);
82
        }
83
        return $this;
84
    }
85
86
    /**
87
     * @param array|Data $data
88
     * @return $this
89
     */
90
    public function setData($data)
91
    {
92
        if (is_array($data)) {
93
            $this->data = new Data($data, $this->escape);
94
        } elseif ($data instanceof Data) {
95
            $this->data = $data;
96
        }
97
        return $this;
98
    }
99
100
    /**
101
     * @param array|Message $messages
102
     * @return $this
103
     */
104
    public function setMessage($messages)
105
    {
106
        if (is_array($messages)) {
107
            $this->message = Message::forge($messages);
108
        } elseif ($messages instanceof Message) {
109
            $this->message = $messages;
110
        }
111
        return $this;
112
    }
113
114
    /**
115
     * @param array|Errors $errors
116
     * @return $this
117
     */
118
    public function setErrors($errors)
119
    {
120
        if (is_array($errors)) {
121
            $this->errors = Errors::forge($errors);
122
        } elseif ($errors instanceof Errors) {
123
            $this->errors = $errors;
124
        }
125
        return $this;
126
    }
127
}