-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPLIB-1220: Support codec option for GridFS buckets (#1149)
* PHPLIB-1220: Support codec option for GridFS buckets * Support null as an uploadDate in test codec * Add test to ensure bucket codec can be overridden * Omit instanceof check for codec * Forbid specifying codec and typeMap in bucket options * Use null as default value for length * Convert embedded metadata document to stdClass
- Loading branch information
Showing
5 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests\Fixtures\Codec; | ||
|
||
use DateTimeImmutable; | ||
use MongoDB\BSON\Document; | ||
use MongoDB\BSON\UTCDateTime; | ||
use MongoDB\Codec\DecodeIfSupported; | ||
use MongoDB\Codec\DocumentCodec; | ||
use MongoDB\Codec\EncodeIfSupported; | ||
use MongoDB\Exception\UnsupportedValueException; | ||
use MongoDB\Tests\Fixtures\Document\TestFile; | ||
|
||
use function assert; | ||
|
||
final class TestFileCodec implements DocumentCodec | ||
{ | ||
use DecodeIfSupported; | ||
use EncodeIfSupported; | ||
|
||
public function canDecode($value): bool | ||
{ | ||
return $value instanceof Document; | ||
} | ||
|
||
public function decode($value): TestFile | ||
{ | ||
if (! $value instanceof Document) { | ||
throw UnsupportedValueException::invalidDecodableValue($value); | ||
} | ||
|
||
$fileObject = new TestFile(); | ||
$fileObject->id = $value->get('_id'); | ||
$fileObject->length = (int) $value->get('length'); | ||
$fileObject->chunkSize = (int) $value->get('chunkSize'); | ||
$fileObject->filename = (string) $value->get('filename'); | ||
|
||
$uploadDate = $value->get('uploadDate'); | ||
if ($uploadDate instanceof UTCDateTime) { | ||
$fileObject->uploadDate = DateTimeImmutable::createFromMutable($uploadDate->toDateTime()); | ||
} | ||
|
||
if ($value->has('metadata')) { | ||
$metadata = $value->get('metadata'); | ||
assert($metadata instanceof Document); | ||
$fileObject->metadata = $metadata->toPHP(); | ||
} | ||
|
||
return $fileObject; | ||
} | ||
|
||
public function canEncode($value): bool | ||
{ | ||
return $value instanceof TestFile; | ||
} | ||
|
||
public function encode($value): Document | ||
{ | ||
if (! $value instanceof TestFile) { | ||
throw UnsupportedValueException::invalidEncodableValue($value); | ||
} | ||
|
||
return Document::fromPHP([ | ||
'_id' => $value->id, | ||
'length' => $value->length, | ||
'chunkSize' => $value->chunkSize, | ||
'uploadDate' => new UTCDateTime($value->uploadDate), | ||
'filename' => $value->filename, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/* | ||
* Copyright 2023-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB\Tests\Fixtures\Document; | ||
|
||
use DateTimeImmutable; | ||
use stdClass; | ||
|
||
final class TestFile | ||
{ | ||
public $id; | ||
public int $length; | ||
public int $chunkSize; | ||
public ?DateTimeImmutable $uploadDate = null; | ||
public string $filename; | ||
public ?stdClass $metadata = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters