Imbo\Helpers\BSONToArray::toArray PHP Method

toArray() public method

Convert to array, recursively
public toArray ( array | MongoDB\Model\BSONDocument | MongoDB\Model\BSONArray $document ) : mixed
$document array | MongoDB\Model\BSONDocument | MongoDB\Model\BSONArray
return mixed
    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;
    }

Usage Example

Example #1
0
File: Mongo.php Project: imbo/imbo
 /**
  * Get details for a given public key
  *
  * @param  string $publicKey
  * @return array
  */
 private function getPublicKeyDetails($publicKey)
 {
     if (isset($this->publicKeys[$publicKey])) {
         return $this->publicKeys[$publicKey];
     }
     // Not in cache, fetch from database
     $pubkeyInfo = $this->getAclCollection()->findOne(['publicKey' => $publicKey]);
     if (!$pubkeyInfo) {
         return [];
     }
     $data = $this->bsonToArray->toArray($pubkeyInfo->getArrayCopy());
     $this->publicKeys[$publicKey] = $data;
     return $data;
 }
All Usage Examples Of Imbo\Helpers\BSONToArray::toArray