• 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

93.1
/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
/**
12
 * @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne>
13
 */
14
class Meta extends MorphOne
15
{
16
    /**
17
     * The field instance.
18
     */
19
    protected Field $field;
20

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

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

40
        parent::__construct($label, $modelAttribute, $relation);
6✔
41

42
        $this->as(Text::class);
6✔
43
    }
44

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

55
    /**
56
     * Get the field instance.
57
     */
58
    public function getField(): Field
3✔
59
    {
60
        return $this->field;
3✔
61
    }
62

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

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

72
        if (! is_null($callback)) {
6✔
73
            call_user_func_array($callback, [$this->field]);
×
74
        }
75

76
        return $this;
6✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function resolveOptions(Request $request, Model $model): array
1✔
83
    {
84
        return [];
1✔
85
    }
86

87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getValue(Model $model): mixed
3✔
91
    {
92
        $name = $this->getRelationName();
3✔
93

94
        if (
95
            $this->relation instanceof Closure
3✔
96
            && $model->relationLoaded('metaData')
3✔
97
            && ! $model->relationLoaded($name)
3✔
98
            && ! is_null(
3✔
99
                $value = $model->getRelation('metaData')
3✔
100
                    ->sortByDesc('created_at')
3✔
101
                    ->firstWhere('key', $this->getModelAttribute())
3✔
102
            )
3✔
103
        ) {
104
            $model->setRelation($name, $value);
×
105
        }
106

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

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

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

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

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

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

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

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

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

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