FirebaseCon.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. use Firebase\FirebaseLib;
  19. class FirebaseCon {
  20. private static $Instance = null;
  21. private string $URL;
  22. private string $Token;
  23. private string $Path;
  24. private FirebaseLib $API;
  25. private function __construct(string $URL, string $Token, string $Path) {
  26. $this->URL = $URL;
  27. $this->Token = $Token;
  28. $this->Path = $Path;
  29. $this->API = new FirebaseLib($this->URL, $this->Token);
  30. }
  31. public static function GetInstance(string $URL, string $Token, string $Path) {
  32. if (self::$Instance === null) {
  33. self::$Instance = new FirebaseCon($URL, $Token, $Path);
  34. }
  35. return self::$Instance;
  36. }
  37. private function GetJSON(string $SpecificPath): string {
  38. $Result = $this->API->get($this->Path . $SpecificPath);
  39. return !in_array($Result, [null, 'null']) ? $Result : '[]';
  40. }
  41. private function SetJSON(string $SpecificPath, array $Element): bool {
  42. return $this->API->set($this->Path . $SpecificPath, $Element);
  43. }
  44. public function DeleteJSON(string $SpecificPath): bool {
  45. return $this->API->delete($this->Path . $SpecificPath);
  46. }
  47. // private function CheckListId(array $List, string $Id): array {
  48. // $Exists = false;
  49. //
  50. // $Position = 0;
  51. // while ($Position < count($List) && !$Exists) {
  52. // if ($List[$Position]['id'] === $Id) {
  53. // $Exists = true;
  54. // }
  55. //
  56. // $Position++;
  57. // }
  58. //
  59. // return $Exists ? $List[$Position] : [];
  60. // }
  61. public function GetList(string $SpecificPath): array {
  62. return json_decode($this->GetJSON($SpecificPath), true);
  63. }
  64. public function GetListElement(string $SpecificPath, $Id): array {
  65. // return $this->CheckListId($this->GetList($SpecificPath), $Id);
  66. // return json_decode($this->GetJSON($SpecificPath . '/' . $Id . '.json'));
  67. return json_decode($this->GetJSON($SpecificPath . '/' . $Id), true);
  68. }
  69. public function AddListElement(string $SpecificPath, array $Element): bool {
  70. $Check = $this->GetListElement($SpecificPath, $Element['id']);
  71. if (isset($Check['error']) || count($Check) === 0) {
  72. return $this->SetJSON($SpecificPath . '/' . $Element['id'],
  73. $Element);
  74. } else {
  75. return false;
  76. }
  77. }
  78. public function RemoveListElement(string $SpecificPath, string $Id): bool {
  79. $Check = $this->GetListElement($SpecificPath, $Id);
  80. if (count($Check) !== 0 && !isset($Check['error'])) {
  81. return $this->DeleteJSON($SpecificPath . '/' . $Id);
  82. } else {
  83. return false;
  84. }
  85. }
  86. }