Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
32 / 40 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
Matrix | |
80.00% |
32 / 40 |
|
83.33% |
10 / 12 |
20.59 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getTop | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMainarg | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
containsFunc | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
inCurlies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
renderMML | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
renderMatrix | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderLine | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
extractIdentifiers | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
flatDeep | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
reduceCallback | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace MediaWiki\Extension\Math\WikiTexVC\Nodes; |
6 | |
7 | use InvalidArgumentException; |
8 | |
9 | class Matrix extends TexNode { |
10 | |
11 | /** @var string */ |
12 | private $top; |
13 | /** @var TexArray */ |
14 | private $mainarg; |
15 | |
16 | /** |
17 | * @param string $top |
18 | * @param TexArray $mainarg |
19 | * @throws InvalidArgumentException if nested arguments are not of type TexArray |
20 | */ |
21 | public function __construct( string $top, TexArray $mainarg ) { |
22 | foreach ( $mainarg->args as $arg ) { |
23 | if ( !$arg instanceof TexArray ) { |
24 | throw new InvalidArgumentException( 'Nested arguments have to be type of TexArray' ); |
25 | } |
26 | } |
27 | parent::__construct( $top, $mainarg ); |
28 | $this->top = $top; |
29 | $this->mainarg = $mainarg; |
30 | } |
31 | |
32 | /** |
33 | * @return string |
34 | */ |
35 | public function getTop(): string { |
36 | return $this->top; |
37 | } |
38 | |
39 | /** |
40 | * @return TexArray |
41 | */ |
42 | public function getMainarg(): TexArray { |
43 | return $this->mainarg; |
44 | } |
45 | |
46 | public function containsFunc( $target, $args = null ) { |
47 | if ( $args == null ) { |
48 | $args = [ |
49 | '\\begin{' . $this->top . '}', |
50 | '\\end{' . $this->top . '}', |
51 | $this->mainarg |
52 | ]; |
53 | } |
54 | return parent::containsFunc( $target, $args ); |
55 | } |
56 | |
57 | public function inCurlies() { |
58 | return $this->render(); |
59 | } |
60 | |
61 | public function render() { |
62 | return '{\\begin{' . $this->top . '}' . $this->renderMatrix( $this->mainarg ) . '\\end{' . $this->top . '}}'; |
63 | } |
64 | |
65 | public function renderMML( $arguments = [], $state = [] ): string { |
66 | return $this->parseToMML( $this->getTop(), $arguments, null ); |
67 | } |
68 | |
69 | private function renderMatrix( $matrix ) { |
70 | $mapped = array_map( [ self::class, 'renderLine' ], $matrix->args ); |
71 | return implode( '\\\\', $mapped ); |
72 | } |
73 | |
74 | private static function renderLine( $l ) { |
75 | $mapped = array_map( static function ( $x ){ |
76 | return $x->render(); |
77 | }, $l->args ); |
78 | return implode( '&', $mapped ); |
79 | } |
80 | |
81 | public function extractIdentifiers( $args = null ) { |
82 | if ( $args == null ) { |
83 | $args = [ $this->mainarg ]; |
84 | } |
85 | |
86 | $mapped = array_map( function ( $a ){ |
87 | return array_map( function ( $p ){ |
88 | return parent::extractIdentifiers( $p->args ); |
89 | }, $a->args ); |
90 | }, $args ); |
91 | |
92 | return self::flatDeep( $mapped ); |
93 | } |
94 | |
95 | private static function flatDeep( $a ) { |
96 | if ( !is_array( $a ) ) { |
97 | return $a; |
98 | } |
99 | |
100 | $reduced = array_reduce( $a, [ self::class, 'reduceCallback' ], [] ); |
101 | return $reduced; |
102 | } |
103 | |
104 | private static function reduceCallback( $acc, $val ) { |
105 | // Casting to array if output is string, this is required for array_merge function. |
106 | $fld = self::flatDeep( $val ); |
107 | if ( !is_array( $fld ) ) { |
108 | $fld = [ $fld ]; |
109 | } |
110 | return array_merge( $acc, $fld ); |
111 | } |
112 | |
113 | } |