diff --git a/CHANGELOG.md b/CHANGELOG.md index 053f3d44..8ad7ee0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file. ### Description The minimum PHP version is now 8.3 +### Added +- CondorcetElectionFormat object can be created directly from a string input instead of only SplFileInfo object. + #### Dev - Complete the migration to Pest PHP diff --git a/src/Tools/Converters/CEF/CondorcetElectionFormat.php b/src/Tools/Converters/CEF/CondorcetElectionFormat.php index 1a85d362..de861281 100644 --- a/src/Tools/Converters/CEF/CondorcetElectionFormat.php +++ b/src/Tools/Converters/CEF/CondorcetElectionFormat.php @@ -11,10 +11,11 @@ namespace CondorcetPHP\Condorcet\Tools\Converters\CEF; -use CondorcetPHP\Condorcet\{Candidate, Election}; +use CondorcetPHP\Condorcet\Election; use CondorcetPHP\Condorcet\Dev\CondorcetDocumentationGenerator\CondorcetDocAttributes\{Description, FunctionParameter, FunctionReturn, PublicAPI, Related}; use CondorcetPHP\Condorcet\Throwable\FileDoesNotExistException; use CondorcetPHP\Condorcet\Tools\Converters\Interface\{ConverterExport, ConverterImport}; +use SplTempFileObject; class CondorcetElectionFormat implements ConverterExport, ConverterImport { @@ -75,6 +76,16 @@ public static function createFromElection( return ($file) ? null : $r; } + #[PublicAPI] + #[Description("Create a CondorcetElectionFormat object from string.\n")] + public static function createFromString(string $input): self + { + $file = new SplTempFileObject; + $file->fwrite($input); + + return new self ($file); + } + public static function boolParser(string $parse): bool { return match (mb_strtolower($parse)) { diff --git a/tests/src/Tools/Converters/CEF/CondorcetElectionFormatTest.php b/tests/src/Tools/Converters/CEF/CondorcetElectionFormatTest.php index 12677fce..df091473 100644 --- a/tests/src/Tools/Converters/CEF/CondorcetElectionFormatTest.php +++ b/tests/src/Tools/Converters/CEF/CondorcetElectionFormatTest.php @@ -335,8 +335,7 @@ }); test('candidates from votes', function (): void { - $file = new SplTempFileObject; - $file->fwrite($input = <<<'CVOTES' + $cef = CondorcetElectionFormat::createFromString(<<<'CVOTES' #/Number of Seats: 42 #/Implicit Ranking: false #/Weight Allowed: false @@ -349,8 +348,6 @@ D>A>B>C>E>F CVOTES); - $cef = new CondorcetElectionFormat($file); - expect($cef->candidates)->toBe(['A', 'B', 'C', 'D', 'E', 'F']); expect($cef->CandidatesParsedFromVotes)->toBeTrue(); @@ -371,7 +368,7 @@ test('non standard parameters', function (): void { $file = new SplTempFileObject; - $file->fwrite($input = <<<'CVOTES' + $file->fwrite(<<<'CVOTES' #/Number Of Seats: 42 #/Implicit Ranking: tRue #/Weight Allowed: false