6
6
7
7
use InvalidArgumentException ;
8
8
use Psr \SimpleCache \CacheInterface ;
9
+ use SessionHandlerInterface ;
9
10
10
11
class Factory
11
12
{
@@ -17,71 +18,131 @@ public function configFromArray(array $settings): Config
17
18
$ config = new Config ();
18
19
19
20
if (isset ($ settings ['save_path ' ])) {
21
+ if (!is_string ($ settings ['save_path ' ])) {
22
+ throw new InvalidArgumentException ('save_path must be a string ' );
23
+ }
20
24
$ config ->setSavePath ($ settings ['save_path ' ]);
21
25
}
22
26
23
27
if (isset ($ settings ['save_handler ' ])) {
28
+ if (! ($ settings ['save_handler ' ] instanceof SessionHandlerInterface)) {
29
+ throw new InvalidArgumentException ('save_handler must implement SessionHandlerInterface ' );
30
+ }
24
31
$ config ->setSaveHandler ($ settings ['save_handler ' ]);
25
32
}
26
33
27
34
if (isset ($ settings ['serialize_handler ' ])) {
35
+ if (!is_string ($ settings ['serialize_handler ' ]) && !is_null ($ settings ['serialize_handler ' ])) {
36
+ throw new InvalidArgumentException ('serialize_handler must be a string or null ' );
37
+ }
28
38
$ config ->setSerializeHandler (
29
39
Serializers \Factory::auto ($ settings ['serialize_handler ' ])
30
40
);
31
41
}
32
42
33
43
if (isset ($ settings ['name ' ])) {
44
+ if (!is_string ($ settings ['name ' ])) {
45
+ throw new InvalidArgumentException ('name must be a string ' );
46
+ }
34
47
$ config ->setName ($ settings ['name ' ]);
35
48
}
36
49
37
50
if (isset ($ settings ['cookie_lifetime ' ])) {
38
- $ config ->setCookieLifetime ((int ) $ settings ['cookie_lifetime ' ]);
51
+ if (!is_int ($ settings ['cookie_lifetime ' ])) {
52
+ throw new InvalidArgumentException ('cookie_lifetime must be an integer ' );
53
+ }
54
+ $ config ->setCookieLifetime ($ settings ['cookie_lifetime ' ]);
39
55
}
56
+
40
57
if (isset ($ settings ['cookie_path ' ])) {
58
+ if (!is_string ($ settings ['cookie_path ' ])) {
59
+ throw new InvalidArgumentException ('cookie_path must be a string ' );
60
+ }
41
61
$ config ->setCookiePath ($ settings ['cookie_path ' ]);
42
62
}
63
+
43
64
if (isset ($ settings ['cookie_domain ' ])) {
65
+ if (!is_string ($ settings ['cookie_domain ' ])) {
66
+ throw new InvalidArgumentException ('cookie_domain must be a string ' );
67
+ }
44
68
$ config ->setCookieDomain ($ settings ['cookie_domain ' ]);
45
69
}
70
+
46
71
if (isset ($ settings ['cookie_secure ' ])) {
47
- $ config ->setCookieSecure ((bool ) $ settings ['cookie_secure ' ]);
72
+ if (!is_bool ($ settings ['cookie_secure ' ])) {
73
+ throw new InvalidArgumentException ('cookie_secure must be a boolean ' );
74
+ }
75
+ $ config ->setCookieSecure ($ settings ['cookie_secure ' ]);
48
76
}
77
+
49
78
if (isset ($ settings ['cookie_httponly ' ])) {
50
- $ config ->setCookieHttpOnly ((bool ) $ settings ['cookie_httponly ' ]);
79
+ if (!is_bool ($ settings ['cookie_httponly ' ])) {
80
+ throw new InvalidArgumentException ('cookie_httponly must be a boolean ' );
81
+ }
82
+ $ config ->setCookieHttpOnly ($ settings ['cookie_httponly ' ]);
51
83
}
84
+
52
85
if (isset ($ settings ['cookie_samesite ' ])) {
86
+ if (!is_string ($ settings ['cookie_samesite ' ])) {
87
+ throw new InvalidArgumentException ('cookie_samesite must be a string ' );
88
+ }
53
89
$ config ->setCookieSameSite ($ settings ['cookie_samesite ' ]);
54
90
}
55
91
56
92
if (isset ($ settings ['cache_limiter ' ])) {
93
+ if (!is_string ($ settings ['cache_limiter ' ])) {
94
+ throw new InvalidArgumentException ('cache_limiter must be a string ' );
95
+ }
57
96
$ config ->setCacheLimiter ($ settings ['cache_limiter ' ]);
58
97
}
98
+
59
99
if (isset ($ settings ['cache_expire ' ])) {
60
- $ config ->setCacheExpire ((int ) $ settings ['cache_expire ' ]);
100
+ if (!is_int ($ settings ['cache_expire ' ])) {
101
+ throw new InvalidArgumentException ('cache_expire must be an integer ' );
102
+ }
103
+ $ config ->setCacheExpire ($ settings ['cache_expire ' ]);
61
104
}
62
105
63
106
if (isset ($ settings ['gc_probability ' ])) {
64
- $ config ->setGcProbability ((int ) $ settings ['gc_probability ' ]);
107
+ if (!is_int ($ settings ['gc_probability ' ])) {
108
+ throw new InvalidArgumentException ('gc_probability must be an integer ' );
109
+ }
110
+ $ config ->setGcProbability ($ settings ['gc_probability ' ]);
65
111
}
66
112
67
113
if (isset ($ settings ['gc_divisor ' ])) {
68
- $ config ->setGcDivisor ((int ) $ settings ['gc_divisor ' ]);
114
+ if (!is_int ($ settings ['gc_divisor ' ])) {
115
+ throw new InvalidArgumentException ('gc_divisor must be an integer ' );
116
+ }
117
+ $ config ->setGcDivisor ($ settings ['gc_divisor ' ]);
69
118
}
70
119
71
120
if (isset ($ settings ['gc_maxlifetime ' ])) {
72
- $ config ->setGcMaxLifetime ((int ) $ settings ['gc_maxlifetime ' ]);
121
+ if (!is_int ($ settings ['gc_maxlifetime ' ])) {
122
+ throw new InvalidArgumentException ('gc_maxlifetime must be an integer ' );
123
+ }
124
+ $ config ->setGcMaxLifetime ($ settings ['gc_maxlifetime ' ]);
73
125
}
74
126
75
127
if (isset ($ settings ['sid_length ' ])) {
76
- $ config ->setSidLength ((int ) $ settings ['sid_length ' ]);
128
+ if (!is_int ($ settings ['sid_length ' ])) {
129
+ throw new InvalidArgumentException ('sid_length must be an integer ' );
130
+ }
131
+ $ config ->setSidLength ($ settings ['sid_length ' ]);
77
132
}
78
133
79
134
if (isset ($ settings ['sid_bits_per_character ' ])) {
80
- $ config ->setSidBitsPerCharacter ((int ) $ settings ['sid_bits_per_character ' ]);
135
+ if (!is_int ($ settings ['sid_bits_per_character ' ])) {
136
+ throw new InvalidArgumentException ('sid_bits_per_character must be an integer ' );
137
+ }
138
+ $ config ->setSidBitsPerCharacter ($ settings ['sid_bits_per_character ' ]);
81
139
}
82
140
83
141
if (isset ($ settings ['lazy_write ' ])) {
84
- $ config ->setLazyWrite ((bool ) $ settings ['lazy_write ' ]);
142
+ if (!is_bool ($ settings ['lazy_write ' ])) {
143
+ throw new InvalidArgumentException ('lazy_write must be a boolean ' );
144
+ }
145
+ $ config ->setLazyWrite ($ settings ['lazy_write ' ]);
85
146
}
86
147
87
148
return $ config ;
0 commit comments