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

PageLayout::addName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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);
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('identifier', 'identifier', $entity->getIdentifier());
91
        $label = new Label('identifier', 'website:pageLayoutIdentifier');
92
        $help  = new Help('website:pageLayoutIdentifierHelp');
93
94
        $this->form[] = new FormGroup($input, $label, $help);
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param Entity $entity
101
     *
102
     * @return $this
103
     */
104
    protected function addBody(Entity $entity): PageLayout
105
    {
106
        $input = new Textarea('body', 'body', $entity->getBody(), [], [Html5::ATTR_ROWS => '15']);
107
        $label = new Label('body', 'website:pageLayoutBody');
108
109
        $this->form[] = new FormGroup($input, $label);
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param Entity $entity
116
     *
117
     * @return $this
118
     */
119
    protected function addAssets(Entity $entity): PageLayout
120
    {
121
        $components = $this->assetsFactory->create($entity);
122
        if (empty($components)) {
123
            return $this;
124
        }
125
126
        $container = new Hideable($this->translator->translate('website:pageLayoutAssetsBtn'));
127
        foreach ($components as $component) {
128
            $container[] = $component;
129
        }
130
131
        $this->form[] = $container;
132
133
        return $this;
134
    }
135
}
136