DefaultButtons::addSaveAndCreate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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