From a32da14caf473953ad4d090f2e788ffe7acd9be4 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 13:35:38 +0200 Subject: [PATCH 01/12] Fix include path for autoload file --- examples/changestream.php | 5 +++-- examples/command_logger.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/changestream.php b/examples/changestream.php index b897da4b2..5e3ca795f 100644 --- a/examples/changestream.php +++ b/examples/changestream.php @@ -3,11 +3,10 @@ namespace MongoDB\Examples; -require '../vendor/autoload.php'; - use MongoDB\Client; use function assert; +use function dirname; use function fprintf; use function getenv; use function is_object; @@ -18,6 +17,8 @@ use const STDERR; +require dirname(__FILE__) . '/../vendor/autoload.php'; + function toJSON(object $document): string { return toRelaxedExtendedJSON(fromPHP($document)); diff --git a/examples/command_logger.php b/examples/command_logger.php index b987f6e19..52c353f5b 100644 --- a/examples/command_logger.php +++ b/examples/command_logger.php @@ -3,8 +3,6 @@ namespace MongoDB\Examples; -require '../vendor/autoload.php'; - use MongoDB\Client; use MongoDB\Driver\Monitoring\CommandFailedEvent; use MongoDB\Driver\Monitoring\CommandStartedEvent; @@ -12,6 +10,7 @@ use MongoDB\Driver\Monitoring\CommandSucceededEvent; use function assert; +use function dirname; use function fprintf; use function get_class; use function getenv; @@ -22,6 +21,8 @@ use const STDERR; +require dirname(__FILE__) . '/../vendor/autoload.php'; + function toJSON(object $document): string { return toRelaxedExtendedJSON(fromPHP($document)); From 498da74e727839e78c61a2e3a9a7090724fc1cd0 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 13:35:47 +0200 Subject: [PATCH 02/12] Add examples for bulk writes and with_transaction --- examples/bulk.php | 79 +++++++++++++++++++++++++++++++++++ examples/with_transaction.php | 56 +++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 examples/bulk.php create mode 100644 examples/with_transaction.php diff --git a/examples/bulk.php b/examples/bulk.php new file mode 100644 index 000000000..db7b3d790 --- /dev/null +++ b/examples/bulk.php @@ -0,0 +1,79 @@ +test->coll; +$collection->drop(); + +$documents = []; + +for ($i = 0; $i < 10; $i++) { + $documents[] = ['x' => $i]; +} + +$collection->insertMany($documents); + +$collection->bulkWrite( + [ + [ + 'deleteMany' => [ + ['x' => ['$gt' => 7]], // Filter + ], + ], + [ + 'deleteOne' => [ + ['x' => 4], // Filter + ], + ], + [ + 'replaceOne' => [ + ['x' => 1], // Filter + ['y' => 1], // Replacement + ], + ], + [ + 'updateMany' => [ + ['x' => ['$gt' => 5]], // Filter + ['$set' => ['updateMany' => true]], // Update + ], + ], + [ + 'updateOne' => [ + ['x' => 2], // Filter + ['$set' => ['y' => 2]], // Update + ], + ], + [ + 'insertOne' => [ + ['x' => 10], // Document + ], + ], + ] +); + +$cursor = $collection->find([], ['batchSize' => 2]); + +foreach ($cursor as $document) { + assert(is_object($document)); + printf("%s\n", toJSON($document)); +} diff --git a/examples/with_transaction.php b/examples/with_transaction.php new file mode 100644 index 000000000..7b773cf6a --- /dev/null +++ b/examples/with_transaction.php @@ -0,0 +1,56 @@ +test->coll; +$collection->drop(); + +$insertData = function (Session $session) use ($collection): void { + $collection->insertMany( + [ + ['x' => 1], + ['x' => 2], + ['x' => 3], + ], + ['session' => $session] + ); + + $collection->updateMany( + ['x' => ['$gt' => 1]], + ['$set' => ['y' => 1]], + ['session' => $session] + ); +}; + +$session = $client->startSession(); + +with_transaction($session, $insertData); + +$cursor = $collection->find([], ['batchSize' => 2]); + +foreach ($cursor as $document) { + assert(is_object($document)); + printf("%s\n", toJSON($document)); +} From 17ccb2f6e672440ded6876ec1c3682c8ba84b946 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 13:42:03 +0200 Subject: [PATCH 03/12] Add aggregation example --- examples/aggregate.php | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 examples/aggregate.php diff --git a/examples/aggregate.php b/examples/aggregate.php new file mode 100644 index 000000000..c6c6b3d0e --- /dev/null +++ b/examples/aggregate.php @@ -0,0 +1,59 @@ +test->coll; +$collection->drop(); + +$documents = []; + +for ($i = 0; $i < 100; $i++) { + $documents[] = ['randomValue' => rand(0, 1000)]; +} + +$collection->insertMany($documents); + +$pipeline = [ + [ + '$group' => [ + '_id' => null, + 'totalCount' => ['$sum' => 1], + 'evenCount' => [ + '$sum' => ['$mod' => ['$randomValue', 2]], + ], + 'oddCount' => [ + '$sum' => ['$subtract' => [1, ['$mod' => ['$randomValue', 2]]]], + ], + 'maxValue' => ['$max' => '$randomValue'], + 'minValue' => ['$min' => '$randomValue'], + ], + ], +]; + +$cursor = $collection->aggregate($pipeline, ['batchSize' => 2]); + +foreach ($cursor as $document) { + assert(is_object($document)); + printf("%s\n", toJSON($document)); +} From eee1ab3cc143f9696bb3b436f92c8228d31c65bf Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 13:57:05 +0200 Subject: [PATCH 04/12] Add example for persistable objects --- examples/persistable.php | 139 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 examples/persistable.php diff --git a/examples/persistable.php b/examples/persistable.php new file mode 100644 index 000000000..aa8c6effe --- /dev/null +++ b/examples/persistable.php @@ -0,0 +1,139 @@ + */ + private $emails = []; + + public function __construct(string $name) + { + $this->id = new ObjectId(); + $this->name = $name; + } + + public function getId(): ObjectId + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getEmails(): array + { + return $this->emails; + } + + public function addEmail(Email $email): void + { + $this->emails[] = $email; + } + + public function deleteEmail(Email $email): void + { + $index = array_search($email, $this->emails, true); + if ($index === false) { + return; + } + + unset($this->emails[$index]); + } + + public function bsonSerialize(): stdClass + { + return (object) [ + '_id' => $this->id, + 'name' => $this->name, + 'emails' => $this->emails, + ]; + } + + public function bsonUnserialize(array $data): void + { + $this->id = $data['_id']; + $this->name = (string) $data['name']; + $this->emails = $data['emails']->getArrayCopy(); // Emails will be passed as a BSONArray instance + } +} + +class Email implements Persistable +{ + /** @var string */ + private $type; + + /** @var string */ + private $address; + + public function __construct(string $type, string $address) + { + $this->type = $type; + $this->address = $address; + } + + public function getType(): string + { + return $this->type; + } + + public function getAddress(): string + { + return $this->address; + } + + public function bsonSerialize(): stdClass + { + return (object) [ + 'type' => $this->type, + 'address' => $this->address, + ]; + } + + public function bsonUnserialize(array $data): void + { + $this->type = (string) $data['type']; + $this->address = (string) $data['address']; + } +} + +$entry = new Entry('alcaeus'); +$entry->addEmail(new Email('work', 'alcaeus@example.com')); +$entry->addEmail(new Email('private', 'secret@example.com')); + +$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); + +$collection = $client->test->coll; +$collection->drop(); + +$collection->insertOne($entry); + +$foundEntry = $collection->findOne([]); + +var_dump($foundEntry); From b47a93e81e9e7472ab63e0b47a7bb2746b7a3a3a Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 14:03:15 +0200 Subject: [PATCH 05/12] Ignore certain phpcs errors for all examples --- examples/command_logger.php | 1 - examples/persistable.php | 34 +++++++++++++++++++++++----------- phpcs.xml.dist | 4 ++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/command_logger.php b/examples/command_logger.php index 52c353f5b..aed14a94b 100644 --- a/examples/command_logger.php +++ b/examples/command_logger.php @@ -28,7 +28,6 @@ function toJSON(object $document): string return toRelaxedExtendedJSON(fromPHP($document)); } -// phpcs:disable Squiz.Classes.ClassFileName.NoMatch class CommandLogger implements CommandSubscriber { public function commandStarted(CommandStartedEvent $event): void diff --git a/examples/persistable.php b/examples/persistable.php index aa8c6effe..867e98788 100644 --- a/examples/persistable.php +++ b/examples/persistable.php @@ -6,7 +6,8 @@ use MongoDB\BSON\ObjectId; use MongoDB\BSON\Persistable; use MongoDB\Client; -use stdClass; +use MongoDB\Model\BSONArray; +use UnexpectedValueException; use function array_search; use function dirname; @@ -15,7 +16,7 @@ require dirname(__FILE__) . '/../vendor/autoload.php'; -class Entry implements Persistable +class PersistableEntry implements Persistable { /** @var ObjectId */ private $id; @@ -23,7 +24,7 @@ class Entry implements Persistable /** @var string */ private $name; - /** @var array */ + /** @var array */ private $emails = []; public function __construct(string $name) @@ -52,12 +53,12 @@ public function getEmails(): array return $this->emails; } - public function addEmail(Email $email): void + public function addEmail(PersistableEmail $email): void { $this->emails[] = $email; } - public function deleteEmail(Email $email): void + public function deleteEmail(PersistableEmail $email): void { $index = array_search($email, $this->emails, true); if ($index === false) { @@ -67,7 +68,7 @@ public function deleteEmail(Email $email): void unset($this->emails[$index]); } - public function bsonSerialize(): stdClass + public function bsonSerialize(): object { return (object) [ '_id' => $this->id, @@ -78,13 +79,23 @@ public function bsonSerialize(): stdClass public function bsonUnserialize(array $data): void { + if (! $data['_id'] instanceof ObjectId) { + throw new UnexpectedValueException('_id field is not of the expected type'); + } + + if (! $data['emails'] instanceof BSONArray) { + throw new UnexpectedValueException('emails field is not of the expected type'); + } + $this->id = $data['_id']; $this->name = (string) $data['name']; + + /** @psalm-suppress MixedPropertyTypeCoercion */ $this->emails = $data['emails']->getArrayCopy(); // Emails will be passed as a BSONArray instance } } -class Email implements Persistable +class PersistableEmail implements Persistable { /** @var string */ private $type; @@ -108,7 +119,7 @@ public function getAddress(): string return $this->address; } - public function bsonSerialize(): stdClass + public function bsonSerialize(): object { return (object) [ 'type' => $this->type, @@ -123,9 +134,9 @@ public function bsonUnserialize(array $data): void } } -$entry = new Entry('alcaeus'); -$entry->addEmail(new Email('work', 'alcaeus@example.com')); -$entry->addEmail(new Email('private', 'secret@example.com')); +$entry = new PersistableEntry('alcaeus'); +$entry->addEmail(new PersistableEmail('work', 'alcaeus@example.com')); +$entry->addEmail(new PersistableEmail('private', 'secret@example.com')); $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); @@ -136,4 +147,5 @@ public function bsonUnserialize(array $data): void $foundEntry = $collection->findOne([]); +/** @psalm-suppress ForbiddenCode */ var_dump($foundEntry); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 546988605..7db7074b7 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -176,6 +176,10 @@ /tests/GridFS/UnusableStream.php + /examples /tests/PHPUnit/ConstraintTrait.php + + /examples + From 9aeb3b080d54ba641e0bb55f49234692c86ed7f7 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 15:24:42 +0200 Subject: [PATCH 06/12] Add example for deserialisation using typemap --- examples/typemap.php | 121 +++++++++++++++++++++++++++++++++++++++++++ psalm-baseline.xml | 9 ++++ 2 files changed, 130 insertions(+) create mode 100644 examples/typemap.php diff --git a/examples/typemap.php b/examples/typemap.php new file mode 100644 index 000000000..e8f8e80fb --- /dev/null +++ b/examples/typemap.php @@ -0,0 +1,121 @@ + */ + private $emails; + + private function __construct() + { + } + + public function getId(): ObjectId + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getEmails(): array + { + return $this->emails; + } + + public function bsonUnserialize(array $data): void + { + if (! $data['_id'] instanceof ObjectId) { + throw new UnexpectedValueException('_id field is not of the expected type'); + } + + if (! is_array($data['emails'])) { + throw new UnexpectedValueException('emails field is not of the expected type'); + } + + $this->id = $data['_id']; + $this->name = (string) $data['name']; + + /** @psalm-suppress MixedPropertyTypeCoercion */ + $this->emails = $data['emails']; + } +} + +class TypemapEmail implements Unserializable +{ + /** @var string */ + private $type; + + /** @var string */ + private $address; + + private function __construct() + { + } + + public function getType(): string + { + return $this->type; + } + + public function getAddress(): string + { + return $this->address; + } + + public function bsonUnserialize(array $data): void + { + $this->type = (string) $data['type']; + $this->address = (string) $data['address']; + } +} + +$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); + +$collection = $client->test->coll; +$collection->drop(); + +$document = [ + 'name' => 'alcaeus', + 'emails' => [ + ['type' => 'work', 'address' => 'alcaeus@example.com'], + ['type' => 'private', 'address' => 'secret@example.com'], + ], +]; + +$collection->insertOne($document); + +$typeMap = [ + 'root' => TypemapEntry::class, // Root object will be an Entry instance + 'fieldPaths' => [ + 'emails' => 'array', // Emails field is used as PHP array + 'emails.$' => TypemapEmail::class, // Each element in the emails array will be an Email instance + ], +]; + +$entry = $collection->findOne([], ['typeMap' => $typeMap]); + +/** @psalm-suppress ForbiddenCode */ +var_dump($entry); diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 952117897..179ce8c12 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,14 @@ + + + $address + $emails + $id + $name + $type + + $driverOptions['driver'] ?? [] From 3654358a11923fe99c01d7a9cd5be88c23414345 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 16:29:34 +0200 Subject: [PATCH 07/12] Remove unnecessary batchSize option --- examples/aggregate.php | 2 +- examples/bulk.php | 2 +- examples/with_transaction.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/aggregate.php b/examples/aggregate.php index c6c6b3d0e..5edb7cdbc 100644 --- a/examples/aggregate.php +++ b/examples/aggregate.php @@ -51,7 +51,7 @@ function toJSON(object $document): string ], ]; -$cursor = $collection->aggregate($pipeline, ['batchSize' => 2]); +$cursor = $collection->aggregate($pipeline); foreach ($cursor as $document) { assert(is_object($document)); diff --git a/examples/bulk.php b/examples/bulk.php index db7b3d790..134e096f3 100644 --- a/examples/bulk.php +++ b/examples/bulk.php @@ -71,7 +71,7 @@ function toJSON(object $document): string ] ); -$cursor = $collection->find([], ['batchSize' => 2]); +$cursor = $collection->find([]); foreach ($cursor as $document) { assert(is_object($document)); diff --git a/examples/with_transaction.php b/examples/with_transaction.php index 7b773cf6a..4a35abc1c 100644 --- a/examples/with_transaction.php +++ b/examples/with_transaction.php @@ -48,7 +48,7 @@ function toJSON(object $document): string with_transaction($session, $insertData); -$cursor = $collection->find([], ['batchSize' => 2]); +$cursor = $collection->find([]); foreach ($cursor as $document) { assert(is_object($document)); From 7ad01fa76e67a8a327acad58589747b3c7104a63 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 28 Sep 2022 16:30:04 +0200 Subject: [PATCH 08/12] Use __DIR__ instead of dirname(__FILE__) --- examples/aggregate.php | 3 +-- examples/bulk.php | 3 +-- examples/changestream.php | 3 +-- examples/command_logger.php | 3 +-- examples/persistable.php | 3 +-- examples/typemap.php | 3 +-- examples/with_transaction.php | 3 +-- 7 files changed, 7 insertions(+), 14 deletions(-) diff --git a/examples/aggregate.php b/examples/aggregate.php index 5edb7cdbc..9a4873593 100644 --- a/examples/aggregate.php +++ b/examples/aggregate.php @@ -6,7 +6,6 @@ use MongoDB\Client; use function assert; -use function dirname; use function getenv; use function is_object; use function MongoDB\BSON\fromPHP; @@ -14,7 +13,7 @@ use function printf; use function rand; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; function toJSON(object $document): string { diff --git a/examples/bulk.php b/examples/bulk.php index 134e096f3..1af53f755 100644 --- a/examples/bulk.php +++ b/examples/bulk.php @@ -6,14 +6,13 @@ use MongoDB\Client; use function assert; -use function dirname; use function getenv; use function is_object; use function MongoDB\BSON\fromPHP; use function MongoDB\BSON\toRelaxedExtendedJSON; use function printf; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; function toJSON(object $document): string { diff --git a/examples/changestream.php b/examples/changestream.php index 5e3ca795f..6ab130d58 100644 --- a/examples/changestream.php +++ b/examples/changestream.php @@ -6,7 +6,6 @@ use MongoDB\Client; use function assert; -use function dirname; use function fprintf; use function getenv; use function is_object; @@ -17,7 +16,7 @@ use const STDERR; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; function toJSON(object $document): string { diff --git a/examples/command_logger.php b/examples/command_logger.php index aed14a94b..7eab169bf 100644 --- a/examples/command_logger.php +++ b/examples/command_logger.php @@ -10,7 +10,6 @@ use MongoDB\Driver\Monitoring\CommandSucceededEvent; use function assert; -use function dirname; use function fprintf; use function get_class; use function getenv; @@ -21,7 +20,7 @@ use const STDERR; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; function toJSON(object $document): string { diff --git a/examples/persistable.php b/examples/persistable.php index 867e98788..fb4a3815d 100644 --- a/examples/persistable.php +++ b/examples/persistable.php @@ -10,11 +10,10 @@ use UnexpectedValueException; use function array_search; -use function dirname; use function getenv; use function var_dump; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; class PersistableEntry implements Persistable { diff --git a/examples/typemap.php b/examples/typemap.php index e8f8e80fb..74c12801e 100644 --- a/examples/typemap.php +++ b/examples/typemap.php @@ -8,12 +8,11 @@ use MongoDB\Client; use UnexpectedValueException; -use function dirname; use function getenv; use function is_array; use function var_dump; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; class TypemapEntry implements Unserializable { diff --git a/examples/with_transaction.php b/examples/with_transaction.php index 4a35abc1c..982bd5144 100644 --- a/examples/with_transaction.php +++ b/examples/with_transaction.php @@ -7,7 +7,6 @@ use MongoDB\Driver\Session; use function assert; -use function dirname; use function getenv; use function is_object; use function MongoDB\BSON\fromPHP; @@ -15,7 +14,7 @@ use function MongoDB\with_transaction; use function printf; -require dirname(__FILE__) . '/../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; function toJSON(object $document): string { From 334bfeeaa3d3c9e26482898daac4a7a2b993144f Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 29 Sep 2022 10:24:14 +0200 Subject: [PATCH 09/12] Fix capitalisation for TypeMap --- examples/typemap.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/typemap.php b/examples/typemap.php index 74c12801e..02d222e01 100644 --- a/examples/typemap.php +++ b/examples/typemap.php @@ -14,7 +14,7 @@ require __DIR__ . '/../vendor/autoload.php'; -class TypemapEntry implements Unserializable +class TypeMapEntry implements Unserializable { /** @var ObjectId */ private $id; @@ -22,7 +22,7 @@ class TypemapEntry implements Unserializable /** @var string */ private $name; - /** @var array */ + /** @var array */ private $emails; private function __construct() @@ -62,7 +62,7 @@ public function bsonUnserialize(array $data): void } } -class TypemapEmail implements Unserializable +class TypeMapEmail implements Unserializable { /** @var string */ private $type; @@ -107,10 +107,10 @@ public function bsonUnserialize(array $data): void $collection->insertOne($document); $typeMap = [ - 'root' => TypemapEntry::class, // Root object will be an Entry instance + 'root' => TypeMapEntry::class, // Root object will be an Entry instance 'fieldPaths' => [ 'emails' => 'array', // Emails field is used as PHP array - 'emails.$' => TypemapEmail::class, // Each element in the emails array will be an Email instance + 'emails.$' => TypeMapEmail::class, // Each element in the emails array will be an Email instance ], ]; From 168225be3b5faccb0525c6ea88608e6aa48fe9e5 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 29 Sep 2022 10:25:55 +0200 Subject: [PATCH 10/12] Simplify persistable example models --- examples/persistable.php | 53 +++++----------------------------------- 1 file changed, 6 insertions(+), 47 deletions(-) diff --git a/examples/persistable.php b/examples/persistable.php index fb4a3815d..f41cc54b3 100644 --- a/examples/persistable.php +++ b/examples/persistable.php @@ -9,7 +9,6 @@ use MongoDB\Model\BSONArray; use UnexpectedValueException; -use function array_search; use function getenv; use function var_dump; @@ -21,10 +20,10 @@ class PersistableEntry implements Persistable private $id; /** @var string */ - private $name; + public $name; /** @var array */ - private $emails = []; + public $emails = []; public function __construct(string $name) { @@ -37,36 +36,6 @@ public function getId(): ObjectId return $this->id; } - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): void - { - $this->name = $name; - } - - public function getEmails(): array - { - return $this->emails; - } - - public function addEmail(PersistableEmail $email): void - { - $this->emails[] = $email; - } - - public function deleteEmail(PersistableEmail $email): void - { - $index = array_search($email, $this->emails, true); - if ($index === false) { - return; - } - - unset($this->emails[$index]); - } - public function bsonSerialize(): object { return (object) [ @@ -97,10 +66,10 @@ public function bsonUnserialize(array $data): void class PersistableEmail implements Persistable { /** @var string */ - private $type; + public $type; /** @var string */ - private $address; + public $address; public function __construct(string $type, string $address) { @@ -108,16 +77,6 @@ public function __construct(string $type, string $address) $this->address = $address; } - public function getType(): string - { - return $this->type; - } - - public function getAddress(): string - { - return $this->address; - } - public function bsonSerialize(): object { return (object) [ @@ -134,8 +93,8 @@ public function bsonUnserialize(array $data): void } $entry = new PersistableEntry('alcaeus'); -$entry->addEmail(new PersistableEmail('work', 'alcaeus@example.com')); -$entry->addEmail(new PersistableEmail('private', 'secret@example.com')); +$entry->emails[] = new PersistableEmail('work', 'alcaeus@example.com'); +$entry->emails[] = new PersistableEmail('private', 'secret@example.com'); $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); From 2f2d180c8029a6d33a869895350b50b3ace11389 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 29 Sep 2022 10:27:07 +0200 Subject: [PATCH 11/12] Add note about replica set requirements --- examples/changestream.php | 1 + examples/with_transaction.php | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/changestream.php b/examples/changestream.php index 6ab130d58..b54be8ff2 100644 --- a/examples/changestream.php +++ b/examples/changestream.php @@ -23,6 +23,7 @@ function toJSON(object $document): string return toRelaxedExtendedJSON(fromPHP($document)); } +// Change streams require a replica set or sharded cluster $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); $collection = $client->test->coll; diff --git a/examples/with_transaction.php b/examples/with_transaction.php index 982bd5144..a30a9ab2b 100644 --- a/examples/with_transaction.php +++ b/examples/with_transaction.php @@ -21,6 +21,7 @@ function toJSON(object $document): string return toRelaxedExtendedJSON(fromPHP($document)); } +// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2) $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); $collection = $client->test->coll; From fb314f5892cea59d2b98d7eef5519a7b5a48e36a Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 29 Sep 2022 10:41:58 +0200 Subject: [PATCH 12/12] Add note about examples to the documentation --- docs/index.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/index.txt b/docs/index.txt index 20045f524..77890982c 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -37,6 +37,8 @@ following pages should help you get started: - :doc:`/reference/bson` +Code examples can be found in the ``examples`` directory in the source code. + If you have previously worked with the legacy ``mongo`` extension, it will be helpful to review the :doc:`/upgrade` for a summary of API changes between the old driver and this library.