Skip to content

Commit e07e050

Browse files
committed
Add File::copy(), only replace current EOL sequence in Str::setEol()
1 parent 6d58260 commit e07e050

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/Utility/File.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public static function tell($stream, $uri = null): int
125125
return self::throwOnFailure($result, 'Error getting file position indicator for stream: %s', $uri, $stream);
126126
}
127127

128+
/**
129+
* Copy a file
130+
*
131+
* @see copy()
132+
* @throws FilesystemErrorException on failure.
133+
*/
134+
public static function copy(string $from, string $to): void
135+
{
136+
$result = @copy($from, $to);
137+
self::throwOnFailure($result, 'Error copying %s to %s', $from, null, false, $to);
138+
}
139+
128140
/**
129141
* Get the status of a file or stream
130142
*
@@ -738,17 +750,18 @@ public static function getStreamUri($stream): ?string
738750
* @param Stringable|string|null $uri
739751
* @param resource|null $stream
740752
* @param TFailure $failure
753+
* @param string|int|float ...$args
741754
* @return ($result is TFailure ? never : TSuccess)
742755
*/
743-
private static function throwOnFailure($result, string $message, $uri, $stream = null, $failure = false)
756+
private static function throwOnFailure($result, string $message, $uri, $stream = null, $failure = false, ...$args)
744757
{
745758
if ($result === $failure) {
746759
$error = error_get_last();
747760
if ($error) {
748761
throw new FilesystemErrorException($error['message']);
749762
}
750763
throw new FilesystemErrorException(
751-
sprintf($message, self::getFriendlyStreamUri($uri, $stream))
764+
sprintf($message, self::getFriendlyStreamUri($uri, $stream), ...$args)
752765
);
753766
}
754767
return $result;

src/Utility/Str.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,19 @@ public static function upperFirst(string $string): string
6060
/**
6161
* Apply an end-of-line sequence to a string
6262
*/
63-
public static function setEol(string $string, string $eol = "\n"): string
63+
public static function setEol(string $string, string $eol = "\n", ?string $currentEol = null): string
6464
{
65-
switch ($eol) {
66-
case "\n":
67-
return str_replace(["\r\n", "\r"], $eol, $string);
68-
69-
case "\r":
70-
return str_replace(["\r\n", "\n"], $eol, $string);
71-
72-
case "\r\n":
73-
return str_replace(["\r\n", "\r", "\n"], ["\n", "\n", $eol], $string);
74-
75-
default:
76-
throw new InvalidArgumentException(sprintf('Invalid end-of-line sequence: %s', $eol));
65+
if ($currentEol === null) {
66+
$currentEol = Get::eol($string);
67+
// No end-of-line sequence = no line breaks = nothing to do
68+
if ($currentEol === null) {
69+
return $string;
70+
}
71+
}
72+
if ($eol === $currentEol) {
73+
return $string;
7774
}
75+
return str_replace($currentEol, $eol, $string);
7876
}
7977

8078
/**

0 commit comments

Comments
 (0)