Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap\Search\LogicalOperator; |
| 6 | |
| 7 | use 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 | */ |
| 13 | final class OrConditions implements ConditionInterface |
| 14 | { |
| 15 | /** |
| 16 | * The conditions that together represent the expression. |
| 17 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 18 | * @var ConditionInterface[] |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 19 | */ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 20 | private array $conditions = []; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 21 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 22 | /** |
| 23 | * @param ConditionInterface[] $conditions |
| 24 | */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 25 | 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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 36 | * |
| 37 | * @return void |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 38 | */ |
| 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 | } |