<?php

namespace App\Models\Objects\Values;

use Carbon\Carbon;

class DatetimeValue extends Value {
    protected $table = 'field_datetime_values';

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

    public function get() {
        return $this->value ? $this->value->toIso8601String() : null;
    }
    public function set($value): bool {
        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 H:i') : null;
        });
        parent::applyFilter($query, ($value->count() === 2) ? $value->all() : $value->first());
    }

}