Datetime::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * A Datetime field
5
 */
6
namespace Rocket\Entities\Fields;
7
8
use Rocket\Entities\Field;
9
10
/**
11
 * DateTime Field
12
 *
13
 * @property DateTime $value The field's value
14
 */
15
class Datetime extends Field
16
{
17
    /**
18
     * @var string The table associated with the model.
19
     */
20
    public $table = 'field_datetime';
21
22
    /**
23
     * @var array The attributes that should be cast to native types.
24
     */
25
    protected $casts = [
26
        'value' => 'datetime',
27
    ];
28
29
    /**
30
     * Prepare the value to be stored.
31
     *
32
     * @param mixed $value The value to prepare
33
     * @return string
34
     */
35 9
    protected function prepareValue($value)
36
    {
37 9
        return $this->fromDateTime($value);
38
    }
39
40
    /**
41
     * Convert the model instance to an array.
42
     *
43
     * @return array
44
     */
45 3
    public function toArray()
46
    {
47 3
        return $this->serializeDate($this->getAttribute('value'));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->serializeDate($th...getAttribute('value')); of type null|string adds the type string to the return on line 47 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Arrayable::toArray of type array.
Loading history...
48
    }
49
50
    /**
51
     * Checks if a field is valid
52
     *
53
     * @param mixed $value The value to validate
54
     * @return bool
55
     */
56 12
    protected function isValid($value)
57
    {
58
        try {
59 12
            $this->asDateTime($value);
60
61 9
            return true;
62 3
        } catch (\Exception $e) {
63 3
            return false;
64
        }
65
    }
66
}
67