diff --git a/src/Exceptions/UnhandledRequestError.php b/src/Exceptions/UnhandledRequestError.php index 21db0a3..972a1c9 100644 --- a/src/Exceptions/UnhandledRequestError.php +++ b/src/Exceptions/UnhandledRequestError.php @@ -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); } } diff --git a/src/FirestoreClient.php b/src/FirestoreClient.php index e8d820e..fdc5fc1 100644 --- a/src/FirestoreClient.php +++ b/src/FirestoreClient.php @@ -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 { /** diff --git a/src/FirestoreDatabaseResource.php b/src/FirestoreDatabaseResource.php index 3662a74..50ccc96 100644 --- a/src/FirestoreDatabaseResource.php +++ b/src/FirestoreDatabaseResource.php @@ -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, diff --git a/src/FirestoreDocument.php b/src/FirestoreDocument.php index ea9ee0c..c234c85 100644 --- a/src/FirestoreDocument.php +++ b/src/FirestoreDocument.php @@ -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; + } } } @@ -76,6 +78,10 @@ public function getAbsoluteName() */ public function getCreatedTime() { + if (is_null($this->createTime)) { + return null; + } + return new DateTime($this->createTime); } @@ -84,6 +90,10 @@ public function getCreatedTime() */ public function getUpdatedTime() { + if (is_null($this->updateTime)) { + return null; + } + return new DateTime($this->updateTime); } diff --git a/tests/FirestoreDocumentTest.php b/tests/FirestoreDocumentTest.php new file mode 100644 index 0000000..f824ddb --- /dev/null +++ b/tests/FirestoreDocumentTest.php @@ -0,0 +1,118 @@ + '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()); + } +}