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

conedevelopment / root / 18282988465

06 Oct 2025 01:45PM UTC coverage: 76.268% (-0.05%) from 76.321%
18282988465

push

github

iamgergo
fix

0 of 4 new or added lines in 1 file covered. (0.0%)

3352 of 4395 relevant lines covered (76.27%)

34.51 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace Cone\Root\Fields;
6

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

13
/**
14
 * @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne>
15
 */
16
class Meta extends MorphOne
17
{
18
    /**
19
     * The field instance.
20
     */
21
    protected Field $field;
22

23
    /**
24
     * Create a new relation field instance.
25
     */
26
    public function __construct(string $label, Closure|string|null $modelAttribute = null, Closure|string|null $relation = null)
6✔
27
    {
28
        $relation ??= function (Model $model): EloquentRelation {
6✔
29
            /** @phpstan-var \Cone\Root\Tests\phpstan\MetaDataModel $model */
30
            $related = $model->metaData()->make();
3✔
31

32
            return $model->metaData()
3✔
33
                ->one()
3✔
34
                ->ofMany(
3✔
35
                    [$related->getCreatedAtColumn() => 'MAX'],
3✔
36
                    function (Builder $query) use ($related): Builder {
3✔
37
                        return $query->where($related->qualifyColumn('key'), $this->getModelAttribute());
3✔
38
                    },
3✔
39
                    'metaData'
3✔
40
                )
3✔
41
                ->withDefault(['key' => $this->getModelAttribute()]);
3✔
42
        };
6✔
43

44
        parent::__construct($label, $modelAttribute, $relation);
6✔
45

46
        $this->as(Text::class);
6✔
47
    }
48

49
    /**
50
     * Get the relation name.
51
     */
52
    public function getRelationName(): string
4✔
53
    {
54
        return $this->relation instanceof Closure
4✔
55
            ? sprintf('__root_%s', $this->getModelAttribute())
4✔
56
            : $this->relation;
4✔
57
    }
58

59
    /**
60
     * Get the field instance.
61
     */
62
    public function getField(): Field
3✔
63
    {
64
        return $this->field;
3✔
65
    }
66

67
    /**
68
     * Set the field class.
69
     */
70
    public function as(string $field, ?Closure $callback = null): static
6✔
71
    {
72
        $this->field = new $field($this->label, $this->getModelAttribute());
6✔
73

74
        $this->field->value(fn (Request $request, Model $model): mixed => $this->resolveValue($request, $model));
6✔
75

76
        if (! is_null($callback)) {
6✔
77
            call_user_func_array($callback, [$this->field]);
×
78
        }
79

80
        return $this;
6✔
81
    }
82

83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function resolveOptions(Request $request, Model $model): array
1✔
87
    {
88
        return [];
1✔
89
    }
90

91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getValue(Model $model): mixed
3✔
95
    {
96
        $name = $this->getRelationName();
3✔
97

98
        if ($this->relation instanceof Closure
3✔
99
            && $model->relationLoaded('metaData')
3✔
100
            && ! $model->relationLoaded($name)
3✔
101
        ) {
NEW
102
            $model->setRelation(
×
NEW
103
                $name,
×
NEW
104
                $model->metaData->sortByDesc('created_at')->firstWhere('key', $this->getModelAttribute())
×
NEW
105
            );
×
106
        }
107

108
        return parent::getValue($model);
3✔
109
    }
110

111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function resolveValue(Request $request, Model $model): mixed
2✔
115
    {
116
        if (is_null($this->valueResolver)) {
2✔
117
            $this->valueResolver = static fn (Request $request, Model $model, mixed $value): mixed => $value?->value;
2✔
118
        }
119

120
        return parent::resolveValue($request, $model);
2✔
121
    }
122

123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function resolveHydrate(Request $request, Model $model, mixed $value): void
1✔
127
    {
128
        if (is_null($this->hydrateResolver)) {
1✔
129
            $this->hydrateResolver = function (Request $request, Model $model, mixed $value): void {
1✔
130
                $related = $this->getValue($model);
1✔
131

132
                $related->setAttribute('value', $value);
1✔
133

134
                $model->setRelation($this->getRelationName(), $related);
1✔
135
            };
1✔
136
        }
137

138
        parent::resolveHydrate($request, $model, $value);
1✔
139
    }
140

141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function toDisplay(Request $request, Model $model): array
1✔
145
    {
146
        return $this->field->toDisplay($request, $model);
1✔
147
    }
148

149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function toInput(Request $request, Model $model): array
1✔
153
    {
154
        return $this->field->toInput($request, $model);
1✔
155
    }
156

157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function toValidate(Request $request, Model $model): array
×
161
    {
162
        return $this->field->toValidate($request, $model);
×
163
    }
164
}
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