blob: cdd7312667c76a3751e060b211102235ab88f41b [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.
16 *
17 * @var string
18 */
19 private $dateFormat;
20
21 /**
22 * The date to be used for the condition.
23 *
24 * @var DateTimeInterface
25 */
26 private $date;
27
28 /**
29 * Constructor.
30 *
31 * @param DateTimeInterface $date optional date for the condition
32 */
33 public function __construct(DateTimeInterface $date, string $dateFormat = 'j-M-Y')
34 {
35 $this->date = $date;
36 $this->dateFormat = $dateFormat;
37 }
38
39 /**
40 * Converts the condition to a string that can be sent to the IMAP server.
41 */
42 final public function toString(): string
43 {
44 return \sprintf('%s "%s"', $this->getKeyword(), $this->date->format($this->dateFormat));
45 }
46
47 /**
48 * Returns the keyword that the condition represents.
49 */
50 abstract protected function getKeyword(): string;
51}