Skip to content

Instantly share code, notes, and snippets.

@eman1986
Created January 12, 2022 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eman1986/86cb6386fa13dd1d12fe0c603c21e3a2 to your computer and use it in GitHub Desktop.
Save eman1986/86cb6386fa13dd1d12fe0c603c21e3a2 to your computer and use it in GitHub Desktop.
trying to show off using the new __serialize() and __unserialize() magic methods
<?php
require 'vendor/autoload.php';
use App\Model\Users;
use Symfony\Component\Uid\Uuid;
$mongo = new MongoDB\Client('mongodb://localhost:27017/');
$collection = $mongo->selectCollection('demo', 'users');
$user = new Users();
$user->userUuid = Uuid::v4()->toRfc4122();
$user->emailAddress = 'ac1Xo2NmH@example.com';
$user->firstName = 'Mouse';
$user->lastName = 'Provide';
$user->roles = ['USER'];
$result = $collection->insertOne($user);
echo 'Inserted with Object ID: '. $result->getInsertedId()."\n";
<?php
require 'vendor/autoload.php';
$typeMap = [
'array' => 'array',
'document' => 'MongoDB\Model\BSONDocument',
'root' => 'MongoDB\Model\BSONDocument'
];
$mongo = new MongoDB\Client('mongodb://localhost:27017/');
$collection = $mongo->selectCollection('demo', 'users');
$query = [
'UserUuid' => '47f4b337-cd9d-446a-b030-c311e534e94a'
];
$document = $collection->findOne($query, ['typeMap' => $typeMap]);
if (null === $document)
{
echo "Nothing was found.\n";
}
else
{
var_dump($document);
}
<?php
namespace App\Model;
use DateTimeImmutable;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Persistable;
use MongoDB\BSON\UTCDateTime;
class Users
{
public ?ObjectId $id = null;
public string $userUuid = '';
public string $emailAddress = '';
public string $firstName = '';
public string $lastName = '';
public string $language = 'en';
public array $roles = [];
public bool $isUserLocked = false;
public bool $isUserEnabled = true;
public DateTimeImmutable $createdOn;
public DateTimeImmutable $updatedOn;
public function __construct()
{
$this->id = new ObjectId();
$this->createdOn = new DateTimeImmutable();
$this->updatedOn = new DateTimeImmutable();
}
public function __serialize(): array
{
return [
'_id' => $this->id,
'CreatedOn' => new UTCDateTime($this->createdOn),
'UpdatedOn' => new UTCDateTime($this->updatedOn),
'UserUuid' => $this->userUuid,
'EmailAddress' => $this->emailAddress,
'FirstName' => $this->firstName,
'LastName' => $this->lastName,
'Roles' => $this->roles,
'IsUserLocked' => $this->isUserLocked,
'IsUserEnabled' => $this->isUserEnabled
];
}
public function __unserialize(array $data): void
{
$this->id = $data['_id'];
$this->createdOn = DateTimeImmutable::createFromMutable($data['CreatedOn']->toDateTime());
$this->updatedOn = DateTimeImmutable::createFromMutable($data['UpdatedOn']->toDateTime());
$this->userUuid = $data['UserUuid'];
$this->emailAddress = $data['EmailAddress'];
$this->firstName = $data['FirstName'];
$this->lastName = $data['LastName'];
$this->roles = $data['Roles'];
$this->isUserLocked = $data['IsUserLocked'];
$this->isUserEnabled = $data['IsUserLocked'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment