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