-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from gsteel/v3/abstract-unicode-refactor
Refactor Lower Case and Upper Case Filters
- Loading branch information
Showing
17 changed files
with
288 additions
and
522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Filter; | ||
|
||
use Laminas\Filter\Exception\InvalidArgumentException; | ||
|
||
use function array_map; | ||
use function in_array; | ||
use function mb_internal_encoding; | ||
use function mb_list_encodings; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** @internal */ | ||
final class EncodingOption | ||
{ | ||
/** | ||
* Asserts the given string is an encoding supported by ext-mbstring, returning the encoding when valid | ||
*/ | ||
public static function assert(string $encoding): string | ||
{ | ||
$encoding = strtolower($encoding); | ||
$available = array_map('strtolower', mb_list_encodings()); | ||
if (! in_array($encoding, $available, true)) { | ||
throw new InvalidArgumentException(sprintf( | ||
"Encoding '%s' is not supported by the mbstring extension", | ||
$encoding, | ||
)); | ||
} | ||
|
||
return $encoding; | ||
} | ||
|
||
/** | ||
* Asserts $encoding is a supported encoding with a fallback when encoding is unspecified | ||
*/ | ||
public static function assertWithDefault(string|null $encoding): string | ||
{ | ||
$encoding = $encoding ?? mb_internal_encoding(); | ||
|
||
return self::assert($encoding); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Filter\File; | ||
|
||
use Laminas\Filter\Exception\InvalidArgumentException; | ||
use Laminas\Filter\Exception\RuntimeException; | ||
use Laminas\Filter\FilterInterface; | ||
|
||
use function file_exists; | ||
use function file_get_contents; | ||
use function file_put_contents; | ||
use function is_writable; | ||
use function sprintf; | ||
|
||
/** @internal */ | ||
final class FilterFileContents | ||
{ | ||
/** @param FilterInterface<string> $filter */ | ||
public function __construct(private readonly FilterInterface $filter) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException If no file exists. | ||
* @throws RuntimeException If the file cannot be written to or read from. | ||
*/ | ||
public function __invoke(string $filePath): void | ||
{ | ||
if (! file_exists($filePath)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'File %s not found', | ||
$filePath, | ||
)); | ||
} | ||
|
||
if (! is_writable($filePath)) { | ||
throw new RuntimeException(sprintf( | ||
'File "%s" is not writable', | ||
$filePath | ||
)); | ||
} | ||
|
||
$content = file_get_contents($filePath); | ||
if ($content === false) { | ||
throw new RuntimeException(sprintf( | ||
'The contents of "%s" could not be read', | ||
$filePath, | ||
)); | ||
} | ||
|
||
$result = file_put_contents( | ||
$filePath, | ||
$this->filter->filter($content), | ||
); | ||
|
||
if ($result === false) { | ||
throw new RuntimeException(sprintf( | ||
'The file "%s" could not be written to', | ||
$filePath, | ||
)); | ||
} | ||
} | ||
} |
Oops, something went wrong.