Converting types with MondocTypes

Mondoc provides a set of type conversion methods that can be used to convert data between PHP and MongoDB. These methods are available in the MondocTypes class.

About MondocTypes

The MondocTypes class provides a set of static methods that can be used to convert data between PHP and MongoDB. These methods are useful when working with MongoDB documents and models, as they allow you to convert data between the two formats.

Exposed methods

Below is a list of the methods available in the MondocTypes class.

  • typeToJsonFriendly(mixed $v): mixed
    Converts a value to a JSON-friendly format. This method is useful when working with MongoDB variable types, or PHP types.
  • newObjectId(): \MongoDB\BSON\ObjectId
    Generate a new MongoDB ObjectId using the MongoDB PHP library.
  • objectIdToString(\MongoDB\BSON\ObjectId $id): string
    Converts a MongoDB ObjectId to a string.
  • toObjectId(\MongoDB\BSON\ObjectId|array|string|null $id): ?\MongoDB\BSON\ObjectId
    Converts a value to a MongoDB ObjectId. This method accepts a wide range of potential types, including an ObjectId, an array, or a string.
  • deduplicateArrayOfObjectIds(array $ids): array
    Deduplicates an array of MongoDB ObjectIds.
  • dateToPHPDateTime(mixed $provided): ?\DateTime
    Converts a MongoDB date to a PHP DateTime object. MongoDB UTCDateTime objects are alsoIf any other type is provided, the method will return null.
  • phpDateToMongoDateTime(mixed $provided): ?\MongoDB\BSON\UTCDateTime
    Converts a PHP DateTime object to a MongoDB UTCDateTime object. If any other type is provided, the method will return null.
  • arrayToPhp(mixed $item): mixed
    Converts an array to a PHP array. This method is useful when working with MongoDB BSONArray, or BSONDocument objects.

MondocTypes example

Below is an example of how you can use the MondocTypes class to convert datatypes.

<?php

use District5\Mondoc\Helper\MondocTypes;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;

$objectId = MondocTypes::newObjectId();

$objectIdString = MondocTypes::objectIdToString($objectId);

$backToObjectId = MondocTypes::toObjectId($objectIdString);

$jsonFriendly = MondocTypes::typeToJsonFriendly($objectId);
$jsonFriendlyString = MondocTypes::typeToJsonFriendly($objectIdString);
$jsonFriendlyObj = MondocTypes::typeToJsonFriendly(new stdClass());
$jsonFriendlyArray = MondocTypes::typeToJsonFriendly([1, 2, 3]);

$ids = [
    $objectId,
    $objectId,
    MondocTypes::newObjectId()
];
$deduplicatedIds = MondocTypes::deduplicateArrayOfObjectIds($ids); // 2 items returned

$utcDateTime = MondocTypes::phpDateToMongoDateTime(new DateTime());
$phpDateTime = MondocTypes::dateToPHPDateTime($utcDateTime);

$phpArrayFromArray = MondocTypes::arrayToPhp(
    ['key' => 'value']
);
$phpArrayFromBsonDocument = MondocTypes::arrayToPhp(
    new BSONDocument(['key' => 'value'])
);
$phpArrayFromBsonArray = MondocTypes::arrayToPhp(
    new BSONArray(['key' => 'value'])
);