• 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

95.12
/src/Fields/Fields.php
1
<?php
2

3
namespace Cone\Root\Fields;
4

5
use Cone\Root\Traits\RegistersRoutes;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Collection;
11

12
class Fields extends Collection
13
{
14
    /**
15
     * Register the given fields.
16
     */
17
    public function register(array|Field $fields): static
198✔
18
    {
19
        foreach (Arr::wrap($fields) as $field) {
198✔
20
            $this->push($field);
198✔
21
        }
22

23
        return $this;
198✔
24
    }
25

26
    /**
27
     * Persist the request value on the model.
28
     */
29
    public function persist(Request $request, Model $model): void
7✔
30
    {
31
        $this->each(static function (Field $field) use ($request, $model): void {
7✔
32
            $field->persist(
7✔
33
                $request, $model, $field->getValueForHydrate($request)
7✔
34
            );
7✔
35
        });
7✔
36
    }
37

38
    /**
39
     * Filter the fields that are available for the current request and model.
40
     */
41
    public function authorized(Request $request, ?Model $model = null): static
19✔
42
    {
43
        return $this->filter->authorized($request, $model)->values();
19✔
44
    }
45

46
    /**
47
     * Filter the fields that are visible in the given context.
48
     */
49
    public function visible(string|array $context): static
16✔
50
    {
51
        return $this->filter->visible($context)->values();
16✔
52
    }
53

54
    /**
55
     * Filter the searchable fields.
56
     */
57
    public function searchable(): static
6✔
58
    {
59
        return $this->filter->isSearchable();
6✔
60
    }
61

62
    /**
63
     * Filter the sortable fields.
64
     */
65
    public function sortable(): static
6✔
66
    {
67
        return $this->filter->isSortable();
6✔
68
    }
69

70
    /**
71
     * Filter the filterable fields.
72
     */
73
    public function filterable(): static
6✔
74
    {
75
        return $this->filter->isFilterable();
6✔
76
    }
77

78
    /**
79
     * Filter the relation fields.
80
     */
81
    public function relation(): static
×
82
    {
83
        return $this->filter(static fn (Field $field): bool => $field instanceof Relation);
×
84
    }
85

86
    /**
87
     * Filter the translatable fields.
88
     */
89
    public function translatable(): static
198✔
90
    {
91
        return $this->filter(static fn (Field $field): bool => $field->isTranslatable());
198✔
92
    }
93

94
    /**
95
     * Filter the subresource fields.
96
     */
97
    public function subResource(bool $value = true): static
11✔
98
    {
99
        return $this->filter(static fn (Field $field): bool => $value
11✔
100
            ? $field instanceof Relation && $field->isSubResource()
1✔
101
            : ! $field instanceof Relation || ! $field->isSubResource());
11✔
102
    }
103

104
    /**
105
     * Map the fields to validate.
106
     */
107
    public function mapToValidate(Request $request, Model $model): array
11✔
108
    {
109
        return $this->reduce(static fn (array $rules, Field $field): array => array_merge_recursive($rules, $field->toValidate($request, $model)), []);
11✔
110
    }
111

112
    /**
113
     * Map the fields to displayable data.
114
     */
115
    public function mapToDisplay(Request $request, Model $model): array
5✔
116
    {
117
        return $this->map->toDisplay($request, $model)->filter()->values()->all();
5✔
118
    }
119

120
    /**
121
     * Map the fields to form inputs.
122
     */
123
    public function mapToInputs(Request $request, Model $model): array
8✔
124
    {
125
        return $this->map->toInput($request, $model)->filter()->values()->all();
8✔
126
    }
127

128
    /**
129
     * Register the field routes.
130
     */
131
    public function registerRoutes(Request $request, Router $router): void
198✔
132
    {
133
        $router->prefix('fields')->group(function (Router $router) use ($request): void {
198✔
134
            $this->each(static function (Field $field) use ($request, $router): void {
198✔
135
                if (in_array(RegistersRoutes::class, class_uses_recursive($field))) {
198✔
136
                    /** @var \Cone\Root\Tests\phpstan\FieldWithRoute $field */
137
                    $field->registerRoutes($request, $router);
198✔
138
                }
139
            });
198✔
140
        });
198✔
141
    }
142
}
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