Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
07cbd47b
Commit
07cbd47b
authored
Jun 30, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #202
parents
2e9b3268
eaf59995
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
2 deletions
+77
-2
BSONArray.php
src/Model/BSONArray.php
+16
-1
BSONDocument.php
src/Model/BSONDocument.php
+13
-1
BSONArrayTest.php
tests/Model/BSONArrayTest.php
+24
-0
BSONDocumentTest.php
tests/Model/BSONDocumentTest.php
+24
-0
No files found.
src/Model/BSONArray.php
View file @
07cbd47b
...
@@ -5,6 +5,7 @@ namespace MongoDB\Model;
...
@@ -5,6 +5,7 @@ namespace MongoDB\Model;
use
MongoDB\BSON\Serializable
;
use
MongoDB\BSON\Serializable
;
use
MongoDB\BSON\Unserializable
;
use
MongoDB\BSON\Unserializable
;
use
ArrayObject
;
use
ArrayObject
;
use
JsonSerializable
;
/**
/**
* Model class for a BSON array.
* Model class for a BSON array.
...
@@ -14,7 +15,7 @@ use ArrayObject;
...
@@ -14,7 +15,7 @@ use ArrayObject;
*
*
* @api
* @api
*/
*/
class
BSONArray
extends
ArrayObject
implements
Serializable
,
Unserializable
class
BSONArray
extends
ArrayObject
implements
JsonSerializable
,
Serializable
,
Unserializable
{
{
/**
/**
* Factory method for var_export().
* Factory method for var_export().
...
@@ -56,4 +57,18 @@ class BSONArray extends ArrayObject implements Serializable, Unserializable
...
@@ -56,4 +57,18 @@ class BSONArray extends ArrayObject implements Serializable, Unserializable
{
{
self
::
__construct
(
$data
);
self
::
__construct
(
$data
);
}
}
/**
* Serialize the array to JSON.
*
* The array data will be numerically reindexed to ensure that it is stored
* as a JSON array.
*
* @see http://php.net/jsonserializable.jsonserialize
* @return array
*/
public
function
jsonSerialize
()
{
return
array_values
(
$this
->
getArrayCopy
());
}
}
}
src/Model/BSONDocument.php
View file @
07cbd47b
...
@@ -5,6 +5,7 @@ namespace MongoDB\Model;
...
@@ -5,6 +5,7 @@ namespace MongoDB\Model;
use
MongoDB\BSON\Serializable
;
use
MongoDB\BSON\Serializable
;
use
MongoDB\BSON\Unserializable
;
use
MongoDB\BSON\Unserializable
;
use
ArrayObject
;
use
ArrayObject
;
use
JsonSerializable
;
/**
/**
* Model class for a BSON document.
* Model class for a BSON document.
...
@@ -14,7 +15,7 @@ use ArrayObject;
...
@@ -14,7 +15,7 @@ use ArrayObject;
*
*
* @api
* @api
*/
*/
class
BSONDocument
extends
ArrayObject
implements
Serializable
,
Unserializable
class
BSONDocument
extends
ArrayObject
implements
JsonSerializable
,
Serializable
,
Unserializable
{
{
/**
/**
* Constructor.
* Constructor.
...
@@ -66,4 +67,15 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
...
@@ -66,4 +67,15 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
{
{
parent
::
__construct
(
$data
,
ArrayObject
::
ARRAY_AS_PROPS
);
parent
::
__construct
(
$data
,
ArrayObject
::
ARRAY_AS_PROPS
);
}
}
/**
* Serialize the array to JSON.
*
* @see http://php.net/jsonserializable.jsonserialize
* @return object
*/
public
function
jsonSerialize
()
{
return
(
object
)
$this
->
getArrayCopy
();
}
}
}
tests/Model/BSONArrayTest.php
View file @
07cbd47b
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
;
use
MongoDB\Model\BSONArray
;
use
MongoDB\Model\BSONArray
;
use
MongoDB\Model\BSONDocument
;
class
BSONArrayTest
extends
TestCase
class
BSONArrayTest
extends
TestCase
{
{
...
@@ -15,6 +16,29 @@ class BSONArrayTest extends TestCase
...
@@ -15,6 +16,29 @@ class BSONArrayTest extends TestCase
$this
->
assertSame
([
'foo'
,
'bar'
],
$array
->
bsonSerialize
());
$this
->
assertSame
([
'foo'
,
'bar'
],
$array
->
bsonSerialize
());
}
}
public
function
testJsonSerialize
()
{
$document
=
new
BSONArray
([
'foo'
,
new
BSONArray
([
'foo'
=>
1
,
'bar'
=>
2
,
'baz'
=>
3
]),
new
BSONDocument
([
'foo'
=>
1
,
'bar'
=>
2
,
'baz'
=>
3
]),
new
BSONArray
([
new
BSONArray
([
new
BSONArray
])]),
]);
$expectedJson
=
'["foo",[1,2,3],{"foo":1,"bar":2,"baz":3},[[[]]]]'
;
$this
->
assertSame
(
$expectedJson
,
json_encode
(
$document
));
}
public
function
testJsonSerializeReindexesKeys
()
{
$data
=
[
0
=>
'foo'
,
2
=>
'bar'
];
$array
=
new
BSONArray
(
$data
);
$this
->
assertSame
(
$data
,
$array
->
getArrayCopy
());
$this
->
assertSame
([
'foo'
,
'bar'
],
$array
->
jsonSerialize
());
}
public
function
testSetState
()
public
function
testSetState
()
{
{
$data
=
[
'foo'
,
'bar'
];
$data
=
[
'foo'
,
'bar'
];
...
...
tests/Model/BSONDocumentTest.php
View file @
07cbd47b
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
;
use
MongoDB\Model\BSONArray
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Model\BSONDocument
;
use
ArrayObject
;
use
ArrayObject
;
...
@@ -23,6 +24,29 @@ class BSONDocumentTest extends TestCase
...
@@ -23,6 +24,29 @@ class BSONDocumentTest extends TestCase
$this
->
assertEquals
((
object
)
[
0
=>
'foo'
,
2
=>
'bar'
],
$document
->
bsonSerialize
());
$this
->
assertEquals
((
object
)
[
0
=>
'foo'
,
2
=>
'bar'
],
$document
->
bsonSerialize
());
}
}
public
function
testJsonSerialize
()
{
$document
=
new
BSONDocument
([
'foo'
=>
'bar'
,
'array'
=>
new
BSONArray
([
1
,
2
,
3
]),
'object'
=>
new
BSONDocument
([
1
,
2
,
3
]),
'nested'
=>
new
BSONDocument
([
new
BSONDocument
([
new
BSONDocument
])]),
]);
$expectedJson
=
'{"foo":"bar","array":[1,2,3],"object":{"0":1,"1":2,"2":3},"nested":{"0":{"0":{}}}}'
;
$this
->
assertSame
(
$expectedJson
,
json_encode
(
$document
));
}
public
function
testJsonSerializeCastsToObject
()
{
$data
=
[
0
=>
'foo'
,
2
=>
'bar'
];
$document
=
new
BSONDocument
(
$data
);
$this
->
assertSame
(
$data
,
$document
->
getArrayCopy
());
$this
->
assertEquals
((
object
)
[
0
=>
'foo'
,
2
=>
'bar'
],
$document
->
jsonSerialize
());
}
public
function
testSetState
()
public
function
testSetState
()
{
{
$data
=
[
'foo'
=>
'bar'
];
$data
=
[
'foo'
=>
'bar'
];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment