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; |
| 6 | |
| 7 | use DateTimeInterface; |
| 8 | |
| 9 | /** |
| 10 | * Represents a date condition. |
| 11 | */ |
| 12 | abstract class AbstractDate implements ConditionInterface |
| 13 | { |
| 14 | /** |
| 15 | * Format for dates to be sent to the IMAP server. |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 16 | */ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 17 | private string $dateFormat; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * The date to be used for the condition. |
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 | private DateTimeInterface $date; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @param DateTimeInterface $date optional date for the condition |
| 28 | */ |
| 29 | public function __construct(DateTimeInterface $date, string $dateFormat = 'j-M-Y') |
| 30 | { |
| 31 | $this->date = $date; |
| 32 | $this->dateFormat = $dateFormat; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Converts the condition to a string that can be sent to the IMAP server. |
| 37 | */ |
| 38 | final public function toString(): string |
| 39 | { |
| 40 | return \sprintf('%s "%s"', $this->getKeyword(), $this->date->format($this->dateFormat)); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the keyword that the condition represents. |
| 45 | */ |
| 46 | abstract protected function getKeyword(): string; |
| 47 | } |