/** * {@inheritdoc } */ public function has($key) { $tKey = $this->getKey($key); $tNow = $this->getTtl(); return $this->collection->count(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))) > 0; }
/** * Auth */ public function connect(Request $request) { $email = $request->email; $password = $request->password; if (!empty($email) && !empty($password)) { $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); $collection = new \MongoDB\Collection($manager, 'builders', 'account'); if ($collection->count(["email" => $email]) == 0) { return response()->json(['data' => "User doesn't exist", 'state' => false]); } $user = $collection->findOne(["email" => $email])->bsonSerialize(); if (password_verify($password, $user->password) == false) { return response()->json(['data' => "Bad email or password", 'state' => false]); } $api = new Api($user); Auth::login($api); return response()->json(['data' => $user, 'state' => true]); } else { return response()->json(['data' => "Invalid parameters", 'state' => false]); } return response()->json(['data' => "Bad credentials", 'state' => false]); }
require_once __DIR__ . "/bootstrap.php"; $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); $collection = new MongoDB\Collection($manager, "phplib_demo.write"); $hannes = ["name" => "Hannes", "nick" => "bjori", "citizen" => "Iceland"]; $hayley = ["name" => "Hayley", "nick" => "Ninja", "citizen" => "USA"]; $bobby = ["name" => "Robert Fischer", "nick" => "Bobby Fischer", "citizen" => "USA"]; $kasparov = ["name" => "Garry Kimovich Kasparov", "nick" => "Kasparov", "citizen" => "Russia"]; $spassky = ["name" => "Boris Vasilievich Spassky", "nick" => "Spassky", "citizen" => "France"]; try { $result = $collection->insertOne($hannes); printf("Inserted _id: %s\n\n", $result->getInsertedId()); $result = $collection->insertOne($hayley); printf("Inserted _id: %s\n\n", $result->getInsertedId()); $result = $collection->insertOne($bobby); printf("Inserted _id: %s\n\n", $result->getInsertedId()); $count = $collection->count(["nick" => "bjori"]); printf("Searching for nick => bjori, should have only one result: %d\n\n", $count); $result = $collection->updateOne(["citizen" => "USA"], ['$set' => ["citizen" => "Iceland"]]); printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount()); $cursor = $collection->find(["citizen" => "Iceland"], ["comment" => "Excellent query"]); echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n"; foreach ($cursor as $document) { var_dump($document); } echo "\n"; } catch (Exception $e) { printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); exit; } try { $cursor = $collection->find();