X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=languages%2FConverterRule.php;h=0d0d90dbc5c94fb2615cc3761c47b1688eaf033c;hb=63e52b83f16a2bf39def2bb615f4a8494f0b4a29;hp=e6625c1a2ad66018c9026eabbe80e38f1b48eff2;hpb=badc035712ded02e8ec7ee4c5e8a0fe09e2811d2;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/ConverterRule.php b/languages/ConverterRule.php index e6625c1a2a..0d0d90dbc5 100644 --- a/languages/ConverterRule.php +++ b/languages/ConverterRule.php @@ -31,11 +31,11 @@ class ConverterRule { public $mRuleTitle = false; public $mRules = '';// string : the text of the rules public $mRulesAction = 'none'; - public $mFlags = array(); - public $mVariantFlags = array(); - public $mConvTable = array(); - public $mBidtable = array();// array of the translation in each variant - public $mUnidtable = array();// array of the translation in each variant + public $mFlags = []; + public $mVariantFlags = []; + public $mConvTable = []; + public $mBidtable = [];// array of the translation in each variant + public $mUnidtable = [];// array of the translation in each variant /** * Constructor @@ -73,8 +73,8 @@ class ConverterRule { */ function parseFlags() { $text = $this->mText; - $flags = array(); - $variantFlags = array(); + $flags = []; + $variantFlags = []; $sepPos = strpos( $text, '|' ); if ( $sepPos !== false ) { @@ -92,16 +92,16 @@ class ConverterRule { if ( !$flags ) { $flags['S'] = true; } elseif ( isset( $flags['R'] ) ) { - $flags = array( 'R' => true );// remove other flags + $flags = [ 'R' => true ];// remove other flags } elseif ( isset( $flags['N'] ) ) { - $flags = array( 'N' => true );// remove other flags + $flags = [ 'N' => true ];// remove other flags } elseif ( isset( $flags['-'] ) ) { - $flags = array( '-' => true );// remove other flags + $flags = [ '-' => true ];// remove other flags } elseif ( count( $flags ) == 1 && isset( $flags['T'] ) ) { $flags['H'] = true; } elseif ( isset( $flags['H'] ) ) { // replace A flag, and remove other flags except T - $temp = array( '+' => true, 'H' => true ); + $temp = [ '+' => true, 'H' => true ]; if ( isset( $flags['T'] ) ) { $temp['T'] = true; } @@ -122,7 +122,7 @@ class ConverterRule { $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->mVariants ); if ( $variantFlags ) { $variantFlags = array_flip( $variantFlags ); - $flags = array(); + $flags = []; } } $this->mVariantFlags = $variantFlags; @@ -136,8 +136,8 @@ class ConverterRule { */ function parseRules() { $rules = $this->mRules; - $bidtable = array(); - $unidtable = array(); + $bidtable = []; + $unidtable = []; $variants = $this->mConverter->mVariants; $varsep_pattern = $this->mConverter->getVarSeparatorPattern(); @@ -155,25 +155,27 @@ class ConverterRule { $to = trim( $v[1] ); $v = trim( $v[0] ); $u = explode( '=>', $v, 2 ); - // if $to is empty, strtr() could return a wrong result - if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) { + // if $to is empty (which is also used as $from in bidtable), + // strtr() could return a wrong result. + if ( count( $u ) == 1 && $to !== '' && in_array( $v, $variants ) ) { $bidtable[$v] = $to; } elseif ( count( $u ) == 2 ) { $from = trim( $u[0] ); $v = trim( $u[1] ); + // if $from is empty, strtr() could return a wrong result. if ( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) - && $to + && $from !== '' && in_array( $v, $variants ) ) { - $unidtable[$v] = array( $from => $to ); - } elseif ( $to && in_array( $v, $variants ) ) { + $unidtable[$v] = [ $from => $to ]; + } elseif ( $from !== '' && in_array( $v, $variants ) ) { $unidtable[$v][$from] = $to; } } // syntax error, pass if ( !isset( $this->mConverter->mVariantNames[$v] ) ) { - $bidtable = array(); - $unidtable = array(); + $bidtable = []; + $unidtable = []; break; } } @@ -220,24 +222,20 @@ class ConverterRule { // display current variant in bidirectional array $disp = $this->getTextInBidtable( $variant ); // or display current variant in fallbacks - if ( !$disp ) { + if ( $disp === false ) { $disp = $this->getTextInBidtable( $this->mConverter->getVariantFallbacks( $variant ) ); } // or display current variant in unidirectional array - if ( !$disp && array_key_exists( $variant, $unidtable ) ) { - $disp = array_values( $unidtable[$variant] ); - $disp = $disp[0]; + if ( $disp === false && array_key_exists( $variant, $unidtable ) ) { + $disp = array_values( $unidtable[$variant] )[0]; } // or display frist text under disable manual convert - if ( !$disp && $this->mConverter->mManualLevel[$variant] == 'disable' ) { + if ( $disp === false && $this->mConverter->mManualLevel[$variant] == 'disable' ) { if ( count( $bidtable ) > 0 ) { - $disp = array_values( $bidtable ); - $disp = $disp[0]; + $disp = array_values( $bidtable )[0]; } else { - $disp = array_values( $unidtable ); - $disp = array_values( $disp[0] ); - $disp = $disp[0]; + $disp = array_values( array_values( $unidtable )[0] )[0]; } } return $disp; @@ -265,8 +263,7 @@ class ConverterRule { return $disp; } if ( array_key_exists( $variant, $this->mUnidtable ) ) { - $disp = array_values( $this->mUnidtable[$variant] ); - $disp = $disp[0]; + $disp = array_values( $this->mUnidtable[$variant] )[0]; } // Assigned above or still false. return $disp; @@ -282,7 +279,7 @@ class ConverterRule { function generateConvTable() { // Special case optimisation if ( !$this->mBidtable && !$this->mUnidtable ) { - $this->mConvTable = array(); + $this->mConvTable = []; return; } @@ -290,7 +287,7 @@ class ConverterRule { $unidtable = $this->mUnidtable; $manLevel = $this->mConverter->mManualLevel; - $vmarked = array(); + $vmarked = []; foreach ( $this->mConverter->mVariants as $v ) { /* for bidirectional array fill in the missing variants, if any, @@ -325,7 +322,7 @@ class ConverterRule { && isset( $unidtable[$v] ) ) { if ( isset( $this->mConvTable[$v] ) ) { - $this->mConvTable[$v] = array_merge( $this->mConvTable[$v], $unidtable[$v] ); + $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v]; } else { $this->mConvTable[$v] = $unidtable[$v]; } @@ -371,7 +368,7 @@ class ConverterRule { } } } - $this->mFlags = $flags = array( 'R' => true ); + $this->mFlags = $flags = [ 'R' => true ]; } if ( !isset( $flags['R'] ) && !isset( $flags['N'] ) ) { @@ -383,12 +380,14 @@ class ConverterRule { if ( !$this->mBidtable && !$this->mUnidtable ) { if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) { - // fill all variants if text in -{A/H/-|text} without rules - foreach ( $this->mConverter->mVariants as $v ) { - $this->mBidtable[$v] = $rules; + // fill all variants if text in -{A/H/-|text}- is non-empty but without rules + if ( $rules !== '' ) { + foreach ( $this->mConverter->mVariants as $v ) { + $this->mBidtable[$v] = $rules; + } } } elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) { - $this->mFlags = $flags = array( 'R' => true ); + $this->mFlags = $flags = [ 'R' => true ]; } }