PluginProbe
WowAddons – Product Addons and Product Options With Custom Fields / trunk
WowAddons – Product Addons and Product Options With Custom Fields vtrunk
1.7.2 1.7.1 1.7.0 1.6.21 1.6.20 1.6.19 1.6.18 1.6.17 1.6.16 1.6.15 1.6.14 1.6.13 1.6.12 1.6.11 1.6.10 1.6.9 1.6.8 1.6.7 1.6.6 1.5.10 1.5.11 1.5.2 1.5.3 1.5.4 1.5.5 All 57 releases
product-addons / includes / common / formula / ast / class-expression-binary-node.php

class-expression-binary-node.php in WowAddons – Product Addons and Product Options With Custom Fields trunk, at includes/common/formula/ast/class-expression-binary-node.php

119 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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