Skip to content

Commit

Permalink
Cef object can be created directly from string input
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Jun 14, 2024
1 parent bc3841f commit e473cac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion src/Tools/Converters/CEF/CondorcetElectionFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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
Expand Down

0 comments on commit e473cac

Please sign in to comment.