123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- /*
- * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- use Firebase\FirebaseLib;
- class FirebaseCon {
- private static $Instance = null;
- private string $URL;
- private string $Token;
- private string $Path;
- private FirebaseLib $API;
- private function __construct(string $URL, string $Token, string $Path) {
- $this->URL = $URL;
- $this->Token = $Token;
- $this->Path = $Path;
- $this->API = new FirebaseLib($this->URL, $this->Token);
- }
- public static function GetInstance(string $URL, string $Token, string $Path) {
- if (self::$Instance === null) {
- self::$Instance = new FirebaseCon($URL, $Token, $Path);
- }
- return self::$Instance;
- }
- private function GetJSON(string $SpecificPath): string {
- $Result = $this->API->get($this->Path . $SpecificPath);
- return !in_array($Result, [null, 'null']) ? $Result : '[]';
- }
- private function SetJSON(string $SpecificPath, array $Element): bool {
- return $this->API->set($this->Path . $SpecificPath, $Element);
- }
- public function DeleteJSON(string $SpecificPath): bool {
- return $this->API->delete($this->Path . $SpecificPath);
- }
- // private function CheckListId(array $List, string $Id): array {
- // $Exists = false;
- //
- // $Position = 0;
- // while ($Position < count($List) && !$Exists) {
- // if ($List[$Position]['id'] === $Id) {
- // $Exists = true;
- // }
- //
- // $Position++;
- // }
- //
- // return $Exists ? $List[$Position] : [];
- // }
- public function GetList(string $SpecificPath): array {
- return json_decode($this->GetJSON($SpecificPath), true);
- }
- public function GetListElement(string $SpecificPath, $Id): array {
- // return $this->CheckListId($this->GetList($SpecificPath), $Id);
- // return json_decode($this->GetJSON($SpecificPath . '/' . $Id . '.json'));
- return json_decode($this->GetJSON($SpecificPath . '/' . $Id), true);
- }
- public function AddListElement(string $SpecificPath, array $Element): bool {
- $Check = $this->GetListElement($SpecificPath, $Element['id']);
-
- if (isset($Check['error']) || count($Check) === 0) {
- return $this->SetJSON($SpecificPath . '/' . $Element['id'],
- $Element);
- } else {
- return false;
- }
- }
- public function RemoveListElement(string $SpecificPath, string $Id): bool {
- $Check = $this->GetListElement($SpecificPath, $Id);
-
- if (count($Check) !== 0 && !isset($Check['error'])) {
- return $this->DeleteJSON($SpecificPath . '/' . $Id);
- } else {
- return false;
- }
- }
- }
|