Skip to content

Commit 9c65561

Browse files
committed
PHPLIB-233: Add examples for each GridFS API method
1 parent b9f3591 commit 9c65561

19 files changed

+422
-21
lines changed

docs/reference/method/MongoDBGridFSBucket-delete.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ Behavior
3737
If the files collection document is not found, this method will still attempt to
3838
delete orphaned chunks.
3939

40-
.. todo: add examples
40+
Examples
41+
--------
42+
43+
.. code-block:: php
44+
45+
<?php
46+
47+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
48+
49+
$stream = fopen('php://temp', 'w+b');
50+
fwrite($stream, "foobar");
51+
rewind($stream);
52+
53+
$id = $bucket->uploadFromStream('filename', $stream);
54+
55+
$bucket->delete($id);

docs/reference/method/MongoDBGridFSBucket-downloadToStream.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,38 @@ Definition
2626

2727
.. include:: /includes/apiargs/MongoDBGridFSBucket-method-downloadToStream-param.rst
2828

29-
.. todo: add examples
30-
3129
Errors/Exceptions
3230
-----------------
3331

3432
.. include:: /includes/extracts/error-gridfs-filenotfoundexception.rst
3533
.. include:: /includes/extracts/error-invalidargumentexception.rst
3634
.. include:: /includes/extracts/error-driver-runtimeexception.rst
3735

36+
Examples
37+
--------
38+
39+
.. code-block:: php
40+
41+
<?php
42+
43+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
44+
45+
$stream = fopen('php://temp', 'w+b');
46+
fwrite($stream, "foobar");
47+
rewind($stream);
48+
49+
$id = $bucket->uploadFromStream('filename', $stream);
50+
51+
$destination = fopen('php://temp', 'w+b');
52+
53+
$bucket->downloadToStream($id, $destination);
54+
55+
var_dump(stream_get_contents($destination, -1, 0));
56+
57+
The output would then resemble::
58+
59+
string(6) "foobar"
60+
3861
See Also
3962
--------
4063

docs/reference/method/MongoDBGridFSBucket-downloadToStreamByName.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,38 @@ Definition
3030

3131
.. include:: /includes/apiargs/MongoDBGridFSBucket-method-downloadToStreamByName-option.rst
3232

33-
.. todo: add examples
34-
3533
Errors/Exceptions
3634
-----------------
3735

3836
.. include:: /includes/extracts/error-gridfs-filenotfoundexception.rst
3937
.. include:: /includes/extracts/error-invalidargumentexception.rst
4038
.. include:: /includes/extracts/error-driver-runtimeexception.rst
4139

40+
Examples
41+
--------
42+
43+
.. code-block:: php
44+
45+
<?php
46+
47+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
48+
49+
$stream = fopen('php://temp', 'w+b');
50+
fwrite($stream, "foobar");
51+
rewind($stream);
52+
53+
$bucket->uploadFromStream('filename', $stream);
54+
55+
$destination = fopen('php://temp', 'w+b');
56+
57+
$bucket->downloadToStreamByName('filename', $destination);
58+
59+
var_dump(stream_get_contents($destination, -1, 0));
60+
61+
The output would then resemble::
62+
63+
string(6) "foobar"
64+
4265
See Also
4366
--------
4467

docs/reference/method/MongoDBGridFSBucket-drop.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ Errors/Exceptions
2626

2727
.. include:: /includes/extracts/error-driver-runtimeexception.rst
2828

29-
.. todo: add examples
29+
Examples
30+
--------
31+
32+
.. code-block:: php
33+
34+
<?php
35+
36+
$database = (new MongoDB\Client)->test;
37+
38+
$bucket = $database->selectGridFSBucket();
39+
40+
$stream = fopen('php://temp', 'w+b');
41+
fwrite($stream, "foobar");
42+
rewind($stream);
43+
44+
$bucket->uploadFromStream('filename', $stream);
45+
46+
$bucket->drop();

docs/reference/method/MongoDBGridFSBucket-find.txt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,49 @@ Behavior
4646

4747
.. include:: /includes/extracts/note-bson-comparison.rst
4848

49-
.. todo: add examples
49+
Examples
50+
--------
51+
52+
.. code-block:: php
53+
54+
<?php
55+
56+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
57+
58+
$stream = fopen('php://temp', 'w+b');
59+
fwrite($stream, "foobar");
60+
rewind($stream);
61+
62+
$bucket->uploadFromStream('b', $stream);
63+
64+
$cursor = $bucket->find(
65+
['length' => ['$lte' => 6]],
66+
[
67+
'projection' => [
68+
'filename' => 1,
69+
'length' => 1,
70+
'_id' => 0,
71+
],
72+
'sort' => ['length' => -1],
73+
]
74+
);
75+
76+
var_dump($cursor->toArray());
77+
78+
The output would then resemble::
79+
80+
array(1) {
81+
[0]=>
82+
object(MongoDB\Model\BSONDocument)#3015 (1) {
83+
["storage":"ArrayObject":private]=>
84+
array(2) {
85+
["filename"]=>
86+
string(1) "b"
87+
["length"]=>
88+
int(6)
89+
}
90+
}
91+
}
5092

5193
See Also
5294
--------

docs/reference/method/MongoDBGridFSBucket-findOne.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,46 @@ Behavior
4949

5050
.. include:: /includes/extracts/note-bson-comparison.rst
5151

52-
.. todo: add examples
52+
Examples
53+
--------
54+
55+
.. code-block:: php
56+
57+
<?php
58+
59+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
60+
61+
$stream = fopen('php://temp', 'w+b');
62+
fwrite($stream, "foobar");
63+
rewind($stream);
64+
65+
$bucket->uploadFromStream('b', $stream);
66+
67+
$fileDocument = $bucket->findOne(
68+
['length' => ['$lte' => 6]],
69+
[
70+
'projection' => [
71+
'filename' => 1,
72+
'length' => 1,
73+
'_id' => 0,
74+
],
75+
'sort' => ['length' => -1],
76+
]
77+
);
78+
79+
var_dump($fileDocument);
80+
81+
The output would then resemble::
82+
83+
object(MongoDB\Model\BSONDocument)#3004 (1) {
84+
["storage":"ArrayObject":private]=>
85+
array(2) {
86+
["filename"]=>
87+
string(1) "b"
88+
["length"]=>
89+
int(6)
90+
}
91+
}
5392

5493
See Also
5594
--------

docs/reference/method/MongoDBGridFSBucket-getBucketName.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@ Return Values
2626

2727
The name of this bucket as a string.
2828

29-
.. todo: add examples
29+
Examples
30+
--------
31+
32+
.. code-block:: php
33+
34+
<?php
35+
36+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
37+
38+
var_dump($bucket->getBucketName());
39+
40+
The output would then resemble::
41+
42+
string(2) "fs"

docs/reference/method/MongoDBGridFSBucket-getChunkSizeBytes.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ Return Values
2828

2929
The chunk size of this bucket in bytes.
3030

31-
.. todo: add examples
31+
Examples
32+
--------
33+
34+
.. code-block:: php
35+
36+
<?php
37+
38+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
39+
40+
var_dump($bucket->getChunkSizeBytes());
41+
42+
The output would then resemble::
43+
44+
int(261120)

docs/reference/method/MongoDBGridFSBucket-getChunksCollection.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ Return Values
2828

2929
A :phpclass:`MongoDB\\Collection` object for the chunks collection.
3030

31-
.. todo: add examples
31+
Examples
32+
--------
33+
34+
.. code-block:: php
35+
36+
<?php
37+
38+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
39+
40+
var_dump((string) $bucket->getChunksCollection());
41+
42+
The output would then resemble::
43+
44+
string(14) "test.fs.chunks"

docs/reference/method/MongoDBGridFSBucket-getDatabaseName.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ Return Values
2626

2727
The name of the database containing this bucket as a string.
2828

29-
.. todo: add examples
29+
Examples
30+
--------
31+
32+
.. code-block:: php
33+
34+
<?php
35+
36+
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
37+
38+
var_dump($bucket->getDatabaseName());
39+
40+
The output would then resemble::
41+
42+
string(4) "test"
43+

0 commit comments

Comments
 (0)