Skip to content

Commit 9c13c6f

Browse files
committed
more phpcs errors
1 parent 9aba378 commit 9c13c6f

File tree

6 files changed

+152
-128
lines changed

6 files changed

+152
-128
lines changed

source/data-formats/modeling-bson-data.txt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ these values into BSON documents when inserting them into a collection.
5757
The following example creates a BSON document that represents the preceding
5858
:ref:`sample BSON document <php-bson-sample>`:
5959

60-
.. literalinclude:: /includes/bson.php
60+
.. literalinclude:: /includes/bson/interact-bson.php
6161
:language: php
6262
:dedent:
6363
:start-after: start-create-doc
@@ -73,7 +73,7 @@ to the :ref:`sample BSON document <php-bson-sample>`:
7373
- Adds a new ``restaurant_id`` field that has a value of ``12345``
7474
- Changes the ``name`` field value to ``"Galaxy Pizza"``
7575

76-
.. literalinclude:: /includes/bson.php
76+
.. literalinclude:: /includes/bson/interact-bson.php
7777
:language: php
7878
:dedent:
7979
:start-after: start-modify-doc
@@ -142,7 +142,7 @@ Custom Type Map Example
142142
The following example sets the ``typeMap`` option for the ``restaurants`` collection
143143
that serializes arrays and BSON documents as ``MongoDB\Model\BSONDocument`` objects:
144144

145-
.. literalinclude:: /includes/bson.php
145+
.. literalinclude:: /includes/bson/interact-bson.php
146146
:language: php
147147
:dedent:
148148
:start-after: start-type-map
@@ -170,7 +170,7 @@ Consider the following ``Person`` class definition, which implements the
170170
``Persistable`` interface and specifies how to serialize and deserialize
171171
object fields as BSON values:
172172

173-
.. literalinclude:: /includes/bson.php
173+
.. literalinclude:: /includes/bson/Person.php
174174
:language: php
175175
:dedent:
176176
:start-after: start-person-class
@@ -182,7 +182,7 @@ database, and reads it back as an object of the same type:
182182
.. io-code-block::
183183
:copyable:
184184

185-
.. input:: /includes/bson.php
185+
.. input:: /includes/bson/interact-bson.php
186186
:start-after: start-person-serialize
187187
:end-before: end-person-serialize
188188
:language: php
@@ -245,24 +245,33 @@ and ``bsonUnserialize()`` methods in your class definition.
245245
Example
246246
```````
247247

248-
Consider the following ``User`` class definition, which specifies
249-
logic for serializing and deserializing its fields into BSON values.
250-
The class includes a ``role`` field, which has a backed enum value:
248+
Consider the following backed enum named ``Role``, which has two
249+
integer-valued cases:
251250

252-
.. literalinclude:: /includes/bson.php
251+
.. literalinclude:: /includes/bson/Role.php
253252
:language: php
254-
:emphasize-lines: 15, 24
255253
:dedent:
256254
:start-after: start-backed-enum
257255
:end-before: end-backed-enum
258256

257+
This ``User`` class definition includes a ``role`` field with a ``Role`` enum
258+
value and specifies logic for serializing and deserializing its fields into BSON
259+
values:
260+
261+
.. literalinclude:: /includes/bson/User.php
262+
:language: php
263+
:emphasize-lines: 5
264+
:dedent:
265+
:start-after: start-user-class
266+
:end-before: end-user-class
267+
259268
The following example constructs a ``User`` object with a ``role`` field,
260269
inserts it into the database, and reads it back as an object of the same type:
261270

262271
.. io-code-block::
263272
:copyable:
264273

265-
.. input:: /includes/bson.php
274+
.. input:: /includes/bson/interact-bson.php
266275
:start-after: start-enum-serialize
267276
:end-before: end-enum-serialize
268277
:language: php

source/includes/bson.php

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

source/includes/bson/Person.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Bson;
4+
5+
// start-person-class
6+
class Person implements MongoDB\BSON\Persistable
7+
{
8+
private MongoDB\BSON\ObjectId $id;
9+
private string $name;
10+
private MongoDB\BSON\UTCDateTime $createdAt;
11+
12+
public function __construct(string $name)
13+
{
14+
$this->id = new MongoDB\BSON\ObjectId;
15+
$this->name = $name;
16+
$this->createdAt = new MongoDB\BSON\UTCDateTime;
17+
}
18+
19+
function bsonSerialize()
20+
{
21+
return [
22+
'_id' => $this->id,
23+
'name' => $this->name,
24+
'createdAt' => $this->createdAt,
25+
];
26+
}
27+
28+
function bsonUnserialize(array $data)
29+
{
30+
$this->id = $data['_id'];
31+
$this->name = $data['name'];
32+
$this->createdAt = $data['createdAt'];
33+
}
34+
}
35+
// end-person-class

source/includes/bson/Role.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bson;
4+
5+
// start-backed-enum
6+
enum Role: int
7+
{
8+
case USER = 1;
9+
case ADMIN = 2;
10+
}
11+
// end-backed-enum

source/includes/bson/User.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bson;
4+
5+
// start-user-class
6+
class User implements MongoDB\BSON\Persistable
7+
{
8+
public function __construct(
9+
private string $username,
10+
private Role $role,
11+
private MongoDB\BSON\ObjectId $_id = new MongoDB\BSON\ObjectId(),
12+
) {}
13+
14+
public function bsonSerialize(): array
15+
{
16+
return [
17+
'_id' => $this->_id,
18+
'username' => $this->username,
19+
'role' => $this->role,
20+
];
21+
}
22+
23+
public function bsonUnserialize(array $data): void
24+
{
25+
$this->_id = $data['_id'];
26+
$this->username = $data['username'];
27+
$this->role = Role::from($data['role']);
28+
}
29+
}
30+
// end-user-class
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use Bson\Person;
6+
use Bson\User;
7+
use Bson\Role;
8+
9+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
10+
$client = new MongoDB\Client($uri);
11+
$db = $client->test;
12+
13+
// start-create-doc
14+
$document = [
15+
'address' => [
16+
'street' => 'Pizza St',
17+
'zipcode' => '10003'
18+
],
19+
'coord' => [-73.982419, 41.579505],
20+
'cuisine' => 'Pizza',
21+
'name' => 'Planet Pizza'
22+
];
23+
// end-create-doc
24+
25+
// start-modify-doc
26+
$document['restaurant_id'] = 12345;
27+
$document['name'] = 'Galaxy Pizza';
28+
// end-modify-doc
29+
30+
// start-type-map
31+
$options = [
32+
'typeMap' => [
33+
'array' => 'MongoDB\Model\BSONDocument',
34+
'root' => 'MongoDB\Model\BSONDocument',
35+
'document' => 'MongoDB\Model\BSONDocument',
36+
],
37+
];
38+
39+
$db->createCollection('restaurants', $options);
40+
// end-type-map
41+
42+
// start-person-serialize
43+
$collection = $client->test->persons;
44+
$result = $collection->insertOne(new Person('Bob'));
45+
$person = $collection->findOne(['_id' => $result->getInsertedId()]);
46+
47+
var_dump($person);
48+
// end-person-serialize
49+
50+
// start-enum-serialize
51+
$collection = $client->test->users;
52+
$result = $collection->insertOne(new User('alice', Role::USER));
53+
$person = $collection->findOne(['_id' => $result->getInsertedId()]);
54+
55+
var_dump($person);
56+
// end-enum-serialize

0 commit comments

Comments
 (0)