mirror of
https://github.com/jonathan-martz/pocketbase-php-sdk.git
synced 2026-04-03 07:27:42 +00:00
add Settings Class, add authAsUser method for collection
This commit is contained in:
@@ -12,7 +12,7 @@ composer require mkay-development/pocketbase-php-sdk
|
|||||||
// Example init and use
|
// Example init and use
|
||||||
use \Pb\Client as pb;
|
use \Pb\Client as pb;
|
||||||
$pb = new pb('https://backend-shop.mkay.dev');
|
$pb = new pb('https://backend-shop.mkay.dev');
|
||||||
var_dump($pb->collection('countries'));
|
var_dump($pb->collection('countries')->getList());
|
||||||
```
|
```
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": [
|
"classmap": [
|
||||||
"src/Client.php",
|
"src/Client.php",
|
||||||
|
"src/Settings.php",
|
||||||
"src/Collection.php"]
|
"src/Collection.php"]
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
|||||||
@@ -15,4 +15,9 @@ class Client
|
|||||||
{
|
{
|
||||||
return new Collection($this->url ,$collection);
|
return new Collection($this->url ,$collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function settings(): Settings
|
||||||
|
{
|
||||||
|
return new Settings($this->url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,19 @@ class Collection
|
|||||||
return json_decode($response, JSON_FORCE_OBJECT);
|
return json_decode($response, JSON_FORCE_OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $email
|
||||||
|
* @param string $password
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function authAsUser(string $email, string $password)
|
||||||
|
{
|
||||||
|
$result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]);
|
||||||
|
if (!empty($result['token'])) {
|
||||||
|
self::$token = $result['token'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $batch
|
* @param int $batch
|
||||||
* @param array $queryParams
|
* @param array $queryParams
|
||||||
|
|||||||
66
src/Settings.php
Normal file
66
src/Settings.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Settings
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private string $url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static string $token = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @param string $collection
|
||||||
|
*/
|
||||||
|
public function __construct(string $url)
|
||||||
|
{
|
||||||
|
$this->url = $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $recordId
|
||||||
|
* @param string $url
|
||||||
|
* @param string $method
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
if (self::$token != '') {
|
||||||
|
$headers = array(
|
||||||
|
'Content-Type:application/json',
|
||||||
|
'Authorization: ' . self::$token
|
||||||
|
);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($bodyParams) {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
|
$output = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getAll(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user