dbEngine = $engine; $this->collection = $this->getCollection($tableName); $this->setup = true; } public function isSetup(): bool { return $this->setup; } /** * @param string $collectionString * @return Collection * @throws DatabaseException */ protected function getCollection(string $collectionString) { // Determine collection $coll = explode('.', $collectionString); if (count($coll) != 2) throw new DatabaseException("Could not load MongoTableModel. Provided tableName is not a valid collection string."); return $this->dbEngine->{$coll[0]}->{$coll[1]}; } /** * @return string */ public function getName(): string { return 'mongo'; } /** * @return string */ public function getEngineName(): string { return 'mongo'; } /** * @return iDatabaseEngine * @throws DatabaseException */ public function getEngine(): iDatabaseEngine { if (!$this->setup) throw new DatabaseException("Could not return Engine. Engine not setup yet."); return $this->dbEngine; } /** * @param array $data * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function create(array $data, array $options = [], string $table = 'default'): int { // If not data is provided, stop now if (empty($data)) throw new DatabaseException("Could not create data. No data provided."); // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // And execute the request if ($this->arrIsAssoc($data)) $res = $collection->insertOne($data, $options); else $res = $collection->insertMany($data, $options); // And return the count of inserted documents return $res->getInsertedCount(); } /** * @param array $filter * @param array $options * @param string $table * @return array * @throws DatabaseException */ public function read(array $filter = [], array $options = [], string $table = 'default'): TableModelResult { // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // Execute the request $results = $collection->find($filter, $options); // Convert the result into a TableModelResult $result = new TableModelResult($results); return $result; } /** * @param array $data * @param array $filter * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function update(array $data, array $filter, array $options = [], string $table = 'default'): int { // If not data is provided, stop now if (empty($data)) throw new DatabaseException("Could not create data. No data provided."); // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // And execute the request $data = ['$set' => $data]; $res = $collection->updateMany($filter, $data, $options); // Return the result return $res->getModifiedCount(); } /** * @param array $filter * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function delete(array $filter, array $options = [], string $table = 'default'): int { // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // Execute the request $res = $collection->deleteMany($filter, $options); // Return the result return $res->getDeletedCount(); } /** * @return bool */ public function transactionStart(): bool { // TODO: Implement transactionStart() method. } /** * @return bool */ public function transactionEnd(): bool { // TODO: Implement transactionEnd() method. } /** * @return bool */ public function transactionCommit(): bool { // TODO: Implement transactionCommit() method. } /** * @return bool */ public function transactionRollback(): bool { // TODO: Implement transactionRollback() method. } /** * Determines whether an array is associative or numeric * * @param array $arr * @return bool */ private function arrIsAssoc(array $arr): bool { if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } }