Completed
Push — master ( dd414d...cbbb05 )
by Ryan
07:19
created

StreamFormBuilder::onMake()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Stream\Form;
2
3
use Anomaly\Streams\Platform\Stream\StreamModel;
4
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
5
6
/**
7
 * Class StreamFormBuilder
8
 *
9
 * @link          http://anomaly.is/streams-platform
10
 * @author        AnomalyLabs, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 * @package       Anomaly\Streams\Platform\Stream\Form
13
 */
14
class StreamFormBuilder extends FormBuilder
15
{
16
17
    /**
18
     * The stream prefix.
19
     *
20
     * @var null|string
21
     */
22
    protected $prefix = null;
23
24
    /**
25
     * The stream namespace.
26
     *
27
     * @var null|string
28
     */
29
    protected $namespace = null;
30
31
    /**
32
     * The form model.
33
     *
34
     * @var StreamModel
35
     */
36
    protected $model = StreamModel::class;
37
38
    /**
39
     * The form fields.
40
     *
41
     * @var StreamFormFields
42
     */
43
    protected $fields = StreamFormFields::class;
44
45
    /**
46
     * Fired when making the form.
47
     */
48
    public function onMake()
49
    {
50
        $editor = $this->getFormField('config');
51
52
        $editor->setValue(json_encode($editor->getValue(), JSON_PRETTY_PRINT));
53
    }
54
55
    /**
56
     * Fired just before saving.
57
     */
58
    public function onSaving()
59
    {
60
        $entry = $this->getFormEntry();
61
62
        if ($prefix = $this->getPrefix()) {
63
            $entry->prefix = $prefix;
64
        }
65
66
        if ($namespace = $this->getNamespace()) {
67
            $entry->namespace = $namespace;
68
        }
69
70
        $editor = $this->getFormField('config');
71
72
        $this->setFormValue('config', json_decode($editor->getValue(), true));
73
    }
74
75
    /**
76
     * Get the prefix.
77
     *
78
     * @return null|string
79
     */
80
    public function getPrefix()
81
    {
82
        return $this->prefix;
83
    }
84
85
    /**
86
     * Set the prefix.
87
     *
88
     * @param $prefix
89
     * @return $this
90
     */
91
    public function setPrefix($prefix)
92
    {
93
        $this->prefix = $prefix;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Get the namespace.
100
     *
101
     * @return null|string
102
     */
103
    public function getNamespace()
104
    {
105
        return $this->namespace;
106
    }
107
108
    /**
109
     * Set the namespace.
110
     *
111
     * @param $namespace
112
     * @return $this
113
     */
114
    public function setNamespace($namespace)
115
    {
116
        $this->namespace = $namespace;
117
118
        return $this;
119
    }
120
121
}
122