Passed
Push — master ( f6fb5e...281f71 )
by Peter
02:15
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
            ->addBody($entity)
58
            ->addAssets($entity)
59
            ->addDefaultButtons($showUrl);
60
61
        $form = $this->form;
62
63
        $this->form = null;
64
65
        return $form;
66
    }
67
68
    /**
69
     * @param Entity $entity
70
     *
71
     * @return $this
72
     */
73
    protected function addName(Entity $entity): PageLayout
74
    {
75
        $input = new Input('name', 'name', $entity->getName());
76
        $label = new Label('name', 'website:pageLayoutName');
77
78
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param Entity $entity
85
     *
86
     * @return $this
87
     */
88
    protected function addIdentifier(Entity $entity): PageLayout
89
    {
90
        $input = new Input(
91
            'identifier',
92
            'identifier',
93
            $entity->getIdentifier(),
94
            [],
95
            [Html5::ATTR_CLASS => 'semi-auto']
96
        );
97
        $label = new Label('identifier', 'website:pageLayoutIdentifier');
98
        $help  = new Help('website:pageLayoutIdentifierHelp');
99
100
        $this->form[] = new FormGroup($input, $label, $help);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param Entity $entity
107
     *
108
     * @return $this
109
     */
110
    protected function addBody(Entity $entity): PageLayout
111
    {
112
        $input = new Textarea('body', 'body', $entity->getBody(), [], [Html5::ATTR_ROWS => '15']);
113
        $label = new Label('body', 'website:pageLayoutBody');
114
115
        $this->form[] = new FormGroup($input, $label);
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param Entity $entity
122
     *
123
     * @return $this
124
     */
125
    protected function addAssets(Entity $entity): PageLayout
126
    {
127
        $components = $this->assetsFactory->create($entity);
128
        if (empty($components)) {
129
            return $this;
130
        }
131
132
        $container = new Hideable($this->translator->translate('website:pageLayoutAssetsBtn'));
133
        foreach ($components as $component) {
134
            $container[] = $component;
135
        }
136
137
        $this->form[] = $container;
138
139
        return $this;
140
    }
141
}
142