add User Collection, adapt crud, refactor crud, add readme

This commit is contained in:
Martz
2023-02-05 20:06:44 +01:00
parent 1d1beef82a
commit c586b4b9ff
5 changed files with 105 additions and 13 deletions

26
README.md Normal file
View File

@@ -0,0 +1,26 @@
### Php Sdk for Pocketbase
#### Crud adapted from js-sdk to php
```
// Returns a paginated records list.
🔓 pb.collection(collectionIdOrName).getList(int $page = 1, int $perPage = 30, array $queryParams = []);
// Returns a list with all records batch fetched at once.
🔓 pb.collection(collectionIdOrName).getFullList(int $batch = 200, array $queryParams = []);
// Returns the first found record matching the specified filter.
🔓 pb.collection(collectionIdOrName).getFirstListItem(string $filter, array $queryParams = []);
// Returns a single record by its id.
🔓 pb.collection(collectionIdOrName).getOne(string $recordId, array $queryParams = []);
// Creates (aka. register) a new record.
🔓 pb.collection(collectionIdOrName).create(array $bodyParams = [], array $queryParams = []);
// Updates an existing record by its id.
🔓 pb.collection(collectionIdOrName).update(string $recordId, array $bodyParams = [],array $queryParams = []);
// Deletes a single record by its id.
🔓 pb.collection(collectionIdOrName).delete(string $recordId, array $queryParams = []);
```

View File

@@ -10,7 +10,8 @@
"autoload": { "autoload": {
"classmap": [ "classmap": [
"src/Client.php", "src/Client.php",
"src/Collection.php" "src/Collection.php",
"src/UsersCollection.php"
] ]
}, },
"require": { "require": {

View File

@@ -2,26 +2,29 @@
namespace Pb; namespace Pb;
use UsersCollection;
class Client class Client
{ {
private string $response; private string $response;
private string $url; private string $url;
private string $route;
public function __construct(string $url) public function __construct(string $url)
{ {
$this->response = '{}'; $this->response = '{}';
$this->url = $url; $this->url = $url;
$this->users = []; $this->users = [];
} }
public function collection(string $collection, int $page = 1) public function users()
{ {
$ch = curl_init(); return new UsersCollection($this->url, 'records');
curl_setopt($ch, CURLOPT_URL, $this->url."/api/collections/".$collection."/records?page=".$page); }
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$collection = new Collection(curl_exec($ch));
curl_close($ch);
return json_decode($collection, JSON_FORCE_OBJECT); public function collection($url,string $collection, int $page = 1)
{
return new Collection($this->url ,$collection);
} }
} }

View File

@@ -4,14 +4,47 @@ namespace Pb;
class Collection class Collection
{ {
private $json; private string $collection;
private string $url;
public function __construct($data)
public function __construct(string $url, string $collection)
{ {
$this->json = $data; $this->url = $url;
$this->collection = $collection;
} }
public function authWithPassword(){ public function getFullList(int $batch = 200, array $queryParams = []){
var_dump($this->json); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = new Collection(curl_exec($ch));
curl_close($ch);
return json_decode($output, JSON_FORCE_OBJECT);
}
public function getFirstListItem(string $filter, array $queryParams = []){
}
public function create(array $bodyParams = [], array $queryParams = []){
}
public function update(string $recordId, array $bodyParams = [],array $queryParams = []){
}
public function delete(string $recordId, array $queryParams = []){
}
public function getOne(string $recordId, array $queryParams = []){
}
public function authWithPassword()
{
$data = json_decode($this->json, JSON_FORCE_OBJECT);
var_dump($data);
} }
} }

29
src/UsersCollection.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
class UsersCollection
{
private string $url;
private string $collection;
public function __construct(string $url, string $collection)
{
$this->url = $url;
$this->collection = $collection;
}
public function getList($page = 1, $perPage = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/users/records");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, JSON_FORCE_OBJECT);
}
public function authWithPassword()
{
var_dump('try auth');
}
}