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

BaseColumn::configTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\Columns;
4
5
use Woo\GridView\Exceptions\GridViewConfigException;
6
use Woo\GridView\GridViewHelper;
7
use Woo\GridView\Traits\Configurable;
8
9
abstract class BaseColumn
10
{
11
    use Configurable;
12
13
    /**
14
     * @var string
15
     */
16
    public $title = '';
17
18
    /**
19
     * @var string|mixed
20
     */
21
    public $value = '';
22
23
    /**
24
     * @var array
25
     */
26
    public $headerHtmlOptions = [];
27
28
    /**
29
     * @var array
30
     */
31
    public $contentHtmlOptions = [];
32
33
    /**
34
     * @var string - allowed: raw, url, email, text, image
35
     */
36
    public $contentFormat = 'text';
37
38
    /**
39
     * Value when
40
     * @var string
41
     */
42
    public $emptyValue = '-';
43
44
    /**
45
     * BaseColumn constructor.
46
     * @param array $config
47
     * @throws \Woo\GridView\Exceptions\GridViewConfigException
48
     */
49
    public function __construct(array $config)
50
    {
51
        $this->loadConfig($config);
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    protected function configTests(): array
58
    {
59
        return [
60
            'title' => 'string',
61
            'value' => 'any',
62
            'headerHtmlOptions' => 'array',
63
            'contentHtmlOptions' => 'array',
64
            'contentFormat' => 'string',
65
            'emptyValue' => 'string',
66
        ];
67
    }
68
69
    /**
70
     * Formatted header html options
71
     * @return string
72
     */
73
    public function headerHtmlOptions() : string
74
    {
75
        return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions);
76
    }
77
78
    /**
79
     * Formatted content html options
80
     * @param array $context
81
     * @return string
82
     */
83
    public function contentHtmlOptions(array $context) : string
84
    {
85
        return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context);
86
    }
87
88
    /**
89
     * Render column value for row
90
     * @param array|object $row
91
     * @return string|mixed
92
     */
93
    protected abstract function _renderValue($row);
94
95
    /**
96
     * Renders column content
97
     * @param $row
98
     * @return string
99
     * @throws GridViewConfigException
100
     */
101
    public function renderValue($row)
102
    {
103
        $value = $this->_renderValue($row);
104
105
        switch ($this->contentFormat) {
106
            case 'raw':
107
                return $value;
108
109
            case 'text':
110
                return htmlentities($value);
111
112 View Code Duplication
            case 'url':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
                return '<a href="' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
114
115 View Code Duplication
            case 'email':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                return '<a href="mailto:' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
117
118
            case 'image':
119
                return '<img src="' . htmlspecialchars($value, ENT_QUOTES) . '">';
120
121
            default:
122
                throw new GridViewConfigException('Invalid content format for attribute collumn: ' . $this->value);
123
        }
124
    }
125
126
}