Completed
Push — master ( 922b0c...88b497 )
by Boudry
03:58
created

CondorcetUtil   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 3
dl 0
loc 86
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

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