Skip to content

Commit

Permalink
Merge pull request #99 from ktamas77/php7-support
Browse files Browse the repository at this point in the history
PHP 7 Support
  • Loading branch information
ktamas77 committed Jan 14, 2021
2 parents c4b5d1a + 9ac892c commit 80ccc4d
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 506 deletions.
5 changes: 3 additions & 2 deletions .gitignore
@@ -1,4 +1,5 @@
nbproject/private/
composer.lock
.idea
/nbproject/private/
.phpunit.result.cache
vendor
composer.lock
4 changes: 1 addition & 3 deletions .travis.yml
@@ -1,5 +1,3 @@
language: php
php:
- 5.3
- 5.4
- 5.5
- 7.2
91 changes: 30 additions & 61 deletions README.md
Expand Up @@ -18,58 +18,70 @@ Available on [Packagist](https://packagist.org/packages/ktamas77/firebase-php).

### Adding Firebase PHP to your project using Composer

#### For PHP 7 or later

```bash
cd <your_project>
composer require ktamas77/firebase-php
```

#### For PHP 5 use v2.2.4

```bash
composer require ktamas77/firebase-php:2.2.4
```

More info about Composer at [getcomposer.org](http://getcomposer.org).

### Example
```php
const DEFAULT_URL = 'https://kidsplace.firebaseio.com/';
const DEFAULT_TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp';
const DEFAULT_PATH = '/firebase/example';

// Firebase Token can be found in the Firebase Console:
// Settings -> Project Settings -> Service accounts -> Database secrets

const URL = 'https://kidsplace.firebaseio.com/';
const TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp';
const PATH = '/firebase/example';

use Firebase\FirebaseLib;

$firebase = new FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
$firebase = new FirebaseLib(URL, TOKEN);

// --- storing an array ---
// Storing an array
$test = [
'foo' => 'bar',
'i_love' => 'lamp',
'id' => 42
];
$dateTime = new DateTime();
$firebase->set(DEFAULT_PATH . '/' . $dateTime->format('c'), $test);
$firebase->set(PATH . '/' . $dateTime->format('c'), $test);

// --- storing a string ---
$firebase->set(DEFAULT_PATH . '/name/contact001', 'John Doe');
// Storing a string
$firebase->set(PATH . '/name/contact001', 'John Doe');

// --- reading the stored string ---
$name = $firebase->get(DEFAULT_PATH . '/name/contact001');
// Reading the stored string
$name = $firebase->get(PATH . '/name/contact001');
```

### Supported Commands
```php
// -- Firebase API commands
// Firebase API commands

$firebase->set($path, $value); // stores data in Firebase
$value = $firebase->get($path); // reads a value from Firebase
$firebase->delete($path); // deletes value from Firebase
$firebase->update($path, $data); // updates data in Firebase
$firebase->push($path, $data); // push data to Firebase

// -- Query Parameters can be optionally used on all operations, example:
// Query Parameters can be optionally used on all operations, example:

$value = $firebase->get($path, array('shallow' => 'true'));
$value = $firebase->get($path, ['shallow' => 'true']);

// -- Query Parameter values with quotes, example (https://firebase.google.com/docs/database/rest/retrieve-data#filtering-by-a-specified-child-key):
// Query Parameter values with quotes example
// Documentation: https://firebase.google.com/docs/database/rest/retrieve-data#filtering-by-a-specified-child-key

$value = $firebase->get($path, array('orderBy' => '"height"'));
$value = $firebase->get($path, ['orderBy' => '"height"']);

// -- Firebase PHP Library commands
// Firebase PHP Library commands

$firebase->setToken($token); // set up Firebase token
$firebase->setBaseURI($uri); // set up Firebase base URI (root node)
Expand All @@ -78,48 +90,6 @@ $firebase->setTimeOut($seconds); // set up maximum timeout / request

Please refer to the [Firebase REST API documentation](https://www.firebase.com/docs/rest/api/) for further details.

### Firebase PHP Stub
A Firebase PHP Stub has been created to allow for integration with phpunit without actually interacting with FirebaseIO.

To use the firebaseLib and firebaseStub in your application and testing, you must pass in a firebase object to your application.

For example, if your current code is:

```php
public function setFirebaseValue($path, $value) {
$url = 'https://radiant-fire-2427.firebaseio.com';
$token = 'czvEX8vMU8FZn4wYCvf466P3J6zH5ZlKQeuwxmEZ';
$firebase = new Firebase($url, $token);
$firebase->set($path, $value);
}
```

You will change it to be:

```php
public function setFirebaseValue($path, $value, $firebase) {
$firebase->set($path, $value);
}
```

Then your phpunit tests will look like:

```php
<?php
require_once '<path>/lib/firebaseInterface.php';
require_once '<path>/lib/firebaseStub.php';

class MyClass extends PHPUnit_Framework_TestCase
{
public function testSetFirebaseValue() {
$myClass = new MyClass();
$firebaseStub = new FirebaseStub($uri, $token);
$myClass->setFirebaseValue($path, $value, $firebaseStub);
}
}
?>
```

### Composer upgrade

Coding standards check / fixing & tests are integrated with composer.
Expand Down Expand Up @@ -151,10 +121,9 @@ To automatically fix standards (whenever it's possible):
$ composer style-fix
```


#### The MIT License (MIT)
```
Copyright (c) 2012-2018 Tamas Kalman
Copyright (c) 2012-2021 Tamas Kalman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Expand Up @@ -7,7 +7,7 @@
"REST",
"wrapper"
],
"version": "2.2.4",
"version": "3.0.0",
"authors": [
{
"name": "Tamas Kalman",
Expand All @@ -17,19 +17,21 @@
"scripts": {
"style": "vendor/squizlabs/php_codesniffer/bin/phpcs -p --encoding=utf-8 --standard=ruleset.xml --colors .",
"style-fix": "vendor/squizlabs/php_codesniffer/bin/phpcbf -p --encoding=utf-8 --standard=ruleset.xml --colors .",
"test": "vendor/phpunit/phpunit/phpunit tests/firebaseStubTest.php"
"test": "vendor/phpunit/phpunit/phpunit"
},
"license": "MIT",
"require": {
"php": ">=5.4.0"
"php": ">=7.2",
"ext-json": "*",
"ext-curl": "*"
},
"autoload": {
"classmap": [
"src"
]
},
"require-dev": {
"phpunit/phpunit": "^5",
"squizlabs/php_codesniffer": "^3.4"
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
}
}
4 changes: 2 additions & 2 deletions phpunit.xml
@@ -1,7 +1,7 @@
<phpunit>
<testsuites>
<testsuite>
<directory>tests/</directory>
<testsuite name="Firebase PHP Client Test Suite">
<directory>tests/Firebase</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 1 addition & 3 deletions ruleset.xml
Expand Up @@ -2,8 +2,6 @@
<ruleset name="Firebase-PHP">
<description>PHP Coding Standards for Firebase PHP Client</description>
<ini name="memory_limit" value="2048M"/>
<rule ref="PSR2">
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
</rule>
<rule ref="PSR2"/>
<exclude-pattern>vendor/</exclude-pattern>
</ruleset>
24 changes: 0 additions & 24 deletions src/firebaseError.php

This file was deleted.

48 changes: 24 additions & 24 deletions src/firebaseInterface.php
Expand Up @@ -10,58 +10,58 @@
interface FirebaseInterface
{
/**
* @param $token
* @param string $token Token
* @return mixed
*/
public function setToken($token);
public function setToken(string $token): void;

/**
* @param $baseURI
* @param string $baseURI Base URI
* @return mixed
*/
public function setBaseURI($baseURI);
public function setBaseURI(string $baseURI): void;

/**
* @param $seconds
* @param int $seconds Seconds
* @return mixed
*/
public function setTimeOut($seconds);
public function setTimeOut(int $seconds): void;

/**
* @param $path
* @param $data
* @param $options
* @param string $path
* @param mixed $data
* @param array $options
* @return mixed
*/
public function set($path, $data, array $options = []);
public function set(string $path, array $data, array $options = []);

/**
* @param $path
* @param $data
* @param $options
* @param string $path
* @param mixed $data
* @param array $options
* @return mixed
*/
public function push($path, $data, array $options = []);
public function push(string $path, $data, array $options = []);

/**
* @param $path
* @param $data
* @param $options
* @param string $path
* @param mixed $data
* @param array $options
* @return mixed
*/
public function update($path, $data, array $options = []);
public function update(string $path, $data, array $options = []);

/**
* @param $path
* @param $options
* @param string $path
* @param array $options
* @return mixed
*/
public function get($path, array $options = []);
public function delete(string $path, array $options = []);

/**
* @param $path
* @param $options
* @param string $path
* @param array $options
* @return mixed
*/
public function delete($path, array $options = []);
public function get(string $path, array $options = []);
}

0 comments on commit 80ccc4d

Please sign in to comment.