CleaveTimeField::dataValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace LeKoala\FormElements;
4
5
use SilverStripe\ORM\DataObjectInterface;
6
7
/**
8
 * Format time field
9
 * This allow to store raw seconds instead of formatted time strings
10
 * eg 00:01:00 is converted to 60
11
 */
12
class CleaveTimeField extends CleaveField
13
{
14
    /**
15
     * Set this to true if internal value is seconds
16
     *
17
     * @var boolean
18
     */
19
    protected $isNumeric = false;
20
21
    public function __construct($name, $title = null, $value = null)
22
    {
23
        parent::__construct($name, $title, $value);
24
        $this->setConfig("swapHiddenInput", false);
25
        $this->setCleaveType('time');
26
    }
27
28
    /**
29
     * @param int $seconds
30
     * @return string
31
     */
32
    public static function secondsToTime($seconds)
33
    {
34
        $hours = floor($seconds / 3600);
35
        $mins = floor($seconds / 60) % 60;
36
        $secs = floor($seconds % 60);
37
        return sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
38
    }
39
40
    /**
41
     * @param string $time
42
     * @return int
43
     */
44
    public static function timeToSeconds($time)
45
    {
46
        sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $minutes seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $seconds seems to be never defined.
Loading history...
47
        $result = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $seconds seems to never exist and therefore isset should always be false.
Loading history...
48
        return (int)$result;
49
    }
50
51
    public function setValue($value, $data = null)
52
    {
53
        if ($this->isNumeric && is_numeric($value)) {
54
            $old = $value;
0 ignored issues
show
Unused Code introduced by
The assignment to $old is dead and can be removed.
Loading history...
55
            $value = self::secondsToTime($value);
56
        }
57
        // Don't call parent that can set locale formatted date
58
        $this->value = $value;
59
        return $this;
60
    }
61
62
    public function dataValue()
63
    {
64
        $value = parent::dataValue();
65
        // Value is stored in database in seconds
66
        if ($this->isNumeric) {
67
            return self::timeToSeconds($value);
68
        }
69
        return $value;
70
    }
71
72
    public function saveInto(DataObjectInterface $record)
73
    {
74
        return parent::saveInto($record);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::saveInto($record) targeting SilverStripe\Forms\FormField::saveInto() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
    }
76
77
    /**
78
     * Get the value of isNumeric
79
     * @return mixed
80
     */
81
    public function getIsNumeric()
82
    {
83
        return $this->isNumeric;
84
    }
85
86
    /**
87
     * Set the value of isNumeric
88
     *
89
     * @param mixed $isNumeric
90
     * @return $this
91
     */
92
    public function setIsNumeric($isNumeric)
93
    {
94
        $this->isNumeric = $isNumeric;
95
        return $this;
96
    }
97
}
98