blob: 01e7d86031783b8df14b060062d28b7485049004 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3declare(strict_types=1);
4
5namespace Ddeboer\Imap\Search\LogicalOperator;
6
7use Ddeboer\Imap\Search\ConditionInterface;
8
9/**
10 * Represents an OR operator. Messages only need to match one of the conditions
11 * after this operator to match the expression.
12 */
13final class OrConditions implements ConditionInterface
14{
15 /**
16 * The conditions that together represent the expression.
17 *
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020018 * @var ConditionInterface[]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010019 */
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020020 private array $conditions = [];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010021
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020022 /**
23 * @param ConditionInterface[] $conditions
24 */
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010025 public function __construct(array $conditions)
26 {
27 foreach ($conditions as $condition) {
28 $this->addCondition($condition);
29 }
30 }
31
32 /**
33 * Adds a new condition to the expression.
34 *
35 * @param ConditionInterface $condition the condition to be added
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020036 *
37 * @return void
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010038 */
39 private function addCondition(ConditionInterface $condition)
40 {
41 $this->conditions[] = $condition;
42 }
43
44 /**
45 * Returns the keyword that the condition represents.
46 */
47 public function toString(): string
48 {
49 $conditions = \array_map(static function (ConditionInterface $condition): string {
50 return $condition->toString();
51 }, $this->conditions);
52
53 return \sprintf('( %s )', \implode(' OR ', $conditions));
54 }
55}