Passed
Branch dev-2.0 (0c9fb9)
by Boudry
02:50
created

CondorcetUtil::isJson()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 2
nop 1
dl 0
loc 12
ccs 5
cts 5
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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
14
abstract class CondorcetUtil
15
{
16
    // Check JSON format
17 3
    public static function isJson (string $string) : bool
18
    {
19 3
        if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) :
20 1
            return false;
21
        endif;
22
23
        // try to decode string
24 2
        json_decode($string);
25
26
        // check if error occured
27 2
        return json_last_error() === JSON_ERROR_NONE;
28
    }
29
30 3
    public static function prepareJson (string $input)
31
    {
32 3
        if (!self::isJson($input)) :
33 3
            throw new CondorcetException(15);
34
        endif;
35
36 2
        return json_decode($input, true);
37
    }
38
39
    // Generic action before parsing data from string input
40 100
    public static function prepareParse (string $input, bool $isFile) : array
41
    {
42
        // Is string or is file ?
43 100
        if ($isFile === true) :
44
            $input = file_get_contents($input);
45
        endif;
46
47
        // Line
48 100
        $input = preg_replace("(\r\n|\n|\r)",';',$input);
49 100
        $input = explode(';', $input);
50
51
        // Delete comments
52 100
        foreach ($input as &$line) :
53
            // Delete comments
54 100
            $is_comment = mb_strpos($line, '#');
55 100
            if ($is_comment !== false) :
56 2
                $line = substr($line, 0, $is_comment);
57
            endif;
58
59
            // Trim
60 100
            $line = trim($line);
61
        endforeach;
62
63 100
        return $input;
64
    }
65
66
    // Simplify Condorcet Var_Dump. Transform object to String.
67 55
    public static function format ($input, bool $convertObject = true)
68
    {
69 55
        if (is_object($input)) :
70
            
71 55
            $r = $input;
72
73 55
            if ($convertObject) :
74 4
                if ($input instanceof Candidate) :
75 1
                    $r = (string) $input;
76 4
                elseif ($input instanceof Vote) :
77 1
                    $r = $input->getSimpleRanking();
78 3
                elseif ($input instanceof Result) :
79 55
                    $r = $input->getResultAsArray(true);
80
                endif;
81
            endif;
82
83 53
        elseif (!is_array($input)) :
84 2
            $r = $input;
85
        else :
86 53
            foreach ($input as $key => $line) :
87 53
                $input[$key] = self::format($line,$convertObject);
88
            endforeach;
89
90 53
            if (count($input) === 1 && is_int(key($input)) && (!is_array(reset($input)) || count(reset($input)) === 1)):
91 46
                $r = reset($input);
92
            else:
93 7
                $r = $input;
94
            endif;
95
        endif;
96
        
97 55
        return $r;
98
    }
99
}
100