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