Completed
Push — master ( 481507...e81850 )
by Denis
01:52
created

GridViewHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A resolveAlias() 0 4 1
A htmlOptionsToString() 0 19 4
1
<?php
2
3
namespace Woo\GridView;
4
5
use Woo\GridView\Columns\ActionsColumn;
6
use Woo\GridView\Columns\AttributeColumn;
7
use Woo\GridView\Columns\RawColumn;
8
use Woo\GridView\Renderers\DefaultRenderer;
9
10
class GridViewHelper
11
{
12
    /**
13
     * A list of grid aliases
14
     * @var array
15
     */
16
    private static $aliases = [
17
        'column' => [
18
            'attribute' => AttributeColumn::class,
19
            'raw' => RawColumn::class,
20
            'actions' => ActionsColumn::class,
21
        ],
22
        'renderer' => [
23
            'default' => DefaultRenderer::class,
24
        ]
25
    ];
26
27
    private function __construct() {}
28
29
    /**
30
     * Allows to resolve class name by its alias
31
     * @param string $context
32
     * @param string $alias
33
     * @return mixed
34
     */
35
    public static function resolveAlias(string $context, string $alias)
36
    {
37
        return self::$aliases[$context][$alias] ?? $alias;
38
    }
39
40
    /**
41
     * Allows to convert options array to html string
42
     * @param array $htmlOptions
43
     * @param array $context - context is variables, which are allowed to use when property value calculated dynamically
44
     * @return string
45
     */
46
    public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string
47
    {
48
        if (empty($htmlOptions)) {
49
            return '';
50
        }
51
52
        $out = [];
53
54
        foreach ($htmlOptions as $k => $v) {
55
56
            if ($v instanceof \Closure) {
57
                $v = call_user_func_array($v, $context);
58
            }
59
60
            $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
61
        }
62
63
        return implode(' ', $out);
64
    }
65
}