Skip to content

Commit 37aeae4

Browse files
authored
Merge pull request #91 from i80and/DOP-4884
DOP-4884: Inline the mongodb/mongo-php-library docs submodule into this repository
2 parents 0b3760d + c448f82 commit 37aeae4

File tree

242 files changed

+22812
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

242 files changed

+22812
-5
lines changed

.gitmodules

Lines changed: 0 additions & 4 deletions
This file was deleted.

mongo-php-library

Lines changed: 0 additions & 1 deletion
This file was deleted.

source/.static/.mongodb

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "mongodb/bref-tutorial",
3+
"type": "project",
4+
"license": "MIT",
5+
"repositories": [
6+
{
7+
"type": "path",
8+
"url": "../../..",
9+
"options": {
10+
"symlink": false
11+
}
12+
}
13+
],
14+
"require": {
15+
"bref/bref": "^2.1",
16+
"bref/extra-php-extensions": "^1.4",
17+
"mongodb/mongodb": "@dev"
18+
}
19+
}

source/examples/aws-lambda/index.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use MongoDB\Client;
4+
5+
require_once __DIR__ . '/vendor/autoload.php';
6+
7+
$uri = getenv('MONGODB_URI');
8+
9+
try {
10+
$client = new Client($uri);
11+
$planets = $client
12+
->selectCollection('sample_guides', 'planets')
13+
->find([], ['sort' => ['orderFromSun' => 1]]);
14+
} catch (Throwable $exception) {
15+
exit($exception->getMessage());
16+
}
17+
18+
?>
19+
<!DOCTYPE html>
20+
<html lang="en">
21+
<head>
22+
<title>MongoDB Planets</title>
23+
</head>
24+
<body>
25+
<ul>
26+
<?php foreach ($planets as $planet) : ?>
27+
<li><?= $planet->name ?></li>
28+
<?php endforeach ?>
29+
</ul>
30+
</body>
31+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
service: app
2+
3+
provider:
4+
name: aws
5+
region: us-east-1
6+
environment:
7+
MONGODB_URI: ${env:MONGODB_URI}
8+
9+
plugins:
10+
- ./vendor/bref/bref
11+
- ./vendor/bref/extra-php-extensions
12+
13+
functions:
14+
api:
15+
handler: index.php
16+
description: ''
17+
runtime: php-83-fpm
18+
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
19+
events:
20+
- httpApi: '*'
21+
layers:
22+
- ${bref-extra:mongodb-php-83}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use MongoDB\BSON\Document;
4+
use MongoDB\BSON\UTCDateTime;
5+
use MongoDB\Codec\Codec;
6+
use MongoDB\Codec\DecodeIfSupported;
7+
use MongoDB\Codec\EncodeIfSupported;
8+
use MongoDB\Exception\UnsupportedValueException;
9+
10+
/** @template-implements Codec<Document, DateTimeImmutable> */
11+
final class DateTimeCodec implements Codec
12+
{
13+
use DecodeIfSupported;
14+
use EncodeIfSupported;
15+
16+
public function canDecode(mixed $value): bool
17+
{
18+
/* This codec inspects the BSON document to ensure it has the fields it expects, and that those fields are of
19+
* the correct type. This is a robust approach to avoid decoding document that are not supported and would cause
20+
* exceptions.
21+
*
22+
* For large documents, this can be inefficient as we're inspecting the entire document four times (once for
23+
* each call to has() and get()). For small documents, this is not a problem.
24+
*/
25+
return $value instanceof Document
26+
&& $value->has('utc') && $value->get('utc') instanceof UTCDateTime
27+
&& $value->has('tz') && is_string($value->get('tz'));
28+
}
29+
30+
public function canEncode(mixed $value): bool
31+
{
32+
return $value instanceof DateTimeInterface;
33+
}
34+
35+
public function decode(mixed $value): DateTimeImmutable
36+
{
37+
if (! $this->canDecode($value)) {
38+
throw UnsupportedValueException::invalidDecodableValue($value);
39+
}
40+
41+
$timeZone = new DateTimeZone($value->get('tz'));
42+
$dateTime = $value->get('utc')
43+
->toDateTime()
44+
->setTimeZone($timeZone);
45+
46+
return DateTimeImmutable::createFromMutable($dateTime);
47+
}
48+
49+
public function encode(mixed $value): Document
50+
{
51+
if (! $this->canEncode($value)) {
52+
throw UnsupportedValueException::invalidEncodableValue($value);
53+
}
54+
55+
return Document::fromPHP([
56+
'utc' => new UTCDateTime($value),
57+
'tz' => $value->getTimezone()->getName(),
58+
]);
59+
}
60+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use MongoDB\BSON\ObjectId;
4+
5+
final class Person
6+
{
7+
public function __construct(
8+
public string $name,
9+
public readonly DateTimeImmutable $createdAt = new DateTimeImmutable(),
10+
public readonly ObjectId $id = new ObjectId(),
11+
) {
12+
}
13+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use MongoDB\BSON\Document;
4+
use MongoDB\Codec\DecodeIfSupported;
5+
use MongoDB\Codec\DocumentCodec;
6+
use MongoDB\Codec\EncodeIfSupported;
7+
use MongoDB\Exception\UnsupportedValueException;
8+
9+
/** @template-implements DocumentCodec<Person> */
10+
final class PersonCodec implements DocumentCodec
11+
{
12+
use DecodeIfSupported;
13+
use EncodeIfSupported;
14+
15+
public function __construct(
16+
private readonly DateTimeCodec $dateTimeCodec = new DateTimeCodec(),
17+
) {
18+
}
19+
20+
public function canDecode(mixed $value): bool
21+
{
22+
return $value instanceof Document && $value->has('name');
23+
}
24+
25+
public function canEncode(mixed $value): bool
26+
{
27+
return $value instanceof Person;
28+
}
29+
30+
public function decode(mixed $value): Person
31+
{
32+
if (! $this->canDecode($value)) {
33+
throw UnsupportedValueException::invalidDecodableValue($value);
34+
}
35+
36+
return new Person(
37+
$value->get('name'),
38+
$this->dateTimeCodec->decode($value->get('createdAt')),
39+
$value->get('_id'),
40+
);
41+
}
42+
43+
public function encode(mixed $value): Document
44+
{
45+
if (! $this->canEncode($value)) {
46+
throw UnsupportedValueException::invalidEncodableValue($value);
47+
}
48+
49+
return Document::fromPHP([
50+
'_id' => $value->id,
51+
'name' => $value->name,
52+
'createdAt' => $this->dateTimeCodec->encode($value->createdAt),
53+
]);
54+
}
55+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use MongoDB\BSON\ObjectId;
4+
5+
final class Person
6+
{
7+
public function __construct(
8+
public string $name,
9+
public readonly ObjectId $id = new ObjectId(),
10+
) {
11+
}
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use MongoDB\BSON\Document;
4+
use MongoDB\Codec\DecodeIfSupported;
5+
use MongoDB\Codec\DocumentCodec;
6+
use MongoDB\Codec\EncodeIfSupported;
7+
use MongoDB\Exception\UnsupportedValueException;
8+
9+
/** @template-implements DocumentCodec<Person> */
10+
final class PersonCodec implements DocumentCodec
11+
{
12+
// These traits define commonly used functionality to avoid duplication
13+
use DecodeIfSupported;
14+
use EncodeIfSupported;
15+
16+
public function canDecode(mixed $value): bool
17+
{
18+
return $value instanceof Document && $value->has('name');
19+
}
20+
21+
public function canEncode(mixed $value): bool
22+
{
23+
return $value instanceof Person;
24+
}
25+
26+
public function decode(mixed $value): Person
27+
{
28+
if (! $this->canDecode($value)) {
29+
throw UnsupportedValueException::invalidDecodableValue($value);
30+
}
31+
32+
return new Person(
33+
$value->get('name'),
34+
$value->get('_id'),
35+
);
36+
}
37+
38+
public function encode(mixed $value): Document
39+
{
40+
if (! $this->canEncode($value)) {
41+
throw UnsupportedValueException::invalidEncodableValue($value);
42+
}
43+
44+
return Document::fromPHP([
45+
'_id' => $value->id,
46+
'name' => $value->name,
47+
]);
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Overrides the collection codec, falling back to the default type map
4+
$collection->aggregate($pipeline, ['codec' => null]);
5+
6+
// Overrides the collection codec, using the specified type map
7+
$collection->findOne($filter, ['typeMap' => ['root' => 'stdClass']]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use MongoDB\Client;
4+
5+
$client = new Client();
6+
$collection = $client->selectCollection('test', 'person', [
7+
'codec' => new PersonCodec(),
8+
]);
9+
10+
$person = new Person('Jane Doe');
11+
$collection->insertOne($person);
12+
13+
$person = $collection->findOne();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
final readonly class Address
4+
{
5+
public function __construct(
6+
public string $street,
7+
public string $postCode,
8+
public string $city,
9+
public string $country,
10+
) {
11+
}
12+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use MongoDB\BSON\Document;
4+
use MongoDB\Codec\DecodeIfSupported;
5+
use MongoDB\Codec\DocumentCodec;
6+
use MongoDB\Codec\EncodeIfSupported;
7+
use MongoDB\Exception\UnsupportedValueException;
8+
9+
/** @template-implements DocumentCodec<Address> */
10+
final class AddressCodec implements DocumentCodec
11+
{
12+
use DecodeIfSupported;
13+
use EncodeIfSupported;
14+
15+
public function canDecode(mixed $value): bool
16+
{
17+
return $value instanceof Document
18+
&& $value->has('street')
19+
&& $value->has('postCode')
20+
&& $value->has('city')
21+
&& $value->has('country');
22+
}
23+
24+
public function canEncode(mixed $value): bool
25+
{
26+
return $value instanceof Address;
27+
}
28+
29+
public function decode(mixed $value): Address
30+
{
31+
if (! $this->canDecode($value)) {
32+
throw UnsupportedValueException::invalidDecodableValue($value);
33+
}
34+
35+
return new Address(
36+
$value->get('street'),
37+
$value->get('postCode'),
38+
$value->get('city'),
39+
$value->get('country'),
40+
);
41+
}
42+
43+
public function encode(mixed $value): Document
44+
{
45+
if (! $this->canEncode($value)) {
46+
throw UnsupportedValueException::invalidEncodableValue($value);
47+
}
48+
49+
return Document::fromPHP([
50+
'street' => $value->street,
51+
'postCode' => $value->postCode,
52+
'city' => $value->city,
53+
'country' => $value->country,
54+
]);
55+
}
56+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use MongoDB\BSON\ObjectId;
4+
5+
final class Person
6+
{
7+
public ?Address $address = null;
8+
9+
public function __construct(
10+
public string $name,
11+
public readonly ObjectId $id = new ObjectId()
12+
) {
13+
}
14+
}

0 commit comments

Comments
 (0)