| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Extension\Math\WikiTexVC; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class TexUtil { |
| 26 |
|
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
private $allFunctions; |
| 30 |
|
| 31 |
private $baseElements; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private function __construct() { |
| 39 |
$jsonContent = $this->getJSON(); |
| 40 |
|
| 41 |
$this->allFunctions = []; |
| 42 |
$this->baseElements = []; |
| 43 |
$this->allFunctions["\\begin"] = true; |
| 44 |
$this->allFunctions["\\end"] = true; |
| 45 |
|
| 46 |
foreach ( $jsonContent as $key => $value ) { |
| 47 |
|
| 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 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function getBaseElements() { |
| 82 |
return $this->baseElements; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
private function getJSON() { |
| 121 |
$file = self::getJsonFile(); |
| 122 |
$json = json_decode( $file, true ); |
| 123 |
return $json; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static function getJsonFile() { |
| 130 |
return file_get_contents( __DIR__ . '/texutil.json' ); |
| 131 |
} |
| 132 |
} |