Skip to content

Commit 27b5bf5

Browse files
Merge pull request Ashishkumbhar01#34 from CodeWithSushil/master
[update] Add type safety and php 8.4 supported
2 parents 573fee6 + d41f483 commit 27b5bf5

File tree

6 files changed

+125
-34
lines changed

6 files changed

+125
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ $data = [
8282
'version' => '8.3'
8383
];
8484

85-
$client->postData('table name', $data);
85+
$client->postData('table name', $data, 'id');
86+
// 3rd option on_conflict
8687
```
8788

8889
### `updateData()`

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "supabase-php/supabase-client",
3-
"description": "Supabase for PHP client.",
4-
"keywords": ["supabase","supabase-php","supabase-client"],
5-
"version": "1.0.7",
2+
"$schema": "https://getcomposer.org/schem a.json",
3+
"name": "supabase-php/supabase-client",
4+
"description": "Supabase for PHP client.",
5+
"keywords": ["supabase","supabase-php","supabase-client"],
66
"type": "library",
77
"require": {
88
"php": "^8.2",
@@ -43,5 +43,8 @@
4343
"allow-plugins": {
4444
"pestphp/pest-plugin": true
4545
}
46-
}
46+
},
47+
"require-dev": {
48+
"phpstan/phpstan": "^2.0"
49+
}
4750
}

composer.lock

Lines changed: 65 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Exceptions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Supabase;
4+
5+
class Exception{
6+
public static function Error()
7+
{
8+
throw new Exception('Error');
9+
}
10+
};

src/Functions.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Supabase;
35
use Supabase\Supabase;
46

57
class Functions extends Supabase
68
{
7-
public function getAllData($table=null)
9+
public function getAllData(?string $table=null)
810
{
911
if (!isset($table)) {
1012
echo "Please provide your Supabase table name.";
@@ -16,7 +18,7 @@ public function getAllData($table=null)
1618
}
1719
}
1820

19-
public function getSingleData($table=null, $query=[])
21+
public function getSingleData(?string $table=null, array $query=[])
2022
{
2123
if (!isset($table)) {
2224
echo "Please provide your Supabase table name.";
@@ -30,7 +32,7 @@ public function getSingleData($table=null, $query=[])
3032
}
3133
}
3234

33-
public function postData($table=null, $query=[], $on_conflict=null)
35+
public function postData(?string $table=null, array $query=[], ?string $on_conflict=null)
3436
{
3537
if (!isset($table)) {
3638
echo "Please provide your Supabase table name.";
@@ -45,11 +47,11 @@ public function postData($table=null, $query=[], $on_conflict=null)
4547
}
4648

4749
$html = $this->grab($path, "POST", json_encode($query));
48-
return $html;
49-
}
50+
return $html;
51+
}
5052
}
5153

52-
public function updateData($table=null, ?int $id=null, $query=[])
54+
public function updateData(?string $table=null, ?int $id=null, array $query=[])
5355
{
5456
if (!isset($table)) {
5557
echo "Please provide your Supabase table name.";
@@ -64,7 +66,7 @@ public function updateData($table=null, ?int $id=null, $query=[])
6466
}
6567
}
6668

67-
public function deleteData($table=null, ?int $id=null)
69+
public function deleteData(?string $table=null, ?int $id=null)
6870
{
6971
if (!isset($table)) {
7072
echo "Please provide your Supabase table name.";
@@ -77,23 +79,23 @@ public function deleteData($table=null, ?int $id=null)
7779
}
7880
}
7981

80-
public function pages($table=null)
82+
public function pages(?string $table=null)
8183
{
8284
$path = "$this->url/$table?select=*";
8385
$html = $this->grab($path, "GET");
8486
$data = json_decode($html, true);
8587
return $data;
8688
}
8789

88-
public function filter($table=null, ?int $range=null)
90+
public function filter(?string $table=null, ?int $range=null)
8991
{
9092
$path = "$this->url/$table?id=eq.$range&select=*";
9193
$html = $this->grab($path, "GET");
9294
$data = json_decode($html, true);
9395
return $data;
9496
}
9597

96-
public function matchs($table=null, $query=[])
98+
public function matchs(?string $table=null, array $query=[])
9799
{
98100
$path = "$this->url/$table";
99101
$html = $this->grab($path, "POST", json_encode($query));

src/Supabase.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Supabase;
35
use Exception;
46

57
class Supabase
68
{
7-
private string|null $apikey;
8-
protected string|null $url;
9+
private $apikey;
10+
protected $url;
911

10-
public function __construct($url=null, $apikey=null)
12+
public function __construct(?string $url=null, ?string $apikey=null)
1113
{
1214
if (!isset($url)){
1315
throw new Exception("Supabase URL must be specified");
@@ -19,25 +21,30 @@ public function __construct($url=null, $apikey=null)
1921
}
2022
}
2123

22-
protected function grab($url, $method, $data=null)
24+
protected function grab(string $url, string $method, ?string $data=null)
2325
{
2426
$headers = array(
2527
"apikey: $this->apikey",
2628
"Authorization: Bearer $this->apikey",
27-
"Content-Type: application/json",
28-
"Prefer: return=minimal",
29-
"Range: 0-9",
30-
"Prefer: resolution=merge-duplicates"
29+
'Content-Type: application/json',
30+
'Accept: application/json',
31+
'Prefer: return=minimal',
32+
'Range: 0-9',
33+
'Prefer: resolution=merge-duplicates'
3134
);
3235

3336
$options = array(
3437
CURLOPT_URL => $url,
3538
CURLOPT_RETURNTRANSFER => true,
39+
CURLOPT_SSL_VERIFYPEER => true,
40+
CURLOPT_SSL_VERIFYHOST => 2,
3641
CURLOPT_HTTPHEADER => $headers,
3742
CURLOPT_CUSTOMREQUEST => $method,
38-
CURLOPT_TIMEOUT => 120,
39-
CURLOPT_ENCODING => "UTF-8",
40-
CURLOPT_CONNECTTIMEOUT => 120
43+
CURLOPT_TIMEOUT => 30,
44+
CURLOPT_ENCODING => "",
45+
CURLOPT_CONNECTTIMEOUT => 10,
46+
CURLOPT_FRESH_CONNECT => true,
47+
CURLOPT_FORBID_REUSE => true
4148
);
4249

4350
$ch = curl_init();
@@ -50,7 +57,16 @@ protected function grab($url, $method, $data=null)
5057

5158
if(curl_errno($ch)){
5259
$error = curl_error($ch);
53-
echo "Error :". $error;
60+
echo json_encode($error, JSON_PRETTY_PRINT);
61+
}
62+
63+
// Validate HTTP status code
64+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
65+
66+
if ($http_code !== 200) {
67+
echo "Request failed with status code $http_code";
68+
curl_close($ch);
69+
exit;
5470
}
5571
curl_close($ch);
5672
}

0 commit comments

Comments
 (0)