Passed
Push — attributes ( 578d64...364588 )
by Peter
03:15
created

DefaultButtons::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Extra;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Attribute;
9
use AbterPhp\Framework\Html\Attributes;
10
use AbterPhp\Framework\Html\Component;
11
use AbterPhp\Framework\Html\Component\Button;
12
13
class DefaultButtons extends Component
14
{
15
    public const BTN_CONTENT_SAVE            = 'framework:save';
16
    public const BTN_CONTENT_SAVE_AND_BACK   = 'framework:saveAndBack';
17
    public const BTN_CONTENT_SAVE_AND_EDIT   = 'framework:saveAndEdit';
18
    public const BTN_CONTENT_SAVE_AND_CREATE = 'framework:saveAndCreate';
19
    public const BTN_CONTENT_BACK_TO_GRID    = 'framework:backToGrid';
20
21
    public const BTN_NAME_NEXT = 'next';
22
23
    public const BTN_VALUE_NEXT_NONE   = '';
24
    public const BTN_VALUE_NEXT_EDIT   = 'edit';
25
    public const BTN_VALUE_NEXT_CREATE = 'create';
26
    public const BTN_VALUE_NEXT_BACK   = 'back';
27
28
    protected const DEFAULT_TAG = 'div';
29
30
    /** @var Button[] */
31
    protected array $components;
32
33
    /** @var Attributes */
34
    protected Attributes $btnAttributes;
35
36
    /**
37
     * DefaultButtons constructor.
38
     *
39
     * @param null            $content
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
40
     * @param array           $intents
41
     * @param Attributes|null $attributes
42
     * @param string|null     $tag
43
     */
44
    public function __construct(
45
        $content = null,
46
        array $intents = [],
47
        ?Attributes $attributes = null,
48
        ?string $tag = null
49
    ) {
50
        $this->btnAttributes = new Attributes(
51
            [
52
                Html5::ATTR_NAME  => [self::BTN_NAME_NEXT],
53
                Html5::ATTR_TYPE  => [Button::TYPE_SUBMIT],
54
                Html5::ATTR_VALUE => [self::BTN_VALUE_NEXT_NONE],
55
            ]
56
        );
57
58
        parent::__construct($content, $intents, $attributes, $tag);
59
    }
60
61
    /**
62
     * @return $this
63
     */
64
    public function addSave(): DefaultButtons
65
    {
66
        $attributes = clone $this->btnAttributes;
67
68
        $this->nodes[] = new Button(
69
            static::BTN_CONTENT_SAVE,
70
            [Button::INTENT_PRIMARY, Button::INTENT_FORM],
71
            $attributes
72
        );
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string ...$intents
79
     *
80
     * @return $this
81
     */
82
    public function addSaveAndBack(string ...$intents): DefaultButtons
83
    {
84
        $intents = $intents ?: [Button::INTENT_PRIMARY, Button::INTENT_FORM];
85
86
        $attributes = clone $this->btnAttributes;
87
88
        $attributes->replaceItem(new Attribute(Html5::ATTR_VALUE, static::BTN_VALUE_NEXT_BACK));
89
90
        $this->nodes[] = new Button(
91
            static::BTN_CONTENT_SAVE_AND_BACK,
92
            $intents,
93
            $attributes
94
        );
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param string ...$intents
101
     *
102
     * @return $this
103
     */
104
    public function addSaveAndEdit(string ...$intents): DefaultButtons
105
    {
106
        $intents = $intents ?: [Button::INTENT_DEFAULT, Button::INTENT_FORM];
107
108
        $attributes = clone $this->btnAttributes;
109
110
        $attributes->replaceItem(new Attribute(Html5::ATTR_VALUE, static::BTN_VALUE_NEXT_EDIT));
111
112
        $this->nodes[] = new Button(
113
            static::BTN_CONTENT_SAVE_AND_EDIT,
114
            $intents,
115
            $attributes
116
        );
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param string ...$intents
123
     *
124
     * @return DefaultButtons
125
     */
126
    public function addSaveAndCreate(string ...$intents): DefaultButtons
127
    {
128
        $intents = $intents ?: [Button::INTENT_DEFAULT, Button::INTENT_FORM];
129
130
        $attributes = $this->btnAttributes;
131
132
        $attributes->replaceItem(new Attribute(Html5::ATTR_VALUE, static::BTN_VALUE_NEXT_CREATE));
133
134
        $this->nodes[] = new Button(
135
            static::BTN_CONTENT_SAVE_AND_CREATE,
136
            $intents,
137
            $attributes
138
        );
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param string $showUrl
145
     * @param string ...$intents
146
     *
147
     * @return $this
148
     */
149
    public function addBackToGrid(string $showUrl, string ...$intents): DefaultButtons
150
    {
151
        $intents = $intents ?: [Button::INTENT_DANGER, Button::INTENT_FORM];
152
153
        $attributes = new Attributes([Html5::ATTR_HREF => [$showUrl]]);
154
155
        $this->nodes[] = new Button(
156
            static::BTN_CONTENT_BACK_TO_GRID,
157
            $intents,
158
            $attributes,
159
            Html5::TAG_A
160
        );
161
162
        return $this;
163
    }
164
}
165