|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace MongoDB\Examples; |
| 5 | + |
| 6 | +use MongoDB\BSON\ObjectId; |
| 7 | +use MongoDB\BSON\Unserializable; |
| 8 | +use MongoDB\Client; |
| 9 | +use UnexpectedValueException; |
| 10 | + |
| 11 | +use function dirname; |
| 12 | +use function getenv; |
| 13 | +use function is_array; |
| 14 | +use function var_dump; |
| 15 | + |
| 16 | +require dirname(__FILE__) . '/../vendor/autoload.php'; |
| 17 | + |
| 18 | +class TypemapEntry implements Unserializable |
| 19 | +{ |
| 20 | + /** @var ObjectId */ |
| 21 | + private $id; |
| 22 | + |
| 23 | + /** @var string */ |
| 24 | + private $name; |
| 25 | + |
| 26 | + /** @var array<TypemapEmail> */ |
| 27 | + private $emails; |
| 28 | + |
| 29 | + private function __construct() |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public function getId(): ObjectId |
| 34 | + { |
| 35 | + return $this->id; |
| 36 | + } |
| 37 | + |
| 38 | + public function getName(): string |
| 39 | + { |
| 40 | + return $this->name; |
| 41 | + } |
| 42 | + |
| 43 | + public function getEmails(): array |
| 44 | + { |
| 45 | + return $this->emails; |
| 46 | + } |
| 47 | + |
| 48 | + public function bsonUnserialize(array $data): void |
| 49 | + { |
| 50 | + if (! $data['_id'] instanceof ObjectId) { |
| 51 | + throw new UnexpectedValueException('_id field is not of the expected type'); |
| 52 | + } |
| 53 | + |
| 54 | + if (! is_array($data['emails'])) { |
| 55 | + throw new UnexpectedValueException('emails field is not of the expected type'); |
| 56 | + } |
| 57 | + |
| 58 | + $this->id = $data['_id']; |
| 59 | + $this->name = (string) $data['name']; |
| 60 | + |
| 61 | + /** @psalm-suppress MixedPropertyTypeCoercion */ |
| 62 | + $this->emails = $data['emails']; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class TypemapEmail implements Unserializable |
| 67 | +{ |
| 68 | + /** @var string */ |
| 69 | + private $type; |
| 70 | + |
| 71 | + /** @var string */ |
| 72 | + private $address; |
| 73 | + |
| 74 | + private function __construct() |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + public function getType(): string |
| 79 | + { |
| 80 | + return $this->type; |
| 81 | + } |
| 82 | + |
| 83 | + public function getAddress(): string |
| 84 | + { |
| 85 | + return $this->address; |
| 86 | + } |
| 87 | + |
| 88 | + public function bsonUnserialize(array $data): void |
| 89 | + { |
| 90 | + $this->type = (string) $data['type']; |
| 91 | + $this->address = (string) $data['address']; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); |
| 96 | + |
| 97 | +$collection = $client->test->coll; |
| 98 | +$collection->drop(); |
| 99 | + |
| 100 | +$document = [ |
| 101 | + 'name' => 'alcaeus', |
| 102 | + 'emails' => [ |
| 103 | + [ 'type' => 'work', 'address' => '[email protected]'], |
| 104 | + [ 'type' => 'private', 'address' => '[email protected]'], |
| 105 | + ], |
| 106 | +]; |
| 107 | + |
| 108 | +$collection->insertOne($document); |
| 109 | + |
| 110 | +$typeMap = [ |
| 111 | + 'root' => TypemapEntry::class, // Root object will be an Entry instance |
| 112 | + 'fieldPaths' => [ |
| 113 | + 'emails' => 'array', // Emails field is used as PHP array |
| 114 | + 'emails.$' => TypemapEmail::class, // Each element in the emails array will be an Email instance |
| 115 | + ], |
| 116 | +]; |
| 117 | + |
| 118 | +$entry = $collection->findOne([], ['typeMap' => $typeMap]); |
| 119 | + |
| 120 | +/** @psalm-suppress ForbiddenCode */ |
| 121 | +var_dump($entry); |
0 commit comments