public function toArray($document) {
        if ($this->isBSONModel($document)) {
            // If the document is a BSON model, get the array copy first
            $document = $document->getArrayCopy();
        } else if (!is_array($document)) {
            // The variable to convert is not an array, simply return it as-is
            return $document;
        }

        $result = [];

        foreach ($document as $key => $value) {
            if ($this->isBSONModel($value)) {
                // The value is another model, convert it as well
                $value = $this->toArray($value->getArrayCopy());
            }

            // Regular value, set it
            $result[$key] = $value;
        }

        return $result;
    }