Completed
Push — master ( 2dbe83...e884f5 )
by Rafał
11:44
created

AbstractWidgetHandler::renderWidgetOpenTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Gimme\Widget;
16
17
use SWP\Bundle\TemplatesSystemBundle\Container\ContainerRendererInterface;
18
use SWP\Component\TemplatesSystem\Gimme\Model\WidgetModelInterface;
19
20
abstract class AbstractWidgetHandler implements WidgetHandlerInterface
21
{
22
    protected static $expectedParameters = [];
23
24
    protected $widgetModel;
25
26
    public static function getExpectedParameters(): array
27
    {
28
        return static::$expectedParameters;
29
    }
30
31
    public function __construct(WidgetModelInterface $widgetModel)
32
    {
33
        $this->widgetModel = $widgetModel;
34
    }
35
36
    public function isVisible(): bool
37
    {
38
        return $this->widgetModel->getVisible();
39
    }
40
41
    public function getId(): int
42
    {
43
        return $this->widgetModel->getId();
44
    }
45
46
    public function renderWidgetOpenTag(string $containerId): string
47
    {
48
        return sprintf(
49
            '<div id="%s_%s" class="%s" data-container="%s">',
50
            ContainerRendererInterface::WIDGET_CLASS,
51
            $this->widgetModel->getId(),
52
            ContainerRendererInterface::WIDGET_CLASS,
53
            $containerId
54
        );
55
    }
56
57
    protected function getModelParameter(string $name): ?string
58
    {
59
        if (isset($this->widgetModel->getParameters()[$name])) {
60
            return $this->widgetModel->getParameters()[$name];
61
        }
62
63
        // Get default value
64
        if (isset(self::getExpectedParameters()[$name])) {
65
            $parameterMetaData = self::getExpectedParameters()[$name];
66
            if (is_array($parameterMetaData) && isset($parameterMetaData['default'])) {
67
                return $parameterMetaData['default'];
68
            }
69
        }
70
71
        return null;
72
    }
73
74
    protected function getAllParametersWithValue(): array
75
    {
76
        $all = array();
77
        foreach (self::getExpectedParameters() as $key => $value) {
78
            $all[$key] = $this->getModelParameter($key);
79
        }
80
81
        return $all;
82
    }
83
}
84