Completed
Push — master ( 5f2f2b...044d29 )
by Denis
69:27 queued 42:33
created

AttributeColumn::renderValue()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 30

Duplication

Lines 4
Ratio 13.33 %

Importance

Changes 0
Metric Value
cc 8
nc 20
nop 1
dl 4
loc 30
rs 8.1954
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\Columns;
4
5
use Woo\GridView\Exceptions\ColumnRenderException;
6
use Woo\GridView\Exceptions\GridViewConfigException;
7
8
class AttributeColumn extends BaseColumn
9
{
10
    /**
11
     * @var string - allowed: url, email, text, image
12
     */
13
    public $contentFormat = 'text';
14
15
    public function __construct($config)
16
    {
17
        if (is_string($config)) {
18
            $config = [
19
                'value' => $config,
20
            ];
21
        }
22
23
        parent::__construct($config);
24
25
        if (empty($this->title)) {
26
            $this->title = ucfirst(str_replace('_', ' ', $this->value));
27
        }
28
    }
29
30
    /**
31
     * @inheritdoc
32
     * @throws ColumnRenderException
33
     * @throws GridViewConfigException
34
     */
35
    public function renderValue($row)
36
    {
37
        $value = '';
38
39
        if (is_array($row)) {
40
41
            if (isset($row[$this->value])) {
42
                $value = $row[$this->value];
43
            }
44
        } elseif (isset($row->{$this->value})) {
45
            $value = $row->{$this->value};
46
        }
47
48
        switch ($this->contentFormat) {
49
            case 'text':
50
                return htmlentities($value);
51
52 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...
53
                return '<a href="' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
54
55 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...
56
                return '<a href="mailto:' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
57
                
58
            case 'image':
59
                return '<img src="' . htmlspecialchars($value, ENT_QUOTES) . '">';
60
                
61
            default:
62
                throw new GridViewConfigException('Invalid content format for attribute collumn: ' . $this->value);
63
        }
64
    }
65
}