-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert blobValue back to a resource on fetch (#126)
* Convert blobValue back to a resource on fetch * Add test coverage to property and exclude mapper * Add Blob class and client factories * Store blob values as resource
- Loading branch information
Showing
10 changed files
with
381 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Copyright 2016 Google 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 | ||
* | ||
* http://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 Google\Cloud\Datastore; | ||
|
||
use GuzzleHttp\Psr7; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
/** | ||
* Represents a Blob value. | ||
* | ||
* Blobs can be used to store binary data in Google Cloud Datastore. | ||
* | ||
* Example: | ||
* ``` | ||
* $blob = $datastore->blob(file_get_contents(__DIR__ .'/family-photo.jpg')); | ||
* ``` | ||
*/ | ||
class Blob | ||
{ | ||
/** | ||
* @var mixed | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* Create a blob | ||
* | ||
* @param string|resource|StreamInterface $value The blob value | ||
*/ | ||
public function __construct($value) | ||
{ | ||
$this->value = Psr7\stream_for($value); | ||
} | ||
|
||
/** | ||
* Get the blob contents as a stream | ||
* | ||
* Example: | ||
* ``` | ||
* $value = $blob->value(); | ||
* ``` | ||
* | ||
* @returns StreamInterface | ||
*/ | ||
public function get() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Cast the blob to a string | ||
* | ||
* Example: | ||
* ``` | ||
* echo (string) $blob->value(); | ||
* ``` | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return (string) $this->value; | ||
} | ||
} |
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
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,56 @@ | ||
<?php | ||
/** | ||
* Copyright 2016 Google 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 | ||
* | ||
* http://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 Google\Cloud\Tests\Datastore; | ||
|
||
use Google\Cloud\Datastore\Blob; | ||
use GuzzleHttp\Psr7; | ||
|
||
/** | ||
* @group datastore | ||
*/ | ||
class BlobTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testBlobString() | ||
{ | ||
$blob = new Blob('hello world'); | ||
$this->assertEquals('hello world', (string) $blob); | ||
} | ||
|
||
public function testBlobResource() | ||
{ | ||
$string = 'hello world'; | ||
$stream = fopen('php://memory','r+'); | ||
fwrite($stream, $string); | ||
rewind($stream); | ||
|
||
$blob = new Blob($stream); | ||
$this->assertEquals('hello world', (string) $blob); | ||
} | ||
|
||
public function testBlobStreamInterface() | ||
{ | ||
$blob = new Blob(Psr7\stream_for('hello world')); | ||
$this->assertEquals('hello world', (string) $blob); | ||
} | ||
|
||
public function testToString() | ||
{ | ||
$blob = new Blob('hello world'); | ||
$this->assertEquals((string)$blob->get(), (string) $blob); | ||
} | ||
} |
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
Oops, something went wrong.