Passed
Push — master ( 45be64...e3c02a )
by Peter
02:31
created

PageLayout::addClasses()   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\Container\Hideable;
11
use AbterPhp\Framework\Form\Element\Input;
12
use AbterPhp\Framework\Form\Element\Textarea;
13
use AbterPhp\Framework\Form\Extra\Help;
14
use AbterPhp\Framework\Form\IForm;
15
use AbterPhp\Framework\Form\Label\Label;
16
use AbterPhp\Framework\I18n\ITranslator;
17
use AbterPhp\Website\Domain\Entities\PageLayout as Entity;
18
use AbterPhp\Website\Form\Factory\PageLayout\Assets as AssetsFactory;
19
use Opulence\Orm\IEntity;
20
use Opulence\Sessions\ISession;
21
22
class PageLayout extends Base
23
{
24
    /** @var AssetsFactory */
25
    protected $assetsFactory;
26
27
    /**
28
     * PageLayout constructor.
29
     *
30
     * @param ISession      $session
31
     * @param ITranslator   $translator
32
     * @param AssetsFactory $assetsFactory
33
     */
34
    public function __construct(ISession $session, ITranslator $translator, AssetsFactory $assetsFactory)
35
    {
36
        parent::__construct($session, $translator);
37
38
        $this->assetsFactory = $assetsFactory;
39
    }
40
41
    /**
42
     * @param string       $action
43
     * @param string       $method
44
     * @param string       $showUrl
45
     * @param IEntity|null $entity
46
     *
47
     * @return IForm
48
     */
49
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
50
    {
51
        assert($entity instanceof Entity, new \InvalidArgumentException());
52
53
        $this->createForm($action, $method)
54
            ->addDefaultElements()
55
            ->addName($entity)
56
            ->addIdentifier($entity)
57
            ->addClasses($entity)
58
            ->addBody($entity)
59
            ->addAssets($entity)
60
            ->addDefaultButtons($showUrl);
61
62
        $form = $this->form;
63
64
        $this->form = null;
65
66
        return $form;
67
    }
68
69
    /**
70
     * @param Entity $entity
71
     *
72
     * @return $this
73
     */
74
    protected function addName(Entity $entity): PageLayout
75
    {
76
        $input = new Input('name', 'name', $entity->getName());
77
        $label = new Label('name', 'website:pageLayoutName');
78
79
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param Entity $entity
86
     *
87
     * @return $this
88
     */
89
    protected function addIdentifier(Entity $entity): PageLayout
90
    {
91
        $input = new Input(
92
            'identifier',
93
            'identifier',
94
            $entity->getIdentifier(),
95
            [],
96
            [Html5::ATTR_CLASS => 'semi-auto']
97
        );
98
        $label = new Label('identifier', 'website:pageLayoutIdentifier');
99
        $help  = new Help('website:pageLayoutIdentifierHelp');
100
101
        $this->form[] = new FormGroup($input, $label, $help);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param Entity $entity
108
     *
109
     * @return $this
110
     */
111
    protected function addClasses(Entity $entity): PageLayout
112
    {
113
        $input = new Input('classes', 'classes', $entity->getClasses());
114
        $label = new Label('classes', 'website:pageLayoutClasses');
115
        $help  = new Help('website:pageLayoutClassesHelp');
116
117
        $this->form[] = new FormGroup($input, $label, $help);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param Entity $entity
124
     *
125
     * @return $this
126
     */
127
    protected function addBody(Entity $entity): PageLayout
128
    {
129
        $input = new Textarea('body', 'body', $entity->getBody(), [], [Html5::ATTR_ROWS => '15']);
130
        $label = new Label('body', 'website:pageLayoutBody');
131
132
        $this->form[] = new FormGroup($input, $label);
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param Entity $entity
139
     *
140
     * @return $this
141
     */
142
    protected function addAssets(Entity $entity): PageLayout
143
    {
144
        $components = $this->assetsFactory->create($entity);
145
        if (empty($components)) {
146
            return $this;
147
        }
148
149
        $container = new Hideable($this->translator->translate('website:pageLayoutAssetsBtn'));
150
        foreach ($components as $component) {
151
            $container[] = $component;
152
        }
153
154
        $this->form[] = $container;
155
156
        return $this;
157
    }
158
}
159