public function testSetters() { $options = new MongoDBOptions(); $options->setDatabase('testDatabase')->setCollection('testCollection')->setSaveOptions(array('safe' => 2))->setNameField('testName')->setDataField('testData')->setLifetimeField('testLifetime')->setModifiedField('testModified'); $this->assertEquals('testDatabase', $options->getDatabase()); $this->assertEquals('testCollection', $options->getCollection()); $this->assertEquals(array('safe' => 2), $options->getSaveOptions()); $this->assertEquals('testName', $options->getNameField()); $this->assertEquals('testData', $options->getDataField()); $this->assertEquals('testLifetime', $options->getLifetimeField()); $this->assertEquals('testModified', $options->getModifiedField()); }
/** * Write session data * * @param string $id * @param string $data * @return bool */ public function write($id, $data) { $saveOptions = array_replace($this->options->getSaveOptions(), array('upsert' => true, 'multiple' => false)); $criteria = array('_id' => $id, $this->options->getNameField() => $this->sessionName); $newObj = array('$set' => array($this->options->getDataField() => (string) $data, $this->options->getLifetimeField() => $this->lifetime, $this->options->getModifiedField() => new MongoDate())); /* Note: a MongoCursorException will be thrown if a record with this ID * already exists with a different session name, since the upsert query * cannot insert a new document with the same ID and new session name. * This should only happen if ID's are not unique or if the session name * is altered mid-process. */ $result = $this->mongoCollection->update($criteria, $newObj, $saveOptions); return (bool) (isset($result['ok']) ? $result['ok'] : $result); }
/** * Write session data * * @param string $id * @param string $data * @return bool */ public function write($id, $data) { $saveOptions = array_replace($this->options->getSaveOptions(), ['upsert' => true, 'multiple' => false]); $criteria = ['_id' => $id, $this->options->getNameField() => $this->sessionName]; $newObj = ['$set' => [$this->options->getDataField() => new Binary((string) $data, Binary::TYPE_GENERIC), $this->options->getLifetimeField() => $this->lifetime, $this->options->getModifiedField() => new UTCDatetime(floor(microtime(true) * 1000))]]; /* Note: a MongoCursorException will be thrown if a record with this ID * already exists with a different session name, since the upsert query * cannot insert a new document with the same ID and new session name. * This should only happen if ID's are not unique or if the session name * is altered mid-process. */ $result = $this->mongoCollection->updateOne($criteria, $newObj, $saveOptions); return $result->isAcknowledged(); }