Skip to content

Commit 999fa8b

Browse files
committed
Add example for persistable objects
1 parent 43878ed commit 999fa8b

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

examples/persistable.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MongoDB\Examples;
5+
6+
use MongoDB\BSON\ObjectId;
7+
use MongoDB\BSON\Persistable;
8+
use MongoDB\Client;
9+
use stdClass;
10+
11+
use function array_search;
12+
use function dirname;
13+
use function getenv;
14+
use function var_dump;
15+
16+
require dirname(__FILE__) . '/../vendor/autoload.php';
17+
18+
class Entry implements Persistable
19+
{
20+
/** @var ObjectId */
21+
private $id;
22+
23+
/** @var string */
24+
private $name;
25+
26+
/** @var array<Email> */
27+
private $emails = [];
28+
29+
public function __construct(string $name)
30+
{
31+
$this->id = new ObjectId();
32+
$this->name = $name;
33+
}
34+
35+
public function getId(): ObjectId
36+
{
37+
return $this->id;
38+
}
39+
40+
public function getName(): string
41+
{
42+
return $this->name;
43+
}
44+
45+
public function setName(string $name): void
46+
{
47+
$this->name = $name;
48+
}
49+
50+
public function getEmails(): array
51+
{
52+
return $this->emails;
53+
}
54+
55+
public function addEmail(Email $email): void
56+
{
57+
$this->emails[] = $email;
58+
}
59+
60+
public function deleteEmail(Email $email): void
61+
{
62+
$index = array_search($email, $this->emails, true);
63+
if ($index === false) {
64+
return;
65+
}
66+
67+
unset($this->emails[$index]);
68+
}
69+
70+
public function bsonSerialize(): stdClass
71+
{
72+
return (object) [
73+
'_id' => $this->id,
74+
'name' => $this->name,
75+
'emails' => $this->emails,
76+
];
77+
}
78+
79+
public function bsonUnserialize(array $data): void
80+
{
81+
$this->id = $data['_id'];
82+
$this->name = (string) $data['name'];
83+
$this->emails = $data['emails']->getArrayCopy(); // Emails will be passed as a BSONArray instance
84+
}
85+
}
86+
87+
class Email implements Persistable
88+
{
89+
/** @var string */
90+
private $type;
91+
92+
/** @var string */
93+
private $address;
94+
95+
public function __construct(string $type, string $address)
96+
{
97+
$this->type = $type;
98+
$this->address = $address;
99+
}
100+
101+
public function getType(): string
102+
{
103+
return $this->type;
104+
}
105+
106+
public function getAddress(): string
107+
{
108+
return $this->address;
109+
}
110+
111+
public function bsonSerialize(): stdClass
112+
{
113+
return (object) [
114+
'type' => $this->type,
115+
'address' => $this->address,
116+
];
117+
}
118+
119+
public function bsonUnserialize(array $data): void
120+
{
121+
$this->type = (string) $data['type'];
122+
$this->address = (string) $data['address'];
123+
}
124+
}
125+
126+
$entry = new Entry('alcaeus');
127+
$entry->addEmail(new Email('work', '[email protected]'));
128+
$entry->addEmail(new Email('private', '[email protected]'));
129+
130+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
131+
132+
$collection = $client->test->coll;
133+
$collection->drop();
134+
135+
$collection->insertOne($entry);
136+
137+
$foundEntry = $collection->findOne([]);
138+
139+
var_dump($foundEntry);

0 commit comments

Comments
 (0)