| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |     Condorcet PHP - Election manager and results calculator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |     Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     https://github.com/julien-boudry/Condorcet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace CondorcetPHP\Condorcet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use CondorcetPHP\Condorcet\Throwable\CondorcetException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | abstract class CondorcetUtil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     // Check JSON format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 3 |  |     public static function isJson (string $string) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 3 |  |         if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         endif; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         // try to decode string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  |         json_decode($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         // check if error occured | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  |         return json_last_error() === JSON_ERROR_NONE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |     public static function prepareJson (string $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |         if (!self::isJson($input)) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |             throw new CondorcetException(15); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         endif; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |         return json_decode($input, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     // Generic action before parsing data from string input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 103 |  |     public static function prepareParse (string $input, bool $isFile) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Is string or is file ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 103 |  |         if ($isFile === true) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $input = file_get_contents($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         endif; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // Line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 103 |  |         $input = preg_replace("(\r\n|\n|\r)",';',$input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 103 |  |         $input = explode(';', $input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // Delete comments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 103 |  |         foreach ($input as &$line) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             // Delete comments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 103 |  |             $is_comment = mb_strpos($line, '#'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 103 |  |             if ($is_comment !== false) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |                 $line = substr($line, 0, $is_comment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             endif; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             // Trim | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 103 |  |             $line = trim($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         endforeach; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 103 |  |         return $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     // Simplify Condorcet Var_Dump. Transform object to String. | 
            
                                                        
            
                                    
            
            
                | 68 | 56 |  |     public static function format ($input, bool $convertObject = true) | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 70 | 56 |  |         if (is_object($input)) : | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |              | 
            
                                                        
            
                                    
            
            
                | 72 | 56 |  |             $r = $input; | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 | 56 |  |             if ($convertObject) : | 
            
                                                        
            
                                    
            
            
                | 75 | 5 |  |                 if ($input instanceof Candidate) : | 
            
                                                        
            
                                    
            
            
                | 76 | 2 |  |                     $r = (string) $input; | 
            
                                                        
            
                                    
            
            
                | 77 | 4 |  |                 elseif ($input instanceof Vote) : | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |                     $r = $input->getSimpleRanking(); | 
            
                                                        
            
                                    
            
            
                | 79 | 3 |  |                 elseif ($input instanceof Result) : | 
            
                                                        
            
                                    
            
            
                | 80 | 56 |  |                     $r = $input->getResultAsArray(true); | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |                 endif; | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             endif; | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 | 53 |  |         elseif (!is_array($input)) : | 
            
                                                        
            
                                    
            
            
                | 85 | 1 |  |             $r = $input; | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         else : | 
            
                                                        
            
                                    
            
            
                | 87 | 53 |  |             foreach ($input as $key => $line) : | 
            
                                                        
            
                                    
            
            
                | 88 | 53 |  |                 $input[$key] = self::format($line,$convertObject); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             endforeach; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 | 53 |  |             if (count($input) === 1 && is_int(key($input)) && (!is_array(reset($input)) || count(reset($input)) === 1)): | 
            
                                                        
            
                                    
            
            
                | 92 | 47 |  |                 $r = reset($input); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |             else: | 
            
                                                        
            
                                    
            
            
                | 94 | 8 |  |                 $r = $input; | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             endif; | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         endif; | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |          | 
            
                                                        
            
                                    
            
            
                | 98 | 56 |  |         return $r; | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |