Base::addHttpMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Form\Factory;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Element\Input;
9
use AbterPhp\Framework\Form\Extra\DefaultButtons;
10
use AbterPhp\Framework\Form\Form;
11
use AbterPhp\Framework\Html\Attribute;
12
use AbterPhp\Framework\Html\Helper\Attributes;
13
use AbterPhp\Framework\I18n\ITranslator;
14
use Opulence\Framework\Http\CsrfTokenChecker;
15
use Opulence\Http\Requests\RequestMethods;
16
use Opulence\Sessions\ISession;
17
18
abstract class Base implements IFormFactory
19
{
20
    protected const MULTISELECT_MIN_SIZE = 3;
21
    protected const MULTISELECT_MAX_SIZE = 20;
22
23
    protected ISession $session;
24
25
    protected ITranslator $translator;
26
27
    protected ?Form $form;
28
29
    /**
30
     * Base constructor.
31
     *
32
     * @param ISession    $session
33
     * @param ITranslator $translator
34
     */
35
    public function __construct(ISession $session, ITranslator $translator)
36
    {
37
        $this->session    = $session;
38
        $this->translator = $translator;
39
    }
40
41
    /**
42
     * @param string                  $action
43
     * @param string                  $method
44
     * @param bool                    $isMultipart
45
     * @param string[]                $intents
46
     * @param array<string,Attribute> $attributes
47
     *
48
     * @return $this
49
     */
50
    protected function createForm(
51
        string $action,
52
        string $method,
53
        bool $isMultipart = false,
54
        array $intents = [],
55
        array $attributes = []
56
    ): Base {
57
        $attributes ??= [];
58
        if ($isMultipart) {
59
            $attributes = Attributes::addItem($attributes, Html5::ATTR_ENCTYPE, Form::ENCTYPE_MULTIPART);
60
        }
61
62
        $formMethod = $method == RequestMethods::GET ? $method : RequestMethods::POST;
63
64
        $this->form = new Form($action, $formMethod, $intents, $attributes);
65
66
        $this->addHttpMethod($method);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $method
73
     */
74
    private function addHttpMethod(string $method)
75
    {
76
        $formAttributes = Attributes::fromArray([Html5::ATTR_TYPE => Input::TYPE_HIDDEN]);
77
        $this->form[]   = new Input('', Input::NAME_HTTP_METHOD, $method, [], $formAttributes);
78
    }
79
80
    /**
81
     * @return $this
82
     */
83
    protected function addDefaultElements(): Base
84
    {
85
        $name  = CsrfTokenChecker::TOKEN_INPUT_NAME;
86
        $value = (string)$this->session->get($name);
87
88
        $formAttributes = Attributes::fromArray([Html5::ATTR_TYPE => Input::TYPE_HIDDEN]);
89
        $this->form[]   = new Input($name, $name, $value, [], $formAttributes);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $showUrl
96
     *
97
     * @return Base
98
     */
99
    protected function addDefaultButtons(string $showUrl): Base
100
    {
101
        $buttons = new DefaultButtons();
102
103
        $buttons
104
            ->addSaveAndBack()
105
            ->addBackToGrid($showUrl)
106
            ->addSaveAndEdit()
107
            ->addSaveAndCreate();
108
109
        $this->form[] = $buttons;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param int $optionCount
116
     * @param int $minSize
117
     * @param int $maxSize
118
     *
119
     * @return int
120
     */
121
    protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int
122
    {
123
        return (int)max(min($optionCount, $maxSize), $minSize);
124
    }
125
}
126