| 1 |
<?php // phpcs:ignore |
| 2 |
/** |
| 3 |
* Binary operator node. |
| 4 |
* |
| 5 |
* @package PRAD |
| 6 |
* @since 1.5.8 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace PRAD\Includes\Common\Formula\Ast; |
| 10 |
|
| 11 |
use PRAD\Includes\Common\Formula\Abstract_Expression_Engine; |
| 12 |
use PRAD\Includes\Common\Formula\Expression_Exception; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Represents a binary operator node in the expression AST. |
| 18 |
* |
| 19 |
* Handles evaluation of binary operations such as arithmetic and logical operators. |
| 20 |
* |
| 21 |
* @package PRAD |
| 22 |
* @since 1.5.8 |
| 23 |
*/ |
| 24 |
final class Expression_Binary_Node implements Expression_Node { |
| 25 |
/** |
| 26 |
* Operator for the binary expression. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $op; |
| 31 |
/** |
| 32 |
* Left operand node. |
| 33 |
* |
| 34 |
* @var Expression_Node |
| 35 |
*/ |
| 36 |
private $left; |
| 37 |
/** |
| 38 |
* Right operand node. |
| 39 |
* |
| 40 |
* @var Expression_Node |
| 41 |
*/ |
| 42 |
private $right; |
| 43 |
|
| 44 |
/** |
| 45 |
* Expression_Binary_Node constructor. |
| 46 |
* |
| 47 |
* @param string $op Operator for the binary expression. |
| 48 |
* @param Expression_Node $left Left operand node. |
| 49 |
* @param Expression_Node $right Right operand node. |
| 50 |
*/ |
| 51 |
public function __construct( $op, Expression_Node $left, Expression_Node $right ) { |
| 52 |
$this->op = (string) $op; |
| 53 |
$this->left = $left; |
| 54 |
$this->right = $right; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Evaluates the binary expression node. |
| 59 |
* |
| 60 |
* @param Abstract_Expression_Engine $engine The expression engine used for evaluation. |
| 61 |
* @param array $context Contextual variables for evaluation. |
| 62 |
* @return mixed The result of the binary operation. |
| 63 |
* @throws Expression_Exception If division by zero or unsupported operator is encountered. |
| 64 |
*/ |
| 65 |
public function evaluate( Abstract_Expression_Engine $engine, array $context = array() ) { |
| 66 |
$op = $this->op; |
| 67 |
|
| 68 |
if ( '||' === $op ) { |
| 69 |
return $engine->to_bool( $this->left->evaluate( $engine, $context ) ) || $engine->to_bool( $this->right->evaluate( $engine, $context ) ); |
| 70 |
} |
| 71 |
if ( '&' === $op ) { |
| 72 |
return $engine->to_bool( $this->left->evaluate( $engine, $context ) ) && $engine->to_bool( $this->right->evaluate( $engine, $context ) ); |
| 73 |
} |
| 74 |
|
| 75 |
$left = $this->left->evaluate( $engine, $context ); |
| 76 |
$right = $this->right->evaluate( $engine, $context ); |
| 77 |
|
| 78 |
if ( '>' === $op ) { |
| 79 |
return $engine->to_number( $left ) > $engine->to_number( $right ); |
| 80 |
} |
| 81 |
if ( '<' === $op ) { |
| 82 |
return $engine->to_number( $left ) < $engine->to_number( $right ); |
| 83 |
} |
| 84 |
if ( '>=' === $op ) { |
| 85 |
return $engine->to_number( $left ) >= $engine->to_number( $right ); |
| 86 |
} |
| 87 |
if ( '<=' === $op ) { |
| 88 |
return $engine->to_number( $left ) <= $engine->to_number( $right ); |
| 89 |
} |
| 90 |
if ( '!=' === $op ) { |
| 91 |
return $engine->to_number( $left ) != $engine->to_number( $right ); |
| 92 |
} |
| 93 |
if ( '=' === $op ) { |
| 94 |
return $engine->to_number( $left ) == $engine->to_number( $right ); |
| 95 |
} |
| 96 |
|
| 97 |
$l = $engine->to_number( $left ); |
| 98 |
$r = $engine->to_number( $right ); |
| 99 |
|
| 100 |
if ( '+' === $op ) { |
| 101 |
return $l + $r; |
| 102 |
} |
| 103 |
if ( '-' === $op ) { |
| 104 |
return $l - $r; |
| 105 |
} |
| 106 |
if ( '*' === $op ) { |
| 107 |
return $l * $r; |
| 108 |
} |
| 109 |
if ( '/' === $op ) { |
| 110 |
if ( 0.0 === $r ) { |
| 111 |
throw new Expression_Exception( 'Division by zero.' ); |
| 112 |
} |
| 113 |
return $l / $r; |
| 114 |
} |
| 115 |
|
| 116 |
throw new Expression_Exception( 'Unsupported operator: ' . esc_html( $op ) ); |
| 117 |
} |
| 118 |
} |
| 119 |
|