1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silviooosilva\CacheerPhp\CacheStore\CacheManager\OptionBuilders; |
6
|
|
|
|
7
|
|
|
use Silviooosilva\CacheerPhp\Support\TimeBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FileOptionBuilder |
11
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
12
|
|
|
* @package Silviooosilva\CacheerPhp |
13
|
|
|
*/ |
14
|
|
|
class FileOptionBuilder |
15
|
|
|
{ |
16
|
|
|
/** @param null|string $cacheDir */ |
17
|
|
|
private ?string $cacheDir = null; |
18
|
|
|
|
19
|
|
|
/** @param null|string $expirationTime */ |
20
|
|
|
private ?string $expirationTime = null; |
21
|
|
|
|
22
|
|
|
/** @param null|string $flushAfter */ |
23
|
|
|
private ?string $flushAfter = null; |
24
|
|
|
|
25
|
|
|
/** @param array $options */ |
26
|
|
|
private array $options = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $cacheDir |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function dir(string $cacheDir) |
33
|
|
|
{ |
34
|
|
|
$this->cacheDir = $cacheDir; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ?string $expirationTime |
40
|
|
|
* @return $this|TimeBuilder |
41
|
|
|
*/ |
42
|
|
|
public function expirationTime(?string $expirationTime = null) |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
if (!is_null($expirationTime)) { |
46
|
|
|
$this->expirationTime = $expirationTime; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return new TimeBuilder(function ($formattedTime){ |
51
|
|
|
$this->expirationTime = $formattedTime; |
52
|
|
|
}, $this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ?string $flushAfter |
57
|
|
|
* @return $this|TimeBuilder |
58
|
|
|
*/ |
59
|
|
|
public function flushAfter(?string $flushAfter = null) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
if (!is_null($flushAfter)) { |
63
|
|
|
$this->flushAfter = mb_strtolower($flushAfter, 'UTF-8'); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new TimeBuilder(function ($formattedTime){ |
68
|
|
|
$this->flushAfter = $formattedTime; |
69
|
|
|
}, $this); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function build() |
76
|
|
|
{ |
77
|
|
|
return $this->validated(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
private function validated() |
84
|
|
|
{ |
85
|
|
|
foreach ($this->properties() as $key => $value) { |
86
|
|
|
if ($this->isValidAndNotNull($value)) { |
87
|
|
|
$this->options[$key] = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $this->options; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $data |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
private function isValidAndNotNull(mixed $data) |
98
|
|
|
{ |
99
|
|
|
return !empty($data) ? true : false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
private function properties() |
106
|
|
|
{ |
107
|
|
|
$properties = [ |
108
|
|
|
'cacheDir' => $this->cacheDir, |
109
|
|
|
'expirationTime' => $this->expirationTime, |
110
|
|
|
'flushAfter' => $this->flushAfter |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
return $properties; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|