Passed
Pull Request — main (#3)
by Peter
07:02 queued 03:23
created

DefaultButtons::addSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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