$collection, static::$idKey => $id ]; if ($database !== null) { $ref['$db'] = $database; } return $ref; } /** * This not actually follow the reference, so it does not determine if it is broken or not. * It merely checks that $ref is in valid database reference format (in that it is an object or array with $ref and $id fields). * * @link http://php.net/manual/en/mongodbref.isref.php * @static * @param mixed $ref Array or object to check * @return boolean Returns true if $ref is a reference */ public static function isRef($ref) { $check = (array) $ref; return array_key_exists(static::$refKey, $check) && array_key_exists(static::$idKey, $check); } /** * Fetches the object pointed to by a reference * @link http://php.net/manual/en/mongodbref.get.php * @static * @param MongoDB $db Database to use * @param array $ref Reference to fetch * @return array|null Returns the document to which the reference refers or null if the document does not exist (the reference is broken) */ public static function get($db, $ref) { if (! static::isRef($ref)) { return null; } return $db->selectCollection($ref[static::$refKey])->findOne(['_id' => $ref[static::$idKey]]); } }