<commit_before><?php
/*
    Condorcet PHP Class, with Schulze Methods and others !

    By <NAME> - MIT LICENSE (Please read LICENSE.txt)
    https://github.com/julien-boudry/Condorcet
*/
declare(strict_types=1);

namespace Condorcet;

use Condorcet\Condorcet;
use Condorcet\CondorcetException;
use Condorcet\CondorcetVersion;
use Condorcet\Result;
use Condorcet\Vote;
use Condorcet\Algo\Pairwise;
use Condorcet\DataManager\VotesManager;
use Condorcet\DataManager\DataHandlerDrivers\DataHandlerDriverInterface;
use Condorcet\Timer\Chrono as Timer_Chrono;
use Condorcet\Timer\Manager as Timer_Manager;


// Base Condorcet class
class Election
{

/////////// PROPERTIES ///////////

    public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string

    protected static $_maxParseIteration = null;
    protected static $_maxVoteNumber = null;
    protected static $_checksumMode = false;

/////////// STATICS METHODS ///////////

    // Change max parse iteration
    public static function setMaxParseIteration (?int $value) : ?int
    {
        self::$_maxParseIteration = $value;
        return self::$_maxParseIteration;
    }

    // Change max vote number
    public static function setMaxVoteNumber (?int $value) : ?int
    {
        self::$_maxVoteNumber = $value;
        return self::$_maxVoteNumber;
    }


    // Check JSON format
    public static function isJson (string $string) : bool
    {
        if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) :
            return false;
        endif;

        // try to decode string
        json_decode($string);

        // check if error occured
        return json_last_error() === JSON_ERROR_NONE;
    }


    // Generic action before parsing data from string input
    public static function prepareParse (string $input, bool $allowFile) : array
    {
        // Is string or is file ?
        if ($allowFile === true && is_file($input)) :
            $input = file_get_contents($input);
        endif;

        // Line
        $input = preg_replace("(\r\n|\n|\r)",';',$input);
        $input = explode(';', $input);

        // Delete comments
        foreach ($input as &$line) :
            // Delete comments
            $is_comment = mb_strpos($line, '#');
            if ($is_comment !== false) :
                $line = substr($line, 0, $is_comment);
            endif;

            // Trim
            $line = trim($line);
        endforeach;

        return $input;
    }


    public static function prepareJson (string $input)
    {
        if (!self::isJson($input)) :
            throw new CondorcetException(15);
        endif;

        return json_decode($input, true);
    }


/////////// CONSTRUCTOR ///////////

    use CondorcetVersion;

    // Data and global options
    protected $_Candidates = []; // Candidate list
    protected $_Votes; // Votes list

    // Mechanics
    protected $_i_CandidateId = 'A';
    protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing
    protected $_timer;
    protected $_nextVoteTag = 0;
    protected $_ignoreStaticMaxVote = false;

    // Params
    protected $_ImplicitRanking = true;

    // Result
    protected $_Pairwise;
    protected $_Calculator;

        //////

    public function __construct ()
    {
        $this->_Candidates = [];
        $this->_Votes = new VotesManager ($this);
        $this->_timer = new Timer_Manager;
    }

    public function __destruct ()
    {
        $this->destroyAllLink();
    }

    public function __sleep () : array
    {
        // Don't include others data
        $include = [
            '_Candidates',
            '_Votes',

            '_i_CandidateId',
            '_State',
            '_nextVoteTag',
            '_objectVersion',
            '_ignoreStaticMaxVote',

            '_Pairwise',
            '_Calculator',
        ];<commit_msg>Bugfix : Keep ImplicitRanking rule after serialize<commit_after><?php
/*
    Condorcet PHP Class, with Schulze Methods and others !

    By <NAME> - MIT LICENSE (Please read LICENSE.txt)
    https://github.com/julien-boudry/Condorcet
*/
declare(strict_types=1);

namespace Condorcet;

use Condorcet\Condorcet;
use Condorcet\CondorcetException;
use Condorcet\CondorcetVersion;
use Condorcet\Result;
use Condorcet\Vote;
use Condorcet\Algo\Pairwise;
use Condorcet\DataManager\VotesManager;
use Condorcet\DataManager\DataHandlerDrivers\DataHandlerDriverInterface;
use Condorcet\Timer\Chrono as Timer_Chrono;
use Condorcet\Timer\Manager as Timer_Manager;


// Base Condorcet class
class Election
{

/////////// PROPERTIES ///////////

    public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string

    protected static $_maxParseIteration = null;
    protected static $_maxVoteNumber = null;
    protected static $_checksumMode = false;

/////////// STATICS METHODS ///////////

    // Change max parse iteration
    public static function setMaxParseIteration (?int $value) : ?int
    {
        self::$_maxParseIteration = $value;
        return self::$_maxParseIteration;
    }

    // Change max vote number
    public static function setMaxVoteNumber (?int $value) : ?int
    {
        self::$_maxVoteNumber = $value;
        return self::$_maxVoteNumber;
    }


    // Check JSON format
    public static function isJson (string $string) : bool
    {
        if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) :
            return false;
        endif;

        // try to decode string
        json_decode($string);

        // check if error occured
        return json_last_error() === JSON_ERROR_NONE;
    }


    // Generic action before parsing data from string input
    public static function prepareParse (string $input, bool $allowFile) : array
    {
        // Is string or is file ?
        if ($allowFile === true && is_file($input)) :
            $input = file_get_contents($input);
        endif;

        // Line
        $input = preg_replace("(\r\n|\n|\r)",';',$input);
        $input = explode(';', $input);

        // Delete comments
        foreach ($input as &$line) :
            // Delete comments
            $is_comment = mb_strpos($line, '#');
            if ($is_comment !== false) :
                $line = substr($line, 0, $is_comment);
            endif;

            // Trim
            $line = trim($line);
        endforeach;

        return $input;
    }


    public static function prepareJson (string $input)
    {
        if (!self::isJson($input)) :
            throw new CondorcetException(15);
        endif;

        return json_decode($input, true);
    }


/////////// CONSTRUCTOR ///////////

    use CondorcetVersion;

    // Data and global options
    protected $_Candidates = []; // Candidate list
    protected $_Votes; // Votes list

    // Mechanics
    protected $_i_CandidateId = 'A';
    protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing
    protected $_timer;
    protected $_nextVoteTag = 0;
    protected $_ignoreStaticMaxVote = false;

    // Params
    protected $_ImplicitRanking = true;

    // Result
    protected $_Pairwise;
    protected $_Calculator;

        //////

    public function __construct ()
    {
        $this->_Candidates = [];
        $this->_Votes = new VotesManager ($this);
        $this->_timer = new Timer_Manager;
    }

    public function __destruct ()
    {
        $this->destroyAllLink();
    }

    public function __sleep () : array
    {
        // Don't include others data
        $include = [
            '_Candidates',
            '_Votes',

            '_i_CandidateId',
            '_State',
            '_nextVoteTag',
            '_objectVersion',
            '_ignoreStaticMaxVote',

            '_ImplicitRanking',

            '_Pairwise',
            '_Calculator',
        ];
{'fraction_non_alphanumeric': 0.12616002006521193, 'fraction_numerical': 0.0027589666415851516, 'mean_word_length': 2.1335952848722988, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3899523', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2219, 'n_words': 596}