File: ExpressionGroup.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (84 lines) | stat: -rw-r--r-- 2,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

namespace Wikimedia\Rdbms;

use InvalidArgumentException;
use Wikimedia\Rdbms\Database\DbQuoter;

// Very long type annotations :(
// phpcs:disable Generic.Files.LineLength

/**
 * A composite node representing a group of expressions.
 *
 * @since 1.42
 */
abstract class ExpressionGroup implements IExpression {
	/**
	 * @var IExpression[]
	 */
	protected array $children = [];

	/**
	 * @param IExpression ...$children
	 * @internal Outside of rdbms, Use IReadableDatabase::andExpr() or ::orExpr to create an expression group object
	 */
	public function __construct( IExpression ...$children ) {
		$this->children = $children;
	}

	final protected function add( IExpression $expression ) {
		$this->children[] = $expression;
	}

	abstract protected function getType(): string;

	/**
	 * @internal to rdbms
	 * @param non-empty-array<string,?scalar|RawSQLValue|Blob|LikeValue|non-empty-list<scalar|Blob>>|non-empty-array<int,IExpression> $conds
	 * @param-taint $conds exec_sql_numkey
	 * @return static
	 */
	public static function newFromArray( array $conds ) {
		if ( !$conds ) {
			throw new InvalidArgumentException( "The array of conditions can't be empty." );
		}
		$exprs = [];
		foreach ( $conds as $field => $cond ) {
			if ( is_numeric( $field ) ) {
				if ( !$cond instanceof IExpression ) {
					throw new InvalidArgumentException( __METHOD__ . ": Only IExpression are allowed with numeric key." );
				}
				$exprs[] = $cond;
			} else {
				if ( $cond instanceof IExpression ) {
					throw new InvalidArgumentException( __METHOD__ . ": unexpected key $field for IExpression value" );
				}
				$exprs[] = new Expression( $field, '=', $cond );
			}
		}
		// @phan-suppress-next-line PhanTypeInstantiateAbstractStatic
		return new static( ...$exprs );
	}

	/**
	 * @param DbQuoter $dbQuoter
	 * @return string
	 * @return-taint none
	 */
	final public function toSql( DbQuoter $dbQuoter ): string {
		if ( !$this->children ) {
			throw new InvalidArgumentException( "The array of values can't be empty." );
		}
		$sqls = array_map( static fn ( $value ) => $value->toSql( $dbQuoter ), $this->children );
		return '(' . implode( ' ' . $this->getType() . ' ', $sqls ) . ')';
	}

	final public function toGeneralizedSql(): string {
		if ( !$this->children ) {
			throw new InvalidArgumentException( "The array of values can't be empty." );
		}
		$sqls = array_map( static fn ( $value ) => $value->toGeneralizedSql(), $this->children );
		return '(' . implode( ' ' . $this->getType() . ' ', $sqls ) . ')';
	}
}