1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace Laminas\Session\SaveHandler;
use Laminas\Session\Exception\InvalidArgumentException;
use Laminas\Stdlib\AbstractOptions;
use function is_array;
use function phpversion;
use function strlen;
use function strtolower;
use function version_compare;
/**
* MongoDB session save handler Options
*/
class MongoDBOptions extends AbstractOptions
{
/**
* Database name
*
* @var string
*/
protected $database;
/**
* Collection name
*
* @var string
*/
protected $collection;
/**
* Save options
*
* @see http://php.net/manual/en/mongocollection.save.php
*
* @var string
*/
protected $saveOptions = ['w' => 1];
/**
* Name field
*
* @var string
*/
protected $nameField = 'name';
/**
* Data field
*
* @var string
*/
protected $dataField = 'data';
/**
* Lifetime field
*
* @var string
*/
protected $lifetimeField = 'lifetime';
/**
* Modified field
*
* @var string
*/
protected $modifiedField = 'modified';
/**
* Use expireAfterSeconds index
*
* @var bool
*/
protected $useExpireAfterSecondsIndex = false;
/**
* {@inheritdoc}
*/
public function __construct($options = null)
{
parent::__construct($options);
$mongoVersion = phpversion('mongo') ?: '0.0.0';
if ($this->saveOptions === ['w' => 1] && version_compare($mongoVersion, '1.3.0', '<')) {
$this->saveOptions = ['safe' => true];
}
}
/**
* Override AbstractOptions::__set
*
* Validates value if save options are being set.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
if (strtolower($key) !== 'saveoptions') {
parent::__set($key, $value);
return;
}
if (! is_array($value)) {
throw new InvalidArgumentException('Expected array for save options');
}
$this->setSaveOptions($value);
}
/**
* Set database name
*
* @param string $database
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setDatabase($database)
{
$database = (string) $database;
if (strlen($database) === 0) {
throw new InvalidArgumentException('$database must be a non-empty string');
}
$this->database = $database;
return $this;
}
/**
* Get database name
*
* @return string
*/
public function getDatabase()
{
return $this->database;
}
/**
* Set collection name
*
* @param string $collection
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setCollection($collection)
{
$collection = (string) $collection;
if (strlen($collection) === 0) {
throw new InvalidArgumentException('$collection must be a non-empty string');
}
$this->collection = $collection;
return $this;
}
/**
* Get collection name
*
* @return string
*/
public function getCollection()
{
return $this->collection;
}
/**
* Set save options
*
* @see http://php.net/manual/en/mongocollection.save.php
*
* @param array $saveOptions
* @return MongoDBOptions
*/
public function setSaveOptions(array $saveOptions)
{
$this->saveOptions = $saveOptions;
return $this;
}
/**
* Get save options
*
* @return string
*/
public function getSaveOptions()
{
return $this->saveOptions;
}
/**
* Set name field
*
* @param string $nameField
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setNameField($nameField)
{
$nameField = (string) $nameField;
if (strlen($nameField) === 0) {
throw new InvalidArgumentException('$nameField must be a non-empty string');
}
$this->nameField = $nameField;
return $this;
}
/**
* Get name field
*
* @return string
*/
public function getNameField()
{
return $this->nameField;
}
/**
* Set data field
*
* @param string $dataField
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setDataField($dataField)
{
$dataField = (string) $dataField;
if (strlen($dataField) === 0) {
throw new InvalidArgumentException('$dataField must be a non-empty string');
}
$this->dataField = $dataField;
return $this;
}
/**
* Get data field
*
* @return string
*/
public function getDataField()
{
return $this->dataField;
}
/**
* Set lifetime field
*
* @param string $lifetimeField
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setLifetimeField($lifetimeField)
{
$lifetimeField = (string) $lifetimeField;
if (strlen($lifetimeField) === 0) {
throw new InvalidArgumentException('$lifetimeField must be a non-empty string');
}
$this->lifetimeField = $lifetimeField;
return $this;
}
/**
* Get lifetime Field
*
* @return string
*/
public function getLifetimeField()
{
return $this->lifetimeField;
}
/**
* Set Modified Field
*
* @param string $modifiedField
* @return MongoDBOptions
* @throws InvalidArgumentException
*/
public function setModifiedField($modifiedField)
{
$modifiedField = (string) $modifiedField;
if (strlen($modifiedField) === 0) {
throw new InvalidArgumentException('$modifiedField must be a non-empty string');
}
$this->modifiedField = $modifiedField;
return $this;
}
/**
* Get modified Field
*
* @return string
*/
public function getModifiedField()
{
return $this->modifiedField;
}
/**
* @return boolean
*/
public function useExpireAfterSecondsIndex()
{
return $this->useExpireAfterSecondsIndex;
}
/**
* Enable expireAfterSeconds index.
*
* @see http://docs.mongodb.org/manual/tutorial/expire-data/
*
* @param boolean $useExpireAfterSecondsIndex
*/
public function setUseExpireAfterSecondsIndex($useExpireAfterSecondsIndex)
{
$this->useExpireAfterSecondsIndex = (bool) $useExpireAfterSecondsIndex;
}
}