6
6
use MongoDB \BSON \ObjectId ;
7
7
use MongoDB \BSON \Persistable ;
8
8
use MongoDB \Client ;
9
- use stdClass ;
9
+ use MongoDB \Model \BSONArray ;
10
+ use UnexpectedValueException ;
10
11
11
12
use function array_search ;
12
13
use function dirname ;
15
16
16
17
require dirname (__FILE__ ) . '/../vendor/autoload.php ' ;
17
18
18
- class Entry implements Persistable
19
+ class PersistableEntry implements Persistable
19
20
{
20
21
/** @var ObjectId */
21
22
private $ id ;
22
23
23
24
/** @var string */
24
25
private $ name ;
25
26
26
- /** @var array<Email > */
27
+ /** @var array<PersistableEmail > */
27
28
private $ emails = [];
28
29
29
30
public function __construct (string $ name )
@@ -52,12 +53,12 @@ public function getEmails(): array
52
53
return $ this ->emails ;
53
54
}
54
55
55
- public function addEmail (Email $ email ): void
56
+ public function addEmail (PersistableEmail $ email ): void
56
57
{
57
58
$ this ->emails [] = $ email ;
58
59
}
59
60
60
- public function deleteEmail (Email $ email ): void
61
+ public function deleteEmail (PersistableEmail $ email ): void
61
62
{
62
63
$ index = array_search ($ email , $ this ->emails , true );
63
64
if ($ index === false ) {
@@ -67,7 +68,7 @@ public function deleteEmail(Email $email): void
67
68
unset($ this ->emails [$ index ]);
68
69
}
69
70
70
- public function bsonSerialize (): stdClass
71
+ public function bsonSerialize (): object
71
72
{
72
73
return (object ) [
73
74
'_id ' => $ this ->id ,
@@ -78,13 +79,23 @@ public function bsonSerialize(): stdClass
78
79
79
80
public function bsonUnserialize (array $ data ): void
80
81
{
82
+ if (! $ data ['_id ' ] instanceof ObjectId) {
83
+ throw new UnexpectedValueException ('_id field is not of the expected type ' );
84
+ }
85
+
86
+ if (! $ data ['emails ' ] instanceof BSONArray) {
87
+ throw new UnexpectedValueException ('emails field is not of the expected type ' );
88
+ }
89
+
81
90
$ this ->id = $ data ['_id ' ];
82
91
$ this ->name = (string ) $ data ['name ' ];
92
+
93
+ /** @psalm-suppress MixedPropertyTypeCoercion */
83
94
$ this ->emails = $ data ['emails ' ]->getArrayCopy (); // Emails will be passed as a BSONArray instance
84
95
}
85
96
}
86
97
87
- class Email implements Persistable
98
+ class PersistableEmail implements Persistable
88
99
{
89
100
/** @var string */
90
101
private $ type ;
@@ -108,7 +119,7 @@ public function getAddress(): string
108
119
return $ this ->address ;
109
120
}
110
121
111
- public function bsonSerialize (): stdClass
122
+ public function bsonSerialize (): object
112
123
{
113
124
return (object ) [
114
125
'type ' => $ this ->type ,
@@ -123,9 +134,9 @@ public function bsonUnserialize(array $data): void
123
134
}
124
135
}
125
136
126
- $ entry = new Entry ('alcaeus ' );
127
- $ entry->
addEmail (
new Email (
'work ' ,
'[email protected] ' ));
128
- $ entry->
addEmail (
new Email (
'private ' ,
'[email protected] ' ));
137
+ $ entry = new PersistableEntry ('alcaeus ' );
138
+ $ entry->
addEmail (
new PersistableEmail (
'work ' ,
'[email protected] ' ));
139
+ $ entry->
addEmail (
new PersistableEmail (
'private ' ,
'[email protected] ' ));
129
140
130
141
$ client = new Client (getenv ('MONGODB_URI ' ) ?: 'mongodb://127.0.0.1/ ' );
131
142
@@ -136,4 +147,5 @@ public function bsonUnserialize(array $data): void
136
147
137
148
$ foundEntry = $ collection ->findOne ([]);
138
149
150
+ /** @psalm-suppress ForbiddenCode */
139
151
var_dump ($ foundEntry );
0 commit comments