src/I18n/Gettext/Languages/FormulaConverter.php
public static function convertFormula($cldrFormula)
    {
        if (\strpbrk($cldrFormula, '()') !== false) {
            throw new \InvalidArgumentException("Unable to convert the formula '$cldrFormula': parenthesis handling not implemented");
        }
        $orSeparatedChunks = [];
        foreach (\explode(' or ', $cldrFormula) as $cldrFormulaChunk) {
            $gettextFormulaChunk = null;
            $andSeparatedChunks = [];
            foreach (\explode(' and ', $cldrFormulaChunk) as $cldrAtom) {
                $gettextAtom = self::convertAtom($cldrAtom);
                if ($gettextAtom === false) {
                    // One atom joined by 'and' always evaluates to false => the whole 'and' group is always false
                    $gettextFormulaChunk = false;
                    break;
                }

                if ($gettextAtom !== true) {
                    $andSeparatedChunks[] = $gettextAtom;
                }
            }

            if (!isset($gettextFormulaChunk)) {
                if (empty($andSeparatedChunks)) {
                    // All the atoms joined by 'and' always evaluate to true => the whole 'and' group is always true
                    $gettextFormulaChunk = true;
                } else {
                    $gettextFormulaChunk = \implode(' && ', $andSeparatedChunks);
                    // Special cases simplification
                    switch ($gettextFormulaChunk) {
                        case 'n >= 0 && n <= 2 && n != 2':
                            $gettextFormulaChunk = 'n == 0 || n == 1';
                            break;
                    }
                }
            }
            if ($gettextFormulaChunk === true) {
                // One part of the formula joined with the others by 'or' always evaluates to true => the whole formula always evaluates to true
                return true;
            }

            if ($gettextFormulaChunk !== false) {
                $orSeparatedChunks[] = $gettextFormulaChunk;
            }
        }
        if (empty($orSeparatedChunks)) {
            // All the parts joined by 'or' always evaluate to false => the whole formula always evaluates to false
            return false;
        }

        return \implode(' || ', $orSeparatedChunks);
    }
public static function convertFormula($cldrFormula)
    {
        if (\strpbrk($cldrFormula, '()') !== false) {
            throw new \InvalidArgumentException("Unable to convert the formula '$cldrFormula': parenthesis handling not implemented");
        }
        $orSeparatedChunks = [];
        foreach (\explode(' or ', $cldrFormula) as $cldrFormulaChunk) {
            $gettextFormulaChunk = null;
            $andSeparatedChunks = [];
            foreach (\explode(' and ', $cldrFormulaChunk) as $cldrAtom) {
                $gettextAtom = self::convertAtom($cldrAtom);
                if ($gettextAtom === false) {
                    // One atom joined by 'and' always evaluates to false => the whole 'and' group is always false
                    $gettextFormulaChunk = false;
                    break;
                }

                if ($gettextAtom !== true) {
                    $andSeparatedChunks[] = $gettextAtom;
                }
            }

            if (!isset($gettextFormulaChunk)) {
                if (empty($andSeparatedChunks)) {
                    // All the atoms joined by 'and' always evaluate to true => the whole 'and' group is always true
                    $gettextFormulaChunk = true;
                } else {
                    $gettextFormulaChunk = \implode(' && ', $andSeparatedChunks);
                    // Special cases simplification
                    switch ($gettextFormulaChunk) {
                        case 'n >= 0 && n <= 2 && n != 2':
                            $gettextFormulaChunk = 'n == 0 || n == 1';
                            break;
                    }
                }
            }
            if ($gettextFormulaChunk === true) {
                // One part of the formula joined with the others by 'or' always evaluates to true => the whole formula always evaluates to true
                return true;
            }

            if ($gettextFormulaChunk !== false) {
                $orSeparatedChunks[] = $gettextFormulaChunk;
            }
        }
        if (empty($orSeparatedChunks)) {
            // All the parts joined by 'or' always evaluate to false => the whole formula always evaluates to false
            return false;
        }

        return \implode(' || ', $orSeparatedChunks);
    }
public static function convertFormula ( $ cldrFormula ) { if ( \ strpbrk ( $ cldrFormula , '()' ) !== false ) { throw new \ InvalidArgumentException ( "Unable to convert the formula '$cldrFormula': parenthesis handling not implemented" ) ; } $ orSeparatedChunks = [ ] ; foreach ( \ explode ( ' or ' , $ cldrFormula ) as $ cldrFormulaChunk ) { $ gettextFormulaChunk = null ; $ andSeparatedChunks = [ ] ; foreach ( \ explode ( ' and ' , $ cldrFormulaChunk ) as $ cldrAtom ) { $ gettextAtom = self :: convertAtom ( $ cldrAtom ) ; if ( $ gettextAtom === false ) { // One atom joined by 'and' always evaluates to false => the whole 'and' group is always false $ gettextFormulaChunk = false ; break ; } if ( $ gettextAtom !== true ) { $ andSeparatedChunks [ ] = $ gettextAtom ; } } if ( ! isset ( $ gettextFormulaChunk ) ) { if ( empty ( $ andSeparatedChunks ) ) { // All the atoms joined by 'and' always evaluate to true => the whole 'and' group is always true $ gettextFormulaChunk = true ; } else { $ gettextFormulaChunk = \ implode ( ' && ' , $ andSeparatedChunks ) ; // Special cases simplification switch ( $ gettextFormulaChunk ) { case 'n >= 0 && n <= 2 && n != 2' : $ gettextFormulaChunk = 'n == 0 || n == 1' ; break ; } } } if ( $ gettextFormulaChunk === true ) { // One part of the formula joined with the others by 'or' always evaluates to true => the whole formula always evaluates to true return true ; } if ( $ gettextFormulaChunk !== false ) { $ orSeparatedChunks [ ] = $ gettextFormulaChunk ; } } if ( empty ( $ orSeparatedChunks ) ) { // All the parts joined by 'or' always evaluate to false => the whole formula always evaluates to false return false ; } return \ implode ( ' || ' , $ orSeparatedChunks ) ; }
Converts a formula from the CLDR representation to the gettext representation.

@param string $cldrFormula The CLDR formula to convert.

@throws \InvalidArgumentException
@return bool|string Returns true if the gettext will always evaluate to true, false if gettext will always evaluate to false, return the gettext formula otherwise.
Converts a formula from the CLDR representation to the gettext representation .