Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
TexUtil
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
8 / 8
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 removeInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInstance
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getBaseElements
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllFunctionsAt
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __call
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getJSON
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getJsonFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types = 1 );
4
5namespace MediaWiki\Extension\Math\WikiTexVC;
6
7use InvalidArgumentException;
8
9class TexUtil {
10    private static $instance = null;
11    private $allFunctions;
12    private $baseElements;
13
14    /**
15     * Loads the file texutil.json
16     * allFunctions holds the root-level function keys
17     * other objects are second level elements and hold all functions which are assigned to this second level elements
18     */
19    private function __construct() {
20        $jsonContent = $this->getJSON();
21        // dynamically create functions from the content
22        $this->allFunctions = [];
23        $this->baseElements = [];
24        $this->allFunctions["\\begin"] = true;
25        $this->allFunctions["\\end"] = true;
26
27        foreach ( $jsonContent as $key => $value ) {
28            // Adding all basic elements as functions
29            foreach ( $value as $elementKey => $element ) {
30                if ( !array_key_exists( $elementKey, $this->baseElements ) ) {
31                    $this->baseElements[$elementKey] = [];
32                    $this->baseElements[$elementKey][$key] = $element;
33
34                } else {
35                    if ( !array_key_exists( $key, $this->baseElements[$elementKey] ) ) {
36                        $this->baseElements[$elementKey][$key] = $element;
37                    }
38                }
39            }
40            // Adding function to all functions
41            $this->allFunctions[$key] = true;
42        }
43    }
44
45    public static function removeInstance() {
46        self::$instance = null;
47    }
48
49    public static function getInstance() {
50        if ( self::$instance == null ) {
51            self::$instance = new TexUtil();
52        }
53
54        return self::$instance;
55    }
56
57    /**
58     * Returning the base elements array.
59     * This is only used for testing in TexUtilTest.php.
60     * @return array
61     */
62    public function getBaseElements() {
63        return $this->baseElements;
64    }
65
66    /**
67     * Getting an element by key in allFunctions.
68     * If the key is defined, return true if not false.
69     * @param string $key string to check in allFunctions
70     * @return bool
71     */
72    public function getAllFunctionsAt( string $key ) {
73        if ( array_key_exists( $key, $this->allFunctions ) ) {
74            return true;
75        } else {
76            return false;
77        }
78    }
79
80    /**
81     * Allows to directly call functions defined in from the json-file.
82     * @param mixed $func
83     * @param mixed $params
84     * @return false|mixed
85     */
86    public function __call( $func, $params ) {
87        if ( array_key_exists( $func, $this->baseElements ) ) {
88            $currentFunction = $this->baseElements[$func];
89            if ( array_key_exists( $params[0], $currentFunction ) ) {
90                return $currentFunction[$params[0]];
91            } else {
92                return false;
93            }
94        } else {
95            throw new InvalidArgumentException( "Function not defined in json " . $func );
96
97        }
98    }
99
100    /**
101     * Reads the json file to an object
102     * @return array
103     */
104    private function getJSON() {
105        $file = self::getJsonFile();
106        $json = json_decode( $file, true );
107        return $json;
108    }
109
110    /**
111     * @return false|string
112     */
113    public static function getJsonFile() {
114        return file_get_contents( __DIR__ . '/texutil.json' );
115    }
116}