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

conedevelopment / root / 6640203321

25 Oct 2023 12:15PM UTC coverage: 5.51% (+0.01%) from 5.498%
6640203321

push

github

iamgergo
wip

4 of 4 new or added lines in 2 files covered. (100.0%)

107 of 1942 relevant lines covered (5.51%)

0.06 hits per line

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

0.0
/src/Fields/Meta.php
1
<?php
2

3
namespace Cone\Root\Fields;
4

5
use Closure;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;
9
use Illuminate\Http\Request;
10

11
class Meta extends MorphOne
12
{
13
    /**
14
     * The field instance.
15
     */
16
    protected Field $field;
17

18
    /**
19
     * Create a new relation field instance.
20
     */
21
    public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null)
22
    {
23
        $relation ??= function (Model $model): EloquentRelation {
×
24
            $related = $model->metaData()->getRelated();
×
25

26
            return $model->metaData()
×
27
                ->one()
×
28
                ->ofMany(
×
29
                    [$related->getCreatedAtColumn() => 'MAX'],
×
30
                    fn (Builder $query): Builder => $query->where($related->qualifyColumn('key'), $this->getModelAttribute()),
×
31
                    'metaData'
×
32
                )
×
33
                ->withDefault(['key' => $this->getModelAttribute()]);
×
34
        };
×
35

36
        parent::__construct($label, $modelAttribute, $relation);
×
37

38
        $this->asText();
×
39
    }
40

41
    /**
42
     * Get the relation name.
43
     */
44
    public function getRelationName(): string
45
    {
46
        return $this->relation instanceof Closure
×
47
            ? sprintf('__root_%s', $this->getModelAttribute())
×
48
            : $this->relation;
×
49
    }
50

51
    /**
52
     * Set the field class.
53
     */
54
    public function as(string $field, Closure $callback = null): static
55
    {
56
        $this->field = new $field($this->label, $this->getModelAttribute());
×
57

58
        if (! is_null($callback)) {
×
59
            call_user_func_array($callback, [$this->field]);
×
60
        }
61

62
        $this->field->value(function (Request $request, Model $model): mixed {
×
63
            return $this->resolveValue($request, $model);
×
64
        });
×
65

66
        return $this;
×
67
    }
68

69
    /**
70
     * Set the meta field as boolean.
71
     */
72
    public function asBoolean(Closure $callback = null): static
73
    {
74
        return $this->as(Boolean::class, $callback);
×
75
    }
76

77
    /**
78
     * Set the meta field as checkbox.
79
     */
80
    public function asCheckbox(Closure $callback = null): static
81
    {
82
        return $this->as(Checkbox::class, $callback);
×
83
    }
84

85
    /**
86
     * Set the meta field as color.
87
     */
88
    public function asColor(Closure $callback = null): static
89
    {
90
        return $this->as(Color::class, $callback);
×
91
    }
92

93
    /**
94
     * Set the meta field as date.
95
     */
96
    public function asDate(Closure $callback = null): static
97
    {
98
        return $this->as(Date::class, $callback);
×
99
    }
100

101
    /**
102
     * Set the meta field as editor.
103
     */
104
    public function asEditor(Closure $callback = null): static
105
    {
106
        return $this->as(Editor::class, $callback);
×
107
    }
108

109
    /**
110
     * Set the meta field as hidden.
111
     */
112
    public function asHidden(Closure $callback = null): static
113
    {
114
        return $this->as(Hidden::class, $callback);
×
115
    }
116

117
    /**
118
     * Set the meta field as number.
119
     */
120
    public function asNumber(Closure $callback = null): static
121
    {
122
        return $this->as(Number::class, $callback);
×
123
    }
124

125
    /**
126
     * Set the meta field as radio.
127
     */
128
    public function asRadio(Closure $callback = null): static
129
    {
130
        return $this->as(Radio::class, $callback);
×
131
    }
132

133
    /**
134
     * Set the meta field as range.
135
     */
136
    public function asRange(Closure $callback = null): static
137
    {
138
        return $this->as(Range::class, $callback);
×
139
    }
140

141
    /**
142
     * Set the meta field as select.
143
     */
144
    public function asSelect(Closure $callback = null): static
145
    {
146
        return $this->as(Select::class, $callback);
×
147
    }
148

149
    /**
150
     * Set the meta field as tag.
151
     */
152
    public function asTag(Closure $callback = null): static
153
    {
154
        return $this->as(Tag::class, $callback);
×
155
    }
156

157
    /**
158
     * Set the meta field as text.
159
     */
160
    public function asText(Closure $callback = null): static
161
    {
162
        return $this->as(Text::class, $callback);
×
163
    }
164

165
    /**
166
     * Set the meta field as textarea.
167
     */
168
    public function asTextarea(Closure $callback = null): static
169
    {
170
        return $this->as(Textarea::class, $callback);
×
171
    }
172

173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function async(bool $value = true): static
177
    {
178
        return $this;
×
179
    }
180

181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function resolveOptions(Request $request, Model $model): array
185
    {
186
        return [];
×
187
    }
188

189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getValue(Model $model): mixed
193
    {
194
        $name = $this->getRelationName();
×
195

196
        if (
197
            $this->relation instanceof Closure
×
198
            && $model->relationLoaded('metaData')
×
199
            && ! $model->relationLoaded($name)
×
200
            && ! is_null(
×
201
                $value = $model->getRelation('metaData')
×
202
                    ->sortByDesc('created_at')
×
203
                    ->firstWhere('key', $this->getModelAttribute())
×
204
            )
×
205
        ) {
206
            $model->setRelation($name, $value);
×
207
        }
208

209
        return parent::getValue($model);
×
210
    }
211

212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function resolveValue(Request $request, Model $model): mixed
216
    {
217
        if (is_null($this->valueResolver)) {
×
218
            $this->valueResolver = static function (Request $request, Model $model, mixed $value): mixed {
×
219
                return $value?->value;
×
220
            };
×
221
        }
222

223
        return parent::resolveValue($request, $model);
×
224
    }
225

226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function resolveHydrate(Request $request, Model $model, mixed $value): void
230
    {
231
        if (is_null($this->hydrateResolver)) {
×
232
            $this->hydrateResolver = function (Request $request, Model $model, mixed $value): void {
×
233
                $related = $this->getValue($model);
×
234

235
                $related->setAttribute('value', $value);
×
236

237
                $model->setRelation($this->getRelationName(), $related);
×
238
            };
×
239
        }
240

241
        parent::resolveHydrate($request, $model, $value);
×
242
    }
243

244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function toArray(): array
248
    {
249
        return $this->field->toArray();
×
250
    }
251

252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function toFormComponent(Request $request, Model $model): array
256
    {
257
        return $this->field->toFormComponent($request, $model);
×
258
    }
259

260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function toValidate(Request $request, Model $model): array
264
    {
265
        return $this->field->toValidate($request, $model);
×
266
    }
267
}
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

© 2026 Coveralls, Inc