Date::prepareValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * A Date field
5
 */
6
namespace Rocket\Entities\Fields;
7
8
use Rocket\Entities\Field;
9
10
/**
11
 * Date Field
12
 *
13
 * @property Date $value The field's value
14
 */
15
class Date extends Field
16
{
17
    /**
18
     * @var string The table associated with the model.
19
     */
20
    public $table = 'field_date';
21
22
    /**
23
     * @var array The attributes that should be cast to native types.
24
     */
25
    protected $casts = [
26
        'value' => 'date',
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
        $value = $this->asDateTime($value)->startOfDay();
38
39 9
        return $value->format($this->getDateFormat());
40
    }
41
42
    /**
43
     * Convert the model instance to an array.
44
     *
45
     * @return array
46
     */
47 3
    public function toArray()
48
    {
49 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 49 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Arrayable::toArray of type array.
Loading history...
50
    }
51
52
    /**
53
     * Checks if a field is valid
54
     *
55
     * @param mixed $value The value to validate
56
     * @return bool
57
     */
58 15
    protected function isValid($value)
59
    {
60
        try {
61 15
            $this->asDateTime($value);
62
63 9
            return true;
64 6
        } catch (\Exception $e) {
65 6
            return false;
66
        }
67
    }
68
}
69