<?php

namespace App\Models\Objects\Values;

use Illuminate\Support\Carbon;

class DateValue extends Value {
    protected $table = 'field_date_values';

    protected $dates = [
        'value',
        'ord'
    ];

    public function get() {
        return $this->value ? $this->value->toIso8601String() : null;
    }
    public function set($value): bool {
        $value = ($value instanceof Carbon) ? $value : ($value ? Carbon::create($value) : null);
        return parent::set($value);
    }

    public static function applyFilter($query, $value) {
        $value = collect($value)->map(function($value) {
            return $value ? Carbon::create($value)->format('Y-m-d') : null;
        });
        parent::applyFilter($query, $value->all());
    }

}