• 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

85.96
/src/Fields/Editor.php
1
<?php
2

3
namespace Cone\Root\Fields;
4

5
use Closure;
6
use Cone\Root\Models\Medium;
7
use Cone\Root\Traits\RegistersRoutes;
8
use Cone\Root\Traits\ResolvesFields;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11
use Illuminate\Http\Request;
12
use Illuminate\Routing\Router;
13
use Illuminate\Support\Facades\Config;
14

15
class Editor extends Field
16
{
17
    use RegistersRoutes {
18
        RegistersRoutes::registerRoutes as __registerRoutes;
19
    }
20
    use ResolvesFields;
21

22
    /**
23
     * The Blade template.
24
     */
25
    protected string $template = 'root::fields.editor';
26

27
    /**
28
     * The media field instance.
29
     */
30
    protected ?Media $media = null;
31

32
    /**
33
     * The editor config.
34
     */
35
    protected array $config = [];
36

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

44
        $this->config = Config::get('root.editor', []);
3✔
45
        $this->height('350px');
3✔
46
        $this->hiddenOn(['index']);
3✔
47
    }
48

49
    /**
50
     * Get the URI key.
51
     */
52
    public function getUriKey(): string
1✔
53
    {
54
        return str_replace('.', '-', $this->getRequestKey());
1✔
55
    }
56

57
    /**
58
     * Set the height style HTML attribute.
59
     */
60
    public function height(string $value): static
3✔
61
    {
62
        return $this->setAttribute('style.height', $value);
3✔
63
    }
64

65
    /**
66
     * Set the configuration.
67
     */
68
    public function withConfig(Closure $callback): static
1✔
69
    {
70
        $this->config = call_user_func_array($callback, [$this->config]);
1✔
71

72
        return $this;
1✔
73
    }
74

75
    /**
76
     * Get the editor configuration.
77
     */
78
    public function getConfig(): array
1✔
79
    {
80
        return $this->config;
1✔
81
    }
82

83
    /**
84
     * Configure the media field.
85
     */
86
    public function withMedia(?Closure $callback = null): static
2✔
87
    {
88
        if (is_null($this->fields)) {
2✔
89
            $this->fields = new Fields;
2✔
90
        }
91

92
        if (is_null($this->media)) {
2✔
93
            $this->media = $this->newMediaField();
2✔
94

95
            $this->fields->register($this->media);
2✔
96
        }
97

98
        if (! is_null($callback)) {
2✔
99
            call_user_func_array($callback, [$this->media]);
1✔
100
        }
101

102
        return $this;
2✔
103
    }
104

105
    /**
106
     * Get the media field.
107
     */
108
    public function getMedia(): ?Media
2✔
109
    {
110
        return $this->media;
2✔
111
    }
112

113
    /**
114
     * Make a custom media field.
115
     */
116
    protected function newMediaField(): Media
2✔
117
    {
118
        return new class($this->getModelAttribute()) extends Media
2✔
119
        {
2✔
120
            public function __construct(string $modelAttribute)
121
            {
122
                parent::__construct(__('Media'), $modelAttribute.'-media', static fn (): MorphToMany => new MorphToMany(
2✔
123
                    Medium::proxy()->newQuery(),
2✔
124
                    new class extends Model
2✔
125
                    {
2✔
126
                        //
127
                    },
2✔
128
                    'media',
2✔
129
                    'root_mediables',
2✔
130
                    'medium_id',
2✔
131
                    '_model_id',
2✔
132
                    'id',
2✔
133
                    'id'
2✔
134
                ));
2✔
135

136
                $this->template = 'root::fields.editor.media';
2✔
137

138
                $this->multiple();
2✔
139
            }
140
        };
2✔
141
    }
142

143
    /**
144
     * Register the routes using the given router.
145
     */
146
    public function registerRoutes(Request $request, Router $router): void
1✔
147
    {
148
        $this->__registerRoutes($request, $router);
1✔
149

150
        if (! is_null($this->media)) {
1✔
151
            $router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
1✔
152
                $this->media->registerRoutes($request, $router);
1✔
153
            });
1✔
154
        }
155
    }
156

157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function toArray(): array
×
161
    {
162
        return array_merge(parent::toArray(), [
×
163
            'config' => $this->config,
×
164
        ]);
×
165
    }
166

167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function toInput(Request $request, Model $model): array
×
171
    {
172
        return array_merge(parent::toInput($request, $model), [
×
173
            'media' => $this->media?->toInput($request, $model),
×
174
        ]);
×
175
    }
176
}
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