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 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | private $conditions = []; |
| 21 | |
| 22 | public function __construct(array $conditions) |
| 23 | { |
| 24 | foreach ($conditions as $condition) { |
| 25 | $this->addCondition($condition); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Adds a new condition to the expression. |
| 31 | * |
| 32 | * @param ConditionInterface $condition the condition to be added |
| 33 | */ |
| 34 | private function addCondition(ConditionInterface $condition) |
| 35 | { |
| 36 | $this->conditions[] = $condition; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns the keyword that the condition represents. |
| 41 | */ |
| 42 | public function toString(): string |
| 43 | { |
| 44 | $conditions = \array_map(static function (ConditionInterface $condition): string { |
| 45 | return $condition->toString(); |
| 46 | }, $this->conditions); |
| 47 | |
| 48 | return \sprintf('( %s )', \implode(' OR ', $conditions)); |
| 49 | } |
| 50 | } |