• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

conedevelopment / root / 15084089635

17 May 2025 10:00AM UTC coverage: 77.93% (+0.04%) from 77.891%
15084089635

push

github

web-flow
Modernize back-end.yml (#240)

3291 of 4223 relevant lines covered (77.93%)

36.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.0
/src/Fields/Date.php
1
<?php
2

3
namespace Cone\Root\Fields;
4

5
use Closure;
6
use Cone\Root\Filters\Filter;
7
use Cone\Root\Filters\RenderableFilter;
8
use Cone\Root\Root;
9
use DateTimeInterface;
10
use DateTimeZone;
11
use Illuminate\Database\Eloquent\Builder;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Support\Facades\Date as DateFactory;
16

17
class Date extends Field
18
{
19
    /**
20
     * The Blade template.
21
     */
22
    protected string $template = 'root::fields.date';
23

24
    /**
25
     * The date format.
26
     */
27
    protected string $format = 'Y-m-d';
28

29
    /**
30
     * The timezone.
31
     */
32
    protected string $timezone;
33

34
    /**
35
     * Indicates if the field should include time.
36
     */
37
    protected bool $withTime = false;
38

39
    /**
40
     * Create a new field instance.
41
     */
42
    public function __construct(string $label, Closure|string|null $modelAttribute = null)
198✔
43
    {
44
        parent::__construct($label, $modelAttribute);
198✔
45

46
        $this->type('date');
198✔
47
        $this->step(1);
198✔
48
        $this->timezone(Root::instance()->getTimezone());
198✔
49
    }
50

51
    /**
52
     * Set the "min" HTML attribute.
53
     */
54
    public function min(string|DateTimeInterface $value): static
1✔
55
    {
56
        return $this->setAttribute('min', is_string($value) ? $value : $value->format('Y-m-d'));
1✔
57
    }
58

59
    /**
60
     * Set the "max" HTML attribute.
61
     */
62
    public function max(string|DateTimeInterface $value): static
1✔
63
    {
64
        return $this->setAttribute('max', is_string($value) ? $value : $value->format('Y-m-d'));
1✔
65
    }
66

67
    /**
68
     * Set the "step" HTML attribute.
69
     */
70
    public function step(int $value): static
198✔
71
    {
72
        return $this->setAttribute('step', $value);
198✔
73
    }
74

75
    /**
76
     * Set the with time attribute.
77
     */
78
    public function withTime(bool $value = true): static
198✔
79
    {
80
        $this->withTime = $value;
198✔
81

82
        $this->format = $value ? 'Y-m-d H:i:s' : 'Y-m-d';
198✔
83

84
        $this->type($value ? 'datetime-local' : 'date');
198✔
85

86
        return $this;
198✔
87
    }
88

89
    /**
90
     * Set the timezone.
91
     */
92
    public function timezone(string|DateTimeZone $value): static
198✔
93
    {
94
        $this->timezone = $value instanceof DateTimeZone ? $value->getName() : $value;
198✔
95

96
        $this->suffix($this->timezone);
198✔
97

98
        return $this;
198✔
99
    }
100

101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getValueForHydrate(Request $request): ?string
×
105
    {
106
        $value = parent::getValueForHydrate($request);
×
107

108
        if (! is_null($value)) {
×
109
            $value = DateFactory::parse($value, $this->timezone)
×
110
                ->setTimezone(Config::get('app.timezone'))
×
111
                ->toISOString();
×
112
        }
113

114
        return $value;
×
115
    }
116

117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getValue(Model $model): mixed
1✔
121
    {
122
        $value = parent::getValue($model);
1✔
123

124
        return $this->parseValue($value);
1✔
125
    }
126

127
    /**
128
     * Parse the given value.
129
     */
130
    public function parseValue(mixed $value): ?DateTimeInterface
1✔
131
    {
132
        if (is_null($value)) {
1✔
133
            return $value;
×
134
        }
135

136
        return DateFactory::parse($value, Config::get('app.timezone'))
1✔
137
            ->setTimezone($this->timezone);
1✔
138
    }
139

140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function resolveFormat(Request $request, Model $model): ?string
1✔
144
    {
145
        if (is_null($this->formatResolver)) {
1✔
146
            $this->formatResolver = function (Request $request, Model $model, mixed $value): ?string {
1✔
147
                return is_null($value) ? $value : $value->format($this->format);
1✔
148
            };
1✔
149
        }
150

151
        return parent::resolveFormat($request, $model);
1✔
152
    }
153

154
    /**
155
     * Set the filterable attribute.
156
     */
157
    public function filterable(bool|Closure $value = true, ?Closure $callback = null): static
×
158
    {
159
        $callback ??= function (Request $request, Builder $query, mixed $value, string $attribute): Builder {
160
            return $query->whereDate($query->qualifyColumn($attribute), $value);
×
161
        };
162

163
        return parent::filterable($value, $callback);
×
164
    }
165

166
    /**
167
     * Get the form component data.
168
     */
169
    public function toDisplay(Request $request, Model $model): array
×
170
    {
171
        return array_merge(parent::toDisplay($request, $model), [
×
172
            'value' => $this->resolveFormat($request, $model),
×
173
        ]);
×
174
    }
175

176
    /**
177
     * Get the filter representation of the field.
178
     */
179
    public function toFilter(): Filter
×
180
    {
181
        return new class($this) extends RenderableFilter
×
182
        {
×
183
            protected Date $field;
184

185
            public function __construct(Date $field)
186
            {
187
                parent::__construct($field->getModelAttribute());
×
188

189
                $this->field = $field;
×
190
            }
191

192
            public function apply(Request $request, Builder $query, mixed $value): Builder
193
            {
194
                return $this->field->resolveFilterQuery($request, $query, $value);
×
195
            }
196

197
            public function toField(): Field
198
            {
199
                return Date::make($this->field->getLabel(), $this->getRequestKey())
×
200
                    ->value(function (Request $request): ?DateTimeInterface {
×
201
                        return $this->field->parseValue($this->getValue($request));
×
202
                    })
×
203
                    ->suffix('');
×
204
            }
205
        };
×
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc