Skip to content

Instantly share code, notes, and snippets.

@squio
Last active August 1, 2016 07:26
Show Gist options
  • Save squio/4bc7653144ec3b5d58d73e1b468f68ee to your computer and use it in GitHub Desktop.
Save squio/4bc7653144ec3b5d58d73e1b468f68ee to your computer and use it in GitHub Desktop.
Yii2 Legacy MongoRegex, MongoId implementation for backward compatibility with php < 7.0
<?php
use MongoDB\BSON\ObjectID;
/**
* Legacy MongoId implementation for backward compatibility with php < 7.0
* Usage: add this to entry script
* if (version_compare(phpversion(), '7.0.0', '>')) {
* // php v7 uses different MongoDB classes
* Yii::$classMap['MongoId'] = '@app/components/MongoId.php';
* }
*/
class MongoId
{
private $mongoId;
public function __construct($id)
{
$this->mongoId = new \MongoDB\BSON\ObjectID($id);
return $this->mongoId;
}
public function __toString()
{
return $this->mongoId->__toString();
}
}
<?php
use MongoDB\BSON\Regex;
/**
* Legacy MongoRegex implementation for backward compatibility with php < 7.0
* Usage: add this to entry script
* if (version_compare(phpversion(), '7.0.0', '>')) {
* // php v7 uses different MongoDB classes
* Yii::$classMap['MongoRegex'] = '@app/components/MongoRegex.php';
* }
*/
class MongoRegex implements MongoDB\BSON\Type
{
private $regex;
public function __construct($pattern)
{
$this->regex = new \MongoDB\BSON\Regex($pattern, null);
return $this->regex;
}
public function getFlags()
{
return $this->regex->getFlags();
}
public function getPattern()
{
return $this->regex->getPattern();
}
public function __toString()
{
return $this->regex->__toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment