Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Exceptions/UnhandledRequestError.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class UnhandledRequestError extends \Exception
public function __construct($code, $response)
{
$message = 'The request failed with the error: '.$code.'. Response: '.$response;
parent::__construct($message);
parent::__construct($message, $code);
}
}
9 changes: 9 additions & 0 deletions src/FirestoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
use MrShan0\PHPFirestore\Handlers\RequestErrorHandler;
use MrShan0\PHPFirestore\Helpers\FirestoreHelper;

/**
* @method array listDocuments($collection, array $parameters = [], array $options = [])
* @method FirestoreDocument getDocument($documentPath, array $parameters = [], array $options = [])
* @method array getBatchDocuments(array $documentsId, array $parameters = [], array $options = [])
* @method FirestoreDocument addDocument($collection, $payload, $documentId = null, array $parameters = [], array $options = [])
* @method FirestoreDocument updateDocument($documentPath, $payload, $documentExists = null, array $parameters = [], array $options = [])
* @method FirestoreDocument setDocument($documentPath, $payload, $documentExists = null, array $parameters = [], array $options = [])
* @method boolean deleteDocument($document, array $options = [])
*/
class FirestoreClient
{
/**
Expand Down
10 changes: 7 additions & 3 deletions src/FirestoreDatabaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ public function listDocuments($collection, array $parameters = [], array $option

$response = $this->client->request('GET', 'documents/' . FirestoreHelper::normalizeCollection($collection), $options, $parameters);

$documents = array_map(function($doc) {
return new FirestoreDocument($doc);
}, $response['documents']);
if (isset($response['documents'])) {
$documents = array_map(function($doc) {
return new FirestoreDocument($doc);
}, $response['documents']);
} else {
$documents = [];
}

return array_merge($response, [
'documents' => $documents,
Expand Down
18 changes: 14 additions & 4 deletions src/FirestoreDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public function __construct($object = null, $databaseResource = null)
{
if (null !== $object) {
$this->name = $object['name'];
$this->createTime = $object['createTime'];
$this->updateTime = $object['updateTime'];
$this->createTime = isset($object['createTime']) ? $object['createTime'] : null;
$this->updateTime = isset($object['updateTime']) ? $object['updateTime'] : null;

foreach ($object['fields'] as $fieldName => $value) {
$this->fields[ $fieldName ] = $value;
if (isset($object['fields'])) {
foreach ($object['fields'] as $fieldName => $value) {
$this->fields[ $fieldName ] = $value;
}
}
}

Expand Down Expand Up @@ -76,6 +78,10 @@ public function getAbsoluteName()
*/
public function getCreatedTime()
{
if (is_null($this->createTime)) {
return null;
}

return new DateTime($this->createTime);
}

Expand All @@ -84,6 +90,10 @@ public function getCreatedTime()
*/
public function getUpdatedTime()
{
if (is_null($this->updateTime)) {
return null;
}

return new DateTime($this->updateTime);
}

Expand Down
118 changes: 118 additions & 0 deletions tests/FirestoreDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace MrShan0\PHPFirestore\Tests;

use MrShan0\PHPFirestore\FirestoreDocument;
use PHPUnit\Framework\TestCase;

class FirestoreDocumentTest extends TestCase
{
/** @test */
public function get_absolute_name_returns_name()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
]
);

$this->assertSame('projects/database/(default)/documents/collection/item', $document->getAbsoluteName());
}


/** @test */
public function get_absolute_name_returns_null_if_no_document_provided()
{
$document = new FirestoreDocument();

$this->assertNull($document->getAbsoluteName());
}


/** @test */
public function get_created_time_returns_created_time()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
'createTime' => '2018-12-24T15:00:00.123456Z',
'updateTime' => '2018-12-24T16:00:00.123456Z',
'fields' => [
'myKey' => [
'stringValue' => 'my value'
]
],
]
);

$this->assertEquals(new \DateTime('2018-12-24T15:00:00.123456Z'), $document->getCreatedTime());
}


/** @test */
public function get_updated_time_returns_updated_time()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
'createTime' => '2018-12-24T15:00:00.123456Z',
'updateTime' => '2018-12-24T16:00:00.123456Z',
'fields' => [
'myKey' => [
'stringValue' => 'my value'
]
],
]
);

$this->assertEquals(new \DateTime('2018-12-24T16:00:00.123456Z'), $document->getUpdatedTime());
}


/** @test */
public function get_created_time_returns_null_if_document_does_not_exist()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
]
);

$this->assertNull($document->getCreatedTime());
}


/** @test */
public function get_updated_time_returns_null_if_document_does_not_exist()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
]
);

$this->assertNull($document->getUpdatedTime());
}


/** @test */
public function get_name_returns_name()
{
$document = new FirestoreDocument(
[
'name' => 'projects/database/(default)/documents/collection/item',
]
);

$this->assertSame('projects/database/(default)/documents/collection/item', $document->getName());
}


/** @test */
public function get_name_returns_null_if_no_document_provided()
{
$document = new FirestoreDocument();

$this->assertNull($document->getName());
}
}