blob: 61bd05e7d513ff618e790272289dfbd73251fd82 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3declare(strict_types=1);
4
5namespace Ddeboer\Imap\Search;
6
7use DateTimeInterface;
8
9/**
10 * Represents a date condition.
11 */
12abstract class AbstractDate implements ConditionInterface
13{
14 /**
15 * Format for dates to be sent to the IMAP server.
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010016 */
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020017 private string $dateFormat;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010018
19 /**
20 * The date to be used for the condition.
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010021 */
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020022 private DateTimeInterface $date;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010023
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}