Completed
Push — master ( 98754e...e187ea )
by Timo
08:02
created

ImageColumn::classes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\ColumnFormatters;
4
5
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter;
6
7
/**
8
 * Class ImageColumn
9
 * @package hamburgscleanest\DataTables\Models\ColumnFormatters
10
 */
11
class ImageColumn implements ColumnFormatter {
12
13
    /** @var string */
14
    private $_classes;
15
16
    /** @var string */
17
    private $_fallback;
18
19
    /**
20
     * ImageColumn constructor.
21
     * @param null|string $fallback
22
     * @param null|string $classes
23
     */
24 4
    public function __construct(? string $fallback = null, ? string $classes = null)
25
    {
26 4
        $this->_fallback = $fallback;
27 4
        $this->_classes = $classes;
28 4
    }
29
30
    /**
31
     * @param string $column
32
     * @return string
33
     */
34 4
    public function format(string $column) : string
35
    {
36 4
        if (!\file_exists($column))
37
        {
38 2
            return $this->_fallback ?? '';
39
        }
40
41 2
        return $this->_renderImage($column);
42
    }
43
44
    /**
45
     * @param string $path
46
     * @return string
47
     */
48 2
    private function _renderImage(string $path) : string
49
    {
50 2
        return '<img src="' . $path . (!empty($this->_classes) ? ('" class="' . $this->_classes) : '') . '"/>';
51
    }
52
53
    /**
54
     * Add styling to the image.
55
     *
56
     * @param string $classes
57
     * @return ImageColumn
58
     */
59 1
    public function classes(string $classes) : ImageColumn
60
    {
61 1
        $this->_classes = $classes;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * Set a fallback value which is used when the image is not found.
68
     *
69
     * @param string $fallback
70
     * @return ImageColumn
71
     */
72 1
    public function fallback(string $fallback) : ImageColumn
73
    {
74 1
        $this->_fallback = $fallback;
75
76 1
        return $this;
77
    }
78
}