Example #1
0
 /**
  * Filters any field in the $fields that has it's value specified as a
  * 'objectId'. It will wraps the $value, if any, into a ObjectID object.
  *
  * @param mixed $value Value that may be converted to ObjectID.
  *
  * @return ObjectID|mixed
  */
 public function objectId($value = null)
 {
     if ($value === null) {
         return new ObjectID();
     }
     if (is_string($value) && ObjectIdUtils::isObjectId($value)) {
         $value = new ObjectID($value);
     }
     return $value;
 }
Example #2
0
 /**
  * Constructor.
  *
  *
  * @param MongoObjectID|string $mongoId MongoDB ObjectID or a string.
  *
  * @throws InvalidArgumentException If $mongoId is not valid.
  */
 public function __construct($mongoId = null)
 {
     if (!$mongoId) {
         $mongoId = new MongoObjectID();
     }
     if (is_object($mongoId)) {
         $mongoId = (string) $mongoId;
     }
     if (!ObjectIdUtils::isObjectId($mongoId)) {
         throw new InvalidArgumentException('Invalid BSON ID provided');
     }
     $this->objectIdString = $mongoId;
 }
Example #3
0
 /**
  * Returns the cursor for the referenced documents as objects.
  *
  * @param string $entity Class of the entity or of the schema of the entity.
  * @param string $field  The field where the _ids are stored.
  *
  * @return array
  */
 protected function referencesMany(string $entity, string $field)
 {
     $referencedIds = (array) $this->{$field};
     if (ObjectIdUtils::isObjectId($referencedIds[0] ?? '')) {
         foreach ($referencedIds as $key => $value) {
             $referencedIds[$key] = new ObjectID($value);
         }
     }
     $query = ['_id' => ['$in' => array_values($referencedIds)]];
     $entityInstance = Ioc::make($entity);
     if ($entityInstance instanceof Schema) {
         $dataMapper = Ioc::make(DataMapper::class);
         $dataMapper->setSchema($entityInstance);
         return $dataMapper->where($query, [], true);
     }
     return $entityInstance::where($query, [], true);
 }
 /**
  * @dataProvider objectIdStringScenarios
  */
 public function testShouldEvaluateIfValueIsAnObjectid($value, $expectation)
 {
     $this->assertEquals($expectation, ObjectIdUtils::isObjectId($value));
 }
Example #5
0
 /**
  * Prepares an embedded array of an query. It will convert string ObjectIDs
  * in operators into actual objects.
  *
  * @param array $value Array that will be treated.
  *
  * @return array Prepared array.
  */
 protected function prepareArrayFieldOfQuery(array $value) : array
 {
     foreach (['$in', '$nin'] as $operator) {
         if (isset($value[$operator]) && is_array($value[$operator])) {
             foreach ($value[$operator] as $index => $id) {
                 if (ObjectIdUtils::isObjectId($id)) {
                     $value[$operator][$index] = new ObjectID($id);
                 }
             }
         }
     }
     return $value;
 }