Skip to content

Commit a2433a7

Browse files
committed
always make sure the path is a valid one
1 parent c7fe475 commit a2433a7

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

fixtures/Path.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,34 @@ public static function any(): Set
2727
*/
2828
public static function relative(): Set
2929
{
30-
return self::strings()
31-
->map(static fn($value) => \ltrim($value, '/'))
32-
->exclude(static fn($value) => \str_ends_with($value, '\\'))
33-
->exclude(static fn($value) => $value === '')
34-
->map(Model::of(...));
30+
return self::build(
31+
self::strings()
32+
->map(static fn($value) => \ltrim($value, '/'))
33+
->exclude(static fn($value) => \str_ends_with($value, '\\')),
34+
);
3535
}
3636

3737
/**
3838
* @return Set<Model>
3939
*/
4040
public static function absolute(): Set
4141
{
42-
return self::strings()
43-
->map(static fn($value) => '/'.\ltrim($value, '/'))
44-
->exclude(static fn($value) => \str_ends_with($value, '\\'))
45-
->map(Model::of(...));
42+
return self::build(
43+
self::strings()
44+
->map(static fn($value) => '/'.\ltrim($value, '/'))
45+
->exclude(static fn($value) => \str_ends_with($value, '\\')),
46+
);
4647
}
4748

4849
/**
4950
* @return Set<Model>
5051
*/
5152
public static function directories(): Set
5253
{
53-
return self::strings()
54-
->map(static fn($value) => \rtrim($value, '/').'/')
55-
->map(Model::of(...));
54+
return self::build(
55+
self::strings()
56+
->map(static fn($value) => \rtrim($value, '/').'/'),
57+
);
5658
}
5759

5860
/**
@@ -77,11 +79,22 @@ private static function strings(): Set
7779
->exclude(static fn($value) => \str_contains($value, '//'))
7880
->exclude(static fn($value) => \str_contains($value, '\\@'))
7981
->exclude(static fn($value) => \str_starts_with($value, '\\'))
80-
->map(static fn($value) => \trim($value, ' '))
82+
->map(static fn($value) => \trim($value, ' '));
83+
}
84+
85+
/**
86+
* @param Set<string> $strings
87+
*
88+
* @return Set<Model>
89+
*/
90+
private static function build(Set $strings): Set
91+
{
92+
return $strings
8193
->exclude(static fn($value) => $value === '')
8294
->filter(static fn($value) => Url::attempt($value)->match(
8395
static fn() => true,
8496
static fn() => false,
85-
));
97+
))
98+
->map(Model::of(...));
8699
}
87100
}

0 commit comments

Comments
 (0)