diff --git a/README.md b/README.md new file mode 100644 index 0000000..a28c72b --- /dev/null +++ b/README.md @@ -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 = []); +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 2245b85..0a172f8 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "autoload": { "classmap": [ "src/Client.php", - "src/Collection.php" + "src/Collection.php", + "src/UsersCollection.php" ] }, "require": { diff --git a/src/Client.php b/src/Client.php index 97a31dc..b97e438 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,26 +2,29 @@ namespace Pb; +use UsersCollection; + class Client { private string $response; private string $url; + private string $route; public function __construct(string $url) { $this->response = '{}'; $this->url = $url; $this->users = []; + } - public function collection(string $collection, int $page = 1) + public function users() { - $ch = curl_init(); - 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 new UsersCollection($this->url, 'records'); + } - return json_decode($collection, JSON_FORCE_OBJECT); + public function collection($url,string $collection, int $page = 1) + { + return new Collection($this->url ,$collection); } } diff --git a/src/Collection.php b/src/Collection.php index e4d5ac0..b00ab7a 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -4,14 +4,47 @@ namespace Pb; 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(){ - var_dump($this->json); + public function getFullList(int $batch = 200, array $queryParams = []){ + $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); } } diff --git a/src/UsersCollection.php b/src/UsersCollection.php new file mode 100644 index 0000000..ff40b72 --- /dev/null +++ b/src/UsersCollection.php @@ -0,0 +1,29 @@ +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'); + } +}