Passed
Push — main ( f05cfd...29a9c1 )
by Stefan
02:38
created

FormDate::buildValue()   B

Complexity

Conditions 11
Paths 10

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 16
c 0
b 0
f 0
nc 10
nop 0
dl 0
loc 25
rs 7.3166

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
 * Input field for date value.
8
 * - size always 10
9
 * - field will be added to JS form validation
10
 * 
11
 * #### History
12
 * - *2020-05-12*   initial version
13
 * - *2021-01-07*   PHP 7.4
14
 *
15
 * @package Formgenerator
16
 * @version 1.1.0
17
 * @author Stefanius <[email protected]>
18
 * @copyright MIT License - see the LICENSE file for details
19
 */
20
class FormDate extends FormInput
21
{
22
    /**
23
     * Creates input field for date values.
24
     * @param string $strName
25
     * @param int $wFlags
26
     */
27
    public function __construct(string $strName, int $wFlags = 0) 
28
    {
29
        parent::__construct($strName, '10', $wFlags);
30
        $this->strValidate = 'aDate';
31
    }
32
    
33
    /**
34
     * Accept date value from Formgenerator-data as <ul>
35
     * <li> DateTime - object </li>
36
     * <li> unix timestamp (int) </li>
37
     * <li> English textual datetime description readable by <b>strtotime</b> <br/>
38
     *      can be a DATE, DATETIME or TIMESTAMP value from a DB query
39
     * </li></ul>
40
     * The displayed format can be configured with the <i>'FormDate.Format'</i> parameter
41
     * as strftime-compatible format string (default settings: '%Y-%m-%d') 
42
     * {@inheritDoc}
43
     * @see \SKien\Formgenerator\FormElement::buildValue()
44
     * @link https://www.php.net/manual/en/function.strftime.php  
45
     */
46
    protected function buildValue() : string
47
    {
48
        $date = $this->oFG->getData()->getValue($this->strName);
49
        $strDateFormat = $this->oFG->getConfig()->getString('Date.Format', '%Y-%m-%d');
50
        
51
        $strValue = '';
52
        if (is_object($date) && get_class($date) == 'DateTime') {
53
            // DateTime-object
54
            $strValue = strftime($strDateFormat, $date->getTimestamp());
55
        } else if (is_numeric($date)) {
56
            $strValue = strftime($strDateFormat, $date);
57
        } else {
58
            if ($date != '0000-00-00 00:00:00' && $date == '0000-00-00' && $date == '00:00:00') {
59
                $unixtime = strtotime($date);
60
                if ($unixtime !== false) {
61
                    $strValue = strftime($strDateFormat, $unixtime);
62
                }
63
            }
64
        }
65
        
66
        $strHTML = '';
67
        if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) {
68
            $strHTML = ' value="' . str_replace('"', '&quot;', $strValue) . '"';
69
        }
70
        return $strHTML;
71
    }
72
}
73