2017 © Pedro Peláez
 

library firestore-php

Firestore PHP Client

image

ahsankhatri/firestore-php

Firestore PHP Client

  • Friday, April 20, 2018
  • by ahsankhatri
  • Repository
  • 3 Watchers
  • 6 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 250 % Grown

The README.md

Firestore Client for PHP

Latest Version on Packagist Total Downloads License, (*1)

This package is totally based on Firestore REST API, (*2)

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, (*3)

Installation

You can install the package via composer:, (*4)

composer require ahsankhatri/firestore-php

or install it by adding it to composer.json then run composer update, (*5)

"require": {
    "ahsankhatri/firestore-php": "^2.0",
}

Dependencies

The bindings require the following extensions in order to work properly:, (*6)

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., (*7)

Usage

Initialization

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

Adding a document

$firestoreClient->addDocument($collection, [
    'booleanTrue' => true,
    'booleanFalse' => false,
    'null' => null,
    'string' => 'abc123',
    'integer' => 123456,
    'arrayRaw' => [
        'string' => 'abc123',
    ],
    'bytes' => new FirestoreBytes('bytesdata'),
    '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., (*8)

Or, (*9)

$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->setBytes('bytes', new FirestoreBytes('bytesdata'));
$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.., (*10)

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

Inserting/Updating a document

  • Update (Merge) or Insert document

Following will merge document (if exist) else insert the data., (*11)

$firestoreClient->updateDocument($documentRoot, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
]);

NOTE: Passing 3rd argument as a boolean true will force check that document must exist and vice-versa in order to perform update operation., (*12)

For example: If you want to update document only if exist else MrShan0\PHPFirestore\Exceptions\Client\NotFound (Exception) will be thrown., (*13)

$firestoreClient->updateDocument($documentPath, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], true);
  • Overwirte or Insert 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);

List documents with pagination (or custom parameters)

$collections = $firestoreClient->listDocuments('users', [
    'pageSize' => 1,
    'pageToken' => 'nextpagetoken'
]);

Note: You can pass custom parameters as supported by firestore list document, (*14)

Get field from document

$document->get('bytes')->parseValue(); // will return bytes decoded value.

// Catch field that doesn't exist in document
try {
    $document->get('allowed_notification');
} catch (\MrShan0\PHPFirestore\Exceptions\Client\FieldNotFound $e) {
    // Set default value
}

Firebase Authentication

Sign in with Email and Password.

$firestoreClient
    ->authenticator()
    ->signInEmailPassword('testuser@example.com', 'abc123');

Sign in Anonymously.

$firestoreClient
    ->authenticator()
    ->signInAnonymously();

Retrieve Auth Token

$authToken = $firestoreClient->authenticator()->getAuthToken();

TODO

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

Testing

bash composer test, (*15)

Changelog

Please see CHANGELOG for more information what has changed recently., (*16)

Contributing

Please see CONTRIBUTING for details., (*17)

Security

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

Credits

License

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

The Versions

20/04 2018

dev-master

9999999-dev https://github.com/ahsankhatri/firestore-php

Firestore PHP Client

  Sources   Download

MIT

The Requires

  • php >=5.6.6
  • ext-curl *
  • ext-json *

 

The Development Requires

php google firebase firestore

20/04 2018

v1.0.0

1.0.0.0 https://github.com/ahsankhatri/firestore-php

Firestore PHP Client

  Sources   Download

MIT

The Requires

  • php >=5.6.6
  • ext-curl *
  • ext-json *

 

The Development Requires

php google firebase firestore