Plural_Forms::execute( int $n )

Execute the plural form function.


Description Description


Parameters Parameters

$n

(int) (Required) Variable "n" to substitute.


Top ↑

Return Return

(int) Plural form value.


Top ↑

Source Source

File: wp-includes/pomo/plural-forms.php

	public function execute( $n ) {
		$stack = array();
		$i     = 0;
		$total = count( $this->tokens );
		while ( $i < $total ) {
			$next = $this->tokens[ $i ];
			$i++;
			if ( $next[0] === 'var' ) {
				$stack[] = $n;
				continue;
			} elseif ( $next[0] === 'value' ) {
				$stack[] = $next[1];
				continue;
			}

			// Only operators left.
			switch ( $next[1] ) {
				case '%':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 % $v2;
					break;

				case '||':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 || $v2;
					break;

				case '&&':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 && $v2;
					break;

				case '<':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 < $v2;
					break;

				case '<=':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 <= $v2;
					break;

				case '>':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 > $v2;
					break;

				case '>=':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 >= $v2;
					break;

				case '!=':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 != $v2;
					break;

				case '==':
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 == $v2;
					break;

				case '?:':
					$v3      = array_pop( $stack );
					$v2      = array_pop( $stack );
					$v1      = array_pop( $stack );
					$stack[] = $v1 ? $v2 : $v3;
					break;

				default:
					throw new Exception( sprintf( 'Unknown operator "%s"', $next[1] ) );
			}
		}

		if ( count( $stack ) !== 1 ) {
			throw new Exception( 'Too many values remaining on the stack' );
		}

		return (int) $stack[0];
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.