Passed
Push — master ( 8df19c...2f8249 )
by Peter
05:06
created

BlockLayout::addName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Form\Factory;
6
7
use AbterPhp\Admin\Form\Factory\Base;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Form\Container\FormGroup;
10
use AbterPhp\Framework\Form\Element\Input;
11
use AbterPhp\Framework\Form\Element\Textarea;
12
use AbterPhp\Framework\Form\Extra\Help;
13
use AbterPhp\Framework\Form\IForm;
14
use AbterPhp\Framework\Form\Label\Label;
15
use AbterPhp\Framework\I18n\ITranslator;
16
use AbterPhp\Website\Domain\Entities\BlockLayout as Entity;
17
use Opulence\Orm\IEntity;
18
use Opulence\Sessions\ISession;
19
20
class BlockLayout extends Base
21
{
22
    /**
23
     * BlockLayout constructor.
24
     *
25
     * @param ISession    $session
26
     * @param ITranslator $translator
27
     */
28
    public function __construct(ISession $session, ITranslator $translator)
29
    {
30
        parent::__construct($session, $translator);
31
    }
32
33
    /**
34
     * @param string       $action
35
     * @param string       $method
36
     * @param string       $showUrl
37
     * @param IEntity|null $entity
38
     *
39
     * @return IForm
40
     */
41
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
42
    {
43
        assert($entity instanceof Entity, new \InvalidArgumentException());
44
45
        $this->createForm($action, $method)
46
            ->addDefaultElements()
47
            ->addName($entity)
48
            ->addIdentifier($entity)
49
            ->addBody($entity)
50
            ->addDefaultButtons($showUrl);
51
52
        $form = $this->form;
53
54
        $this->form = null;
55
56
        return $form;
57
    }
58
59
    /**
60
     * @param Entity $entity
61
     *
62
     * @return $this
63
     */
64
    protected function addName(Entity $entity): BlockLayout
65
    {
66
        $input = new Input('name', 'name', $entity->getName());
67
        $label = new Label('identifier', 'website:blockLayoutIdentifier');
68
        $help  = new Help('website:blockLayoutIdentifierHelp');
69
70
        $this->form[] = new FormGroup($input, $label, $help);
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param Entity $entity
77
     *
78
     * @return $this
79
     */
80
    protected function addIdentifier(Entity $entity): BlockLayout
81
    {
82
        $input = new Input('identifier', 'identifier', $entity->getIdentifier());
83
        $label = new Label('identifier', 'website:blockLayoutIdentifier');
84
        $help  = new Help('website:blockLayoutIdentifierHelp');
0 ignored issues
show
Unused Code introduced by
The assignment to $help is dead and can be removed.
Loading history...
85
86
        $this->form[] = new FormGroup($input, $label);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param Entity $entity
93
     *
94
     * @return $this
95
     */
96
    protected function addBody(Entity $entity): BlockLayout
97
    {
98
        $input = new Textarea('body', 'body', $entity->getBody(), [], [Html5::ATTR_ROWS => '15']);
99
        $label = new Label('body', 'website:blockLayoutBody');
100
101
        $this->form[] = new FormGroup($input, $label);
102
103
        return $this;
104
    }
105
}
106