1<?php
2
3
4namespace ComboStrap;
5
6
7class CacheExpirationFrequency extends MetadataText
8{
9
10    /**
11     * The meta that has the cron expression
12     */
13    public const PROPERTY_NAME = "cache_expiration_frequency";
14    public const CANONICAL = "page-cache-expiration-frequency";
15
16    public static function createForPage(ResourceCombo $page): CacheExpirationFrequency
17    {
18        return (new CacheExpirationFrequency())
19            ->setResource($page);
20    }
21
22    public function getTab(): string
23    {
24        return MetaManagerForm::TAB_CACHE_VALUE;
25    }
26
27    /**
28     * @param string|null $value
29     * @return Metadata
30     * @throws ExceptionCombo
31     */
32    public function setValue($value): Metadata
33    {
34
35        if ($value === null) {
36            parent::setValue($value);
37            return $this;
38        }
39
40        $value = trim($value);
41        if ($value === "") {
42            // html form send an empty string
43            return $this;
44        }
45
46        try {
47            $cacheExpirationCalculatedDate = Cron::getDate($value);
48            $cacheExpirationDate = CacheExpirationDate::createForPage($this->getResource());
49            $cacheExpirationDate
50                ->setValue($cacheExpirationCalculatedDate)
51                ->persist();
52            parent::setValue($value);
53            return $this;
54        } catch (ExceptionCombo $e) {
55            throw new ExceptionCombo("The cache frequency expression ($value) is not a valid cron expression. <a href=\"https://crontab.guru/\">Validate it on this website</a>", CacheExpirationFrequency::PROPERTY_NAME, 0, $e);
56        }
57
58    }
59
60
61    public function getDescription(): string
62    {
63        return "A page expiration frequency expressed as a cron expression";
64    }
65
66    public function getLabel(): string
67    {
68        return "Cache Expiration Frequency";
69    }
70
71    public static function getName(): string
72    {
73        return self::PROPERTY_NAME;
74    }
75
76    public function getPersistenceType(): string
77    {
78        return MetadataDokuWikiStore::PERSISTENT_METADATA;
79    }
80
81    public function getMutable(): bool
82    {
83        return true;
84    }
85
86    public function getDefaultValue()
87    {
88        return null;
89    }
90
91    public function getCanonical(): string
92    {
93        return self::CANONICAL;
94    }
95
96
97}
98