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

conedevelopment / root / 18109928020

29 Sep 2025 08:33PM UTC coverage: 77.523% (-0.09%) from 77.609%
18109928020

push

github

iamgergo
wip

5 of 5 new or added lines in 1 file covered. (100.0%)

14 existing lines in 2 files now uncovered.

3342 of 4311 relevant lines covered (77.52%)

35.2 hits per line

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

74.0
/src/Fields/URL.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Root\Fields;
6

7
use Closure;
8
use Cone\Root\Root;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\Request;
11

12
class URL extends Text
13
{
14
    /**
15
     * The text resolver callback.
16
     */
17
    protected Closure $textResolver;
18

19
    /**
20
     * The link attributes.
21
     */
22
    protected array $linkAttributes = [];
23

24
    /**
25
     * Create a new field instance.
26
     */
27
    public function __construct(string $label, Closure|string|null $modelAttribute = null)
2✔
28
    {
29
        parent::__construct($label, $modelAttribute);
2✔
30

31
        $this->type('url');
2✔
32

33
        $this->textResolver = fn (): string => 'URL';
2✔
34
    }
35

36
    /**
37
     * Set the text resolver callback.
38
     */
UNCOV
39
    public function text(Closure|string $value): static
×
40
    {
UNCOV
41
        $this->textResolver = is_string($value) ? fn (): string => $value : $value;
×
42

UNCOV
43
        return $this;
×
44
    }
45

46
    /**
47
     * Set the download attribute.
48
     */
UNCOV
49
    public function download(string|Closure $filename = ''): static
×
50
    {
UNCOV
51
        $this->linkAttributes['download'] = $filename;
×
52

UNCOV
53
        return $this;
×
54
    }
55

56
    /**
57
     * Set the target attribute.
58
     */
UNCOV
59
    public function target(string|Closure $target): static
×
60
    {
UNCOV
61
        $this->linkAttributes['target'] = $target;
×
62

UNCOV
63
        return $this;
×
64
    }
65

66
    /**
67
     * Set the rel attribute.
68
     */
UNCOV
69
    public function rel(string|Closure $rel): mixed
×
70
    {
UNCOV
71
        $this->linkAttributes['rel'] = $rel;
×
72

UNCOV
73
        return $this;
×
74
    }
75

76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function resolveFormat(Request $request, Model $model): ?string
1✔
80
    {
81
        if (is_null($this->formatResolver)) {
1✔
82
            $this->formatResolver = function (Request $request, Model $model, mixed $value): ?string {
1✔
83
                if (is_null($value)) {
1✔
UNCOV
84
                    return $value;
×
85
                }
86

87
                $attributes = array_merge(
1✔
88
                    $this->linkAttributes,
1✔
89
                    ['href' => $value, 'title' => $value],
1✔
90
                    $this->isExternal($value) ? ['data-turbo' => 'false', 'target' => '_blank'] : []
1✔
91
                );
1✔
92

93
                $attributes = array_map(
1✔
94
                    static function (string|Closure|null $attribute, string $name) use ($request, $model, $value): string {
1✔
95
                        $attribute = (string) match (true) {
1✔
96
                            $attribute instanceof Closure => call_user_func_array($attribute, [$request, $model, $value]),
1✔
97
                            default => $attribute,
1✔
98
                        };
1✔
99

100
                        return sprintf('%s="%s"', $name, $attribute);
1✔
101
                    },
1✔
102
                    array_values($attributes),
1✔
103
                    array_keys($attributes)
1✔
104
                );
1✔
105

106
                return sprintf(
1✔
107
                    '<a %s>%s</a>',
1✔
108
                    implode(' ', $attributes),
1✔
109
                    call_user_func_array($this->textResolver, [$model])
1✔
110
                );
1✔
111
            };
1✔
112
        }
113

114
        return parent::resolveFormat($request, $model);
1✔
115
    }
116

117
    /**
118
     * Determine if the given URL is external.
119
     */
120
    public function isExternal(string $value): bool
1✔
121
    {
122
        $root = Root::instance();
1✔
123

124
        [$domain, $path] = [$root->getDomain(), $root->getPath()];
1✔
125

126
        $url = parse_url($value);
1✔
127

128
        return $domain !== ($url['host'] ?? null)
1✔
129
            && ! str_starts_with(ltrim($url['path'] ?? '', '/'), ltrim($path, '/'));
1✔
130
    }
131
}
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