diff --git a/README.md b/README.md index 09704c4..06973ce 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ composer require mkay-development/pocketbase-php-sdk // Example init and use use \Pb\Client as pb; $pb = new pb('https://backend-shop.mkay.dev'); -var_dump($pb->collection('countries')); +var_dump($pb->collection('countries')->getList()); ``` *** diff --git a/composer.json b/composer.json index 8aa7d40..120bf2b 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "autoload": { "classmap": [ "src/Client.php", + "src/Settings.php", "src/Collection.php"] }, "require": { diff --git a/src/Client.php b/src/Client.php index e8472ec..30cae1d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -15,4 +15,9 @@ class Client { return new Collection($this->url ,$collection); } + + public function settings(): Settings + { + return new Settings($this->url); + } } diff --git a/src/Collection.php b/src/Collection.php index 4cd8599..5722b32 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -46,6 +46,19 @@ class Collection 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 array $queryParams diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000..2529576 --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,66 @@ +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(){ + + } +} \ No newline at end of file