Passed
Push — main ( 1f9a40...3e1577 )
by Stefan
01:51
created

FormImage::getHTML()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 31
dl 0
loc 45
rs 6.9
c 2
b 0
f 1
cc 10
nc 192
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Element to display image inside of a form.
8
 *
9
 * #### History
10
 * - *2020-05-12*   initial version
11
 * - *2021-01-07*   PHP 7.4
12
 *
13
 * @package Formgenerator
14
 * @version 1.1.0
15
 * @author Stefanius <[email protected]>
16
 * @copyright MIT License - see the LICENSE file for details
17
 */
18
class FormImage extends FormElement
19
{
20
    /** standard delete image */
21
    const IMG_DELETE            = 1;
22
    /** standard search image */
23
    const IMG_SEARCH            = 2;
24
    /** standard search image */
25
    const IMG_BROWSE            = 3;
26
    /** standard image for date picker */
27
    const IMG_DATE_PICKER       = 4;
28
    /** standard image for time picker */
29
    const IMG_TIME_PICKER       = 5;
30
    /** standard image for dtu insert (DTU: Date-Time-User) */
31
    const IMG_DTU               = 6;
32
    
33
    /** @var string|int image to display     */
34
    protected $img;
35
    /** @var string CSS styles     */
36
    protected string $strStyle;
37
    /** @var string JS onclick() handler     */
38
    protected string $strOnClick;
39
    /** @var string image is bound to this input element    */
40
    protected string $strBoundTo = '';
41
    /** @var string image to use, if no image set    */
42
    protected string $strDefault = '';
43
    
44
    /**
45
     * Create image element. 
46
     * @param string $strName
47
     * @param string|int $img       image to display or index to a standard image
48
     * @param string $strOnClick    JS onclick() handler
49
     * @param int $wFlags       
50
     * @param string $strStyle      CSS style (default '')
51
     */
52
    public function __construct(string $strName, $img, string $strOnClick, int $wFlags = 0, string $strStyle = '')
53
    {
54
        parent::__construct($wFlags);
55
        $this->strName = $strName;
56
        $this->img = $img;
57
        $this->strOnClick = $strOnClick;
58
59
        if (strlen($strStyle) > 0) {
60
            $this->aStyle = self::parseStyle($strStyle);
61
        }
62
        if (!isset($this->aStyle['vertical-align'])) {
63
            $this->addStyle('vertical-align', 'text-bottom');
64
        }
65
    }
66
    
67
    /**
68
     * Bind the image to an input field that contains the imagepath.
69
     * @param string $strBoundTo
70
     */
71
    public function bindTo(string $strBoundTo) : void
72
    {
73
        $this->strBoundTo = $strBoundTo;
74
    }
75
    
76
    /**
77
     * Set a default image, if no image set or the bounded input contains no data.
78
     * @param string $strDefault
79
     */
80
    public function setDefault(string $strDefault) : void
81
    {
82
        $this->strDefault = $strDefault;
83
    }
84
    
85
    /**
86
     * Build the HTML-notation for the image.
87
     * @return string
88
     */
89
    public function getHTML() : string
90
    {
91
        if (strlen($this->strDefault) > 0) {
92
            $this->addAttribute('data-default', $this->strDefault);
93
        }
94
        
95
        $strStyle = '';
96
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
97
            $strStyle = 'text-align: center;';
98
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
99
            $strStyle = 'text-align: right;';
100
        }
101
        $strHTML  = $this->buildContainerDiv($strStyle);
102
        
103
        if (strlen($this->strBoundTo) > 0) {
104
            $this->addAttribute('data-bound-to', $this->strBoundTo);
105
            $strImg = $this->oFG->getData()->getValue($this->strBoundTo);
106
        } else if (is_numeric($this->img)) {
107
            [$strImg, $strTitle] = $this->oFG->getStdImage(intval($this->img));
108
            if (strlen($strTitle) > 0) {
109
                $this->addAttribute('title', $strTitle);
110
            }
111
        } else {
112
            $strImg = $this->img;
113
        }
114
        
115
        if (strlen($strImg) == 0) {
116
            $strImg = $this->strDefault;
117
        }
118
        
119
        $strAlt = 'Image'; 
120
        $strHTML .= '<img src="' . $strImg . '" alt="' . $strAlt . '"';
121
        if (!empty($this->strName)) {
122
            $strHTML .= ' id="' . $this->strName . '"';
123
        }
124
        $strHTML .= $this->buildStyle();
125
        if (!empty($this->strOnClick)) {
126
            $strHTML .= ' onclick="' . $this->strOnClick . ';"';
127
        }
128
        $strHTML .= $this->buildClass();
129
        $strHTML .= $this->buildID();
130
        $strHTML .= $this->buildAttributes();
131
        $strHTML .= '></div>' . PHP_EOL;
132
        
133
        return $strHTML;
134
    }
135
}
136