Firestore PHP Client
Switch branches/tags
Nothing to show
Clone or download
ahsankhatri Merge pull request #1 from Baha2Odeh/patch-1
Update FireStoreApiClient.php
Latest commit 84e0f14 Jul 31, 2018

README.md

Firestore Client for PHP

Latest Version on Packagist Total Downloads License

This package is totally based on Firestore REST API

Authentication / Generate API Key

  1. Visit Google Cloud Firestore API
  2. Select your desired project.
  3. Select Credentials from left menu and select API Key from Server key or Create your own credentials

Installation

You can install the package via composer:

composer require ahsankhatri/firestore-php

Dependencies

The bindings require the following extensions in order to work properly:

If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.

Usage

Initialization

$firestoreClient = new FireStoreApiClient('project-id', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
    'database' => '(default)',
]);

Adding a document

$firestoreClient->addDocument($collection, [
    'booleanTrue' => true,
    'booleanFalse' => false,
    'null' => null,
    'string' => 'abc123',
    'integer' => 123456,
    'arrayRaw' => [
        'string' => 'abc123',
    ],
    'array' => new FireStoreArray([
        'string' => 'abc123',
    ]),
    'reference' => new FireStoreReference('/users/23'),
    'object' => new FireStoreObject(['nested1' => new FireStoreObject(['nested2' => new FireStoreObject(['nested3' => 'test'])])]),
    'timestamp' => new FireStoreTimestamp,
    'geopoint' => new FireStoreGeoPoint(1,1),
]);

NOTE: Pass third argument if you want your custom document id to set else auto-id will generate it for you.

Or

$document = new FireStoreDocument;
$document->setObject('sdf', new FireStoreObject(['nested1' => new FireStoreObject(['nested2' => new FireStoreObject(['nested3' => 'test'])])]));
$document->setBoolean('booleanTrue', true);
$document->setBoolean('booleanFalse', false);
$document->setNull('null', null);
$document->setString('string', 'abc123');
$document->setInteger('integer', 123456);
$document->setArray('arrayRaw', ['string'=>'abc123']);
$document->setArray('arrayObject', new FireStoreArray(['string' => 'abc123']));
$document->setTimestamp('timestamp', new FireStoreTimestamp);
$document->setGeoPoint('geopoint', new FireStoreGeoPoint(1.11,1.11));
$firestoreClient->addDocument($collection, $document, 'customDocumentId');

And..

$document->fillValues([
    'string' => 'abc123',
    'boolean' => true,
]);

Updating a document

  • Update existing document
$firestoreClient->updateDocument($collection, $documentId, [
    'newFieldToAdd' => new FireStoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FireStoreDeleteAttribute
], true);

NOTE: Passing 3rd argument as a boolean true will indicate that document must exist and vice-versa.

  • Overwrite existing document
$firestoreClient->setDocument($collection, $documentId, [
    'newFieldToAdd' => new FireStoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FireStoreDeleteAttribute
], [
    'exists' => true, // Indicate document must exist
]);

Deleting a document

$collection = 'collection/document/innerCollection';
$firestoreClient->deleteDocument($collection, $documentId);

TODO

  • Added delete attribute support.
  • Add Support for Object, Boolean, Null, String, Integer, Array, Timestamp, GeoPoint
  • Add Exception Handling.
  • List all documents and collections.
  • Filters and pagination support.
  • Transaction support.
  • Indexes support.
  • Entire collection delete support.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email ahsankhatri1992@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.